msgLog.c File Reference

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include "minosDaq.h"
#include "msgLog.h"
#include "msgCommon.h"
#include "fake_clock_gettime.h"

Go to the source code of this file.

Defines

#define NATIVE_MSG(args...)   syslog(DAQ_LOG_FACILITY|LOG_ERR, ## args)

Functions

void logMessage (int priority, const char *fmt, va_list args)
static void msgSend (int priority)
void msgLogInit (const char *ident)
void msgLogCleanup (void)
void msgLogNodeIdSet (long nodeId)
void msgLogPidSet (long pid)
void msgLogLocalEchoSet (int enable)
void logMaxRepRateSet (long max_dt_ns)
void logDebugLevelSet (unsigned int level)
void msgLogResetMsgCount (long count)
long msgLogGetMsgCount ()
void logCritical (const char *fmt,...)
void logError (const char *fmt,...)
void logWarn (const char *fmt,...)
void logNotice (const char *fmt,...)
void logInfo (const char *fmt,...)
void logDebug (unsigned int level, const char *fmt,...)

Variables

static unsigned int lDebugLevel = 0
static long lMaxRptDt = MAX_DTMSG_BUFFER
static int lSocket = 0
static struct sockaddr_in lServerAddr
static mdMsgLogBuffer lBuffer
static int lMsgLocalEcho = 0
static const char logTag [] = "EACEWNID"
static struct timespec lastMessageTime

Define Documentation

#define NATIVE_MSG ( args...   )     syslog(DAQ_LOG_FACILITY|LOG_ERR, ## args)

Definition at line 163 of file msgLog.c.

Referenced by msgLogInit(), and msgSend().


Function Documentation

void logCritical ( const char *  fmt,
  ... 
)

Definition at line 571 of file msgLog.c.

Referenced by main(), MAIN_FUNCTION(), MsgLogTErrorHandler(), and RotoServer::Run().

00571                                        {
00572 
00573     va_list args;
00574     va_start(args, fmt);
00575     logMessage(LOG_CRIT, fmt, args);
00576     va_end(args);
00577 
00578 } /* logCritical */

void logDebug ( unsigned int  level,
const char *  fmt,
  ... 
)

Definition at line 667 of file msgLog.c.

Referenced by DbuRunSummary::Commit(), DbuDaqFileSummary::Commit(), DbuSignalHandler::Delay(), DbuDaqFileModule::DoBeginEndRunAndFile(), DbuDaqMonitorModule::EndJob(), DbuSignalHandler::HandleDelayedSignal(), DbuDaqMonitorModule::InitTSQLConnection(), main(), DbuSIGTERMHandler::Notify(), DbuSIGINTHandler::Notify(), RotoServer::ProcessBuffer(), DbuDaqMonitorModule::ProcessRawRecord(), DbuDaqFileModule::ProcessRawRecord(), roto_open_connection(), roto_send_autosave_config(), roto_send_basketsize_config(), roto_send_compress_config(), roto_send_record(), RotoServer::Run(), send_bogus_stuff(), and DbuDaqMonitorModule::WriteSubRunSummaryEntry().

00667                                                         {
00668 
00669     va_list args;
00670 
00671     /* 
00672      * Check requested level against threshold - values < 1 are
00673      * prohibited and reset to one to avoid situation where some
00674      * debug messages always get through.
00675      */
00676     if (level < 1) {
00677        level = 1;
00678     }
00679     if (level <= lDebugLevel) {
00680        va_start(args, fmt);
00681        logMessage(LOG_DEBUG, fmt, args);
00682        va_end(args);
00683     }
00684 
00685 } /* logDebug */

void logDebugLevelSet ( unsigned int  level  ) 

Definition at line 518 of file msgLog.c.

Referenced by main(), and MAIN_FUNCTION().

00521 {
00522 
00523    lDebugLevel = level;
00524 
00525 } /* logDebugLevelSet */

void logError ( const char *  fmt,
  ... 
)
void logInfo ( const char *  fmt,
  ... 
)
void logMaxRepRateSet ( long  max_dt_ns  ) 

Definition at line 498 of file msgLog.c.

00501 {
00502   lMaxRptDt = max_dt_ns;
00503 }

void logMessage ( int  priority,
const char *  fmt,
va_list  args 
)

MSG_USERPT

Definition at line 699 of file msgLog.c.

References clock_gettime(), CLOCK_REALTIME, DAQ_LOG_FACILITY, mdMsgLogBuffer_t::hdr, lastMessageTime, len, lMaxRptDt, logTag, MAX_DTMSG_BUFFER, MAX_RPTMSG_BUFFER, mdMsgLogBuffer_t::message, MSGLOG_MAXMESSAGE, msgSend(), and mdMsgLogHeader_t::priority.

Referenced by logCritical(), logDebug(), logError(), logInfo(), logNotice(), and logWarn().

00699                                                              {
00700 
00701     int len = 0;
00702     static char lastMessage[MSGLOG_MAXMESSAGE]="";
00703     static int lastMessageCount=0;    
00704 
00705     static struct timespec thisMessageTime;
00706     clock_gettime(CLOCK_REALTIME,&thisMessageTime);
00707 
00708 
00709     /* Build message from argument list */
00710 #ifdef VxWorks
00711     vsprintf(lBuffer.message, fmt, args);
00712 #else
00713     vsnprintf(lBuffer.message, MSGLOG_MAXMESSAGE, fmt, args);
00714 #endif
00715 
00716     /* Discard any trailing new lines as we'll force one if necessary*/
00717     len = strlen(lBuffer.message);
00718     if (lBuffer.message[len-1] == '\n') lBuffer.message[len-1]=0;
00719 
00720 #ifdef USESYSLOG
00721 #ifdef VxWorks
00722     logMsg("<%c> %s\n", (int)logTag[priority], (int)lBuffer.message, 0, 0, 0, 0);
00723     printf("<%c> %s\n", logTag[priority], lBuffer.message);
00724 
00725 #else /* !VxWorks */
00726     syslog(DAQ_LOG_FACILITY | priority, "<%c> %s\n", logTag[priority], lBuffer.message);
00727 
00728 #endif /* VxWorks */
00729 #else  /* !USESYSLOG */
00730 
00731     /* Check if this is the same message as last time */
00732     len = strncmp(lastMessage, lBuffer.message, MSGLOG_MAXMESSAGE);
00733 
00734     /* Decide whether this is a repeated message and if it needs to be sent or not */
00735     long long deltat = (thisMessageTime.tv_sec-lastMessageTime.tv_sec)*1e9 +  (thisMessageTime.tv_nsec-lastMessageTime.tv_nsec);
00736 
00737     if(lastMessageCount!=0 && len==0 && lastMessageCount<MAX_RPTMSG_BUFFER && deltat<lMaxRptDt) {
00738       
00739         /* Same message - less than max counts nothing needs to be sent*/
00740         lastMessageCount++;
00741 
00742     } else{ 
00743       /* Either new message, or we've exceeded max count so send "repeated" message */
00744       if((len!=0 && lastMessageCount>1) || 
00745          (len==0 && (lastMessageCount>=MAX_RPTMSG_BUFFER || deltat>MAX_DTMSG_BUFFER) )) {
00746 #ifdef MSG_USERPT /*use the repeat message*/
00747           sprintf(lBuffer.message, "Last message repeated %d times", lastMessageCount);
00748           msgSend(LOG_INFO); 
00749 #ifdef VxWorks
00750           vsprintf(lBuffer.message, fmt, args);
00751 #else
00752           vsnprintf(lBuffer.message, MSGLOG_MAXMESSAGE, fmt, args);
00753 #endif
00754           lBuffer.hdr.priority = priority;        
00755 #else 
00756           strncpy(lastMessage, lBuffer.message, MSGLOG_MAXMESSAGE);
00757           msgSend(priority);
00758 #endif /*MSG_USERPT*/
00759       }
00760       /* New message, send it */
00761       if( len!=0 ) {
00762           strncpy(lastMessage, lBuffer.message, MSGLOG_MAXMESSAGE);
00763           msgSend(priority);      
00764       }
00765       /* Reset last message count */
00766       lastMessageTime.tv_sec = thisMessageTime.tv_sec;
00767       lastMessageTime.tv_nsec = thisMessageTime.tv_nsec;
00768       lastMessageCount=1;
00769     }
00770 
00771 #endif /* USESYSLOG */
00772 
00773     return;
00774 }

void logNotice ( const char *  fmt,
  ... 
)
void logWarn ( const char *  fmt,
  ... 
)
void msgLogCleanup ( void   ) 

Definition at line 389 of file msgLog.c.

Referenced by main(), MAIN_FUNCTION(), and roto_sighandler().

00390 {
00391 
00392 #ifdef VxWorks
00393     /* Clean up any existing msgLogger task and message queue */
00394     if (lMsgLogTaskId != NULL) {
00395         taskDelete(lMsgLogTaskId);
00396         lMsgLogTaskId = NULL;
00397     }
00398     if (lMsgLogQId != NULL) {
00399         msgQDelete(lMsgLogQId);
00400         lMsgLogQId = NULL;
00401     }
00402 #endif
00403 
00404 #ifndef USESYSLOG
00405     /* Close the UDP socket if it is open */
00406     if (lSocket != 0) {
00407       close(lSocket);
00408       lSocket = 0;
00409     }
00410 #endif
00411 
00412 } /* msgLogCleanup */

long msgLogGetMsgCount (  ) 

Definition at line 546 of file msgLog.c.

Referenced by main().

00547 {
00548 #ifndef USESYSLOG
00549 #ifndef VxWorks
00550   return (lBuffer.hdr.count);
00551 #endif
00552 #else
00553   return -1;
00554 #endif
00555 } /*  msgLogResetMsgCount */

void msgLogInit ( const char *  ident  ) 

Definition at line 219 of file msgLog.c.

Referenced by main(), and MAIN_FUNCTION().

00222 {
00223 
00224 #ifdef USESYSLOG
00225 #ifdef VxWorks    
00226     /* Do nothing */
00227 #else /* !VxWorks */
00228     openlog(ident, LOG_PERROR | LOG_PID, DAQ_LOG_FACILITY);
00229 #endif /* VxWorks */
00230 
00231 #else /* !USESYSLOG */
00232 
00233   const char* ip;
00234   const char* portenv;
00235   int port;
00236   int sockAddrSize;
00237   int pid;
00238 
00239   /*set last message time to 01/01/70
00240    * this way first message is always sent */
00241   lastMessageTime.tv_sec = 0; 
00242   lastMessageTime.tv_nsec = 0;
00243 
00244   sockAddrSize = sizeof(struct sockaddr_in);
00245 
00246   /* Clear socket address structure */
00247   bzero((char *)&lServerAddr, sockAddrSize);
00248 
00249   /* 
00250    * For VxWorks we pull the message server IP address directly from
00251    * the boot parameters. Otherwise, the server name/IP is resolved from
00252    * environment variables or falls back to the local host 
00253    */
00254 #ifdef VxWorks
00255   {
00256       char bootString[BOOT_LINE_SIZE];
00257       BOOT_PARAMS bootParams;
00258 
00259       /* 
00260        * Get the msgLogger IP adddress from the "other" field of the boot
00261        * parameters
00262        */
00263       sysNvRamGet(bootString, BOOT_LINE_SIZE, 0);
00264       bootStringToStruct(bootString, &bootParams);
00265       ip = (char *)&(bootParams.other);
00266       port = MSGLOG_PORT;
00267 
00268       if ((lServerAddr.sin_addr.s_addr = inet_addr(ip)) == ERROR) {
00269           NATIVE_MSG("msgLogInit: bad server name from boot params: %s", ip);
00270           return;
00271       }
00272       pid = taskIdSelf();
00273 
00274       /* Clean up any existing msgLogger task and message queue */
00275       if (lMsgLogTaskId != NULL) {
00276           NATIVE_MSG("msgLogInit: deleting existing message logger task (tid=0x%x)\n", lMsgLogTaskId);
00277           taskDelete(lMsgLogTaskId);
00278           lMsgLogTaskId = NULL;
00279       }
00280       if (lMsgLogQId != NULL) {
00281           NATIVE_MSG("msgLogInit: deleting existing message logger queue (id=0x%x)\n", lMsgLogQId);
00282           msgQDelete(lMsgLogQId);
00283           lMsgLogQId = NULL;
00284       }
00285 
00286       /* Create the message queue for the logging task */
00287       lMsgLogQId = msgQCreate(MSGQ_MAX_MSGS, sizeof(mdMsgLogBuffer), MSG_Q_FIFO);
00288       if (lMsgLogQId == NULL) {
00289           NATIVE_MSG("msgLogInit: msgQCreate failed: %s", strerror(errno));
00290           return;
00291       }
00292       /* Spawn the message logging task */
00293       lMsgLogTaskId = taskSpawn("tMsgLogger", TASK_PRIORITY_MSGLOGGER, 0, 
00294                                 TASK_SIZE_MSGLOGGER, (FUNCPTR)tMsgLogger,
00295                                 0,0,0,0,0,0,0,0,0,0);
00296       if (lMsgLogTaskId == ERROR) {
00297           NATIVE_MSG("msgLogInit: task creation failed: %s", strerror(errno));
00298           return;
00299       }
00300   }
00301 #else /* !Vxworks */
00302   {
00303       struct hostent     *host_ptr; /*the host add*/
00304 
00305       /* 
00306        * Pull the msg logger IP address and port from env vars, otherwise
00307        * take the default values
00308        */      
00309       ip = getenv(DAQ_MSG_IP);
00310       if (ip == NULL) {
00311           ip = MSGLOG_DEFAULT_IP;
00312       }
00313       portenv = getenv("DAQ_MSG_PORT");
00314       if ( portenv == NULL){
00315           port = MSGLOG_PORT;
00316       } else {
00317           sscanf(portenv, "%d", &port);
00318       }
00319   
00320       /* Resolve server hostname to address */
00321       host_ptr = gethostbyname(ip);
00322       if(!host_ptr ||host_ptr->h_addrtype != AF_INET ){
00323           NATIVE_MSG( "msgLogInit: bad server name/address: %s , %s",ip, strerror(errno));
00324           return;
00325       }
00326       memcpy(&lServerAddr.sin_addr.s_addr, host_ptr->h_addr, host_ptr->h_length);
00327       if(lServerAddr.sin_addr.s_addr == INADDR_NONE)
00328         {
00329           NATIVE_MSG( "msgLogInit: unresolved server address, unable to open msgLog socket\n");
00330           return;
00331         }
00332 
00333       /* Get current process ID */ 
00334       pid = getpid();
00335   
00336       /* Open syslog connection for fallback (native) messages */
00337       openlog(ident, LOG_PERROR | LOG_PID, DAQ_LOG_FACILITY);
00338   }
00339 #endif /* VxWorks */
00340 
00341   /* Fill in port and type in socket address structure */
00342   /* lServerAddr.sin_len    = (unsigned char)sockAddrSize; */
00343   lServerAddr.sin_family = AF_INET;
00344   lServerAddr.sin_port   = htons(port);
00345 
00346   /* Close the old socket if it exists */
00347   if (lSocket != 0) {
00348     NATIVE_MSG("msgLogInit: closing existing message logger socket (id=0x%x)\n", lSocket);
00349     close(lSocket);
00350     lSocket = 0;
00351   }
00352 
00353   /* Create the UDP datagram socket */
00354   lSocket = socket(AF_INET, SOCK_DGRAM, 0);
00355   if(lSocket == -1) {
00356       NATIVE_MSG("msgLogInit: socket() failed: %s", strerror(errno));
00357       return;
00358   }
00359     
00360   /* Initialise message header */
00361   lBuffer.hdr.pid = pid;
00362   lBuffer.hdr.count = 0;
00363   lBuffer.hdr.nodeFrom = MINOS_UNIDENTIFIED_CLIENT;
00364   strncpy(lBuffer.hdr.prcName, ident, MSGLOG_MAXNAME);
00365 
00366   /* Set local message echoing off by default */
00367   lMsgLocalEcho = 0;
00368 
00369 #endif /* USESYSLOG */
00370   
00371   /* Set default debug level to zero */
00372   lDebugLevel = 0;
00373 
00374   return;
00375 
00376 } /* msgLogInit */

void msgLogLocalEchoSet ( int  enable  ) 

Definition at line 475 of file msgLog.c.

Referenced by main(), and MAIN_FUNCTION().

00478 {
00479 #ifdef USESYSLOG
00480   
00481 #else   
00482   lMsgLocalEcho = enable;
00483 #endif
00484   return;
00485 
00486 } /* msgLogLocalEchoSet */

void msgLogNodeIdSet ( long  nodeId  ) 

Definition at line 427 of file msgLog.c.

Referenced by main(), and MAIN_FUNCTION().

00430 {
00431 #ifdef USESYSLOG
00432   
00433 #else   
00434   lBuffer.hdr.nodeFrom = nodeId;
00435 #endif
00436   return;
00437 
00438 } /* msgLoNodeIdSet */

void msgLogPidSet ( long  pid  ) 

Definition at line 451 of file msgLog.c.

Referenced by main().

00454 {
00455 #ifdef USESYSLOG
00456   
00457 #else   
00458   lBuffer.hdr.pid = pid;
00459 #endif
00460   return;
00461 
00462 } /* msgLogPidSet */

void msgLogResetMsgCount ( long  count  ) 

Definition at line 537 of file msgLog.c.

Referenced by main().

00538 {
00539 #ifndef USESYSLOG
00540 #ifndef VxWorks
00541   lBuffer.hdr.count = count;
00542 #endif
00543 #endif
00544 } /*  msgLogResetMsgCount */

static void msgSend ( int  priority  )  [static]

Definition at line 790 of file msgLog.c.

References clock_gettime(), CLOCK_REALTIME, mdMsgLogHeader_t::count, ERROR, mdMsgLogBuffer_t::hdr, len, mdMsgLogHeader_t::length, lMsgLocalEcho, logTag, lServerAddr, lSocket, mdMsgLogBuffer_t::message, MSGLOG_MAXNAME, NATIVE_MSG, mdMsgLogHeader_t::pid, mdMsgLogHeader_t::prcName, mdMsgLogHeader_t::priority, STATUS, and mdMsgLogHeader_t::stime.

Referenced by logMessage().

00793 {
00794 
00795   int len=0;
00796 
00797   /* Assemble the message header */
00798   lBuffer.hdr.priority = priority;
00799   lBuffer.hdr.length = strlen(lBuffer.message);
00800   lBuffer.hdr.count++;
00801   lBuffer.hdr.stime = 0;
00802 
00803   /* Complete the header OS-dependent fields then send the message */
00804 #ifdef VxWorks
00805   {
00806       struct timespec msgTime;
00807       STATUS rc;
00808       if (clock_gettime(CLOCK_REALTIME, &msgTime) == 0) {
00809           lBuffer.hdr.stime = msgTime.tv_sec;
00810       }
00811       /* VxWorks PID and name depend on current task context */
00812       lBuffer.hdr.pid   = taskIdSelf();
00813       strncpy(lBuffer.hdr.prcName, (char *)taskName(lBuffer.hdr.pid), MSGLOG_MAXNAME);
00814   
00815       /* Push message into message queue for logging task to handle */
00816       rc = msgQSend(lMsgLogQId, (char *)&lBuffer, sizeof(mdMsgLogBuffer), 
00817               NO_WAIT, MSG_PRI_NORMAL);
00818       if (rc == ERROR) {
00819           NATIVE_MSG("msgSend: msgQSend failed: %s", strerror(errno));
00820       }
00821 
00822   }
00823 #else /* !VxWorks */
00824   {
00825       struct timeval msgTime;
00826       if(gettimeofday(&msgTime, 0)==0) {
00827           lBuffer.hdr.stime = msgTime.tv_sec;
00828       }
00829       /* Send the message */  
00830       len = sendto(lSocket, (char *)&lBuffer, sizeof(mdMsgLogBuffer), MSG_DONTWAIT, 
00831                    (struct sockaddr *)&lServerAddr, sizeof(struct sockaddr_in)); 
00832       if(len < 0) {
00833           NATIVE_MSG( "msgSend: sendto() failed: %s", strerror(errno));
00834       }
00835 
00836   }
00837 #endif /* VxWorks */
00838   
00839   if (lMsgLocalEcho) {
00840       printf("%-.10s[%ld]: <%c> %-.100s\n", lBuffer.hdr.prcName, 
00841              lBuffer.hdr.pid, logTag[lBuffer.hdr.priority], lBuffer.message);  
00842   }
00843 
00844   return;
00845 }


Variable Documentation

struct timespec lastMessageTime [static]

Definition at line 190 of file msgLog.c.

Referenced by logMessage(), and msgLogInit().

Definition at line 185 of file msgLog.c.

Referenced by RotoServer::RecvCommand().

unsigned int lDebugLevel = 0 [static]

Definition at line 173 of file msgLog.c.

Referenced by logDebug(), logDebugLevelSet(), and msgLogInit().

long lMaxRptDt = MAX_DTMSG_BUFFER [static]

Definition at line 174 of file msgLog.c.

Referenced by logMaxRepRateSet(), and logMessage().

int lMsgLocalEcho = 0 [static]

Definition at line 186 of file msgLog.c.

Referenced by msgLogInit(), msgLogLocalEchoSet(), and msgSend().

const char logTag[] = "EACEWNID" [static]

Definition at line 188 of file msgLog.c.

Referenced by logMessage(), MAIN_FUNCTION(), and msgSend().

struct sockaddr_in lServerAddr [static]

Definition at line 184 of file msgLog.c.

Referenced by msgLogInit(), and msgSend().

int lSocket = 0 [static]

Definition at line 183 of file msgLog.c.

Referenced by msgLogCleanup(), msgLogInit(), and msgSend().


Generated on 3 Dec 2018 for loon by  doxygen 1.6.1