00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 #include <stdio.h>
00118 #include <stdarg.h>
00119 #include <string.h>
00120 #include <time.h>
00121
00122 #ifdef VxWorks
00123 #include <bootLib.h>
00124 #include <logLib.h>
00125 #include <socket.h>
00126 #include <sockLib.h>
00127 #include <stdLib.h>
00128 #include <sysLib.h>
00129 #include <in.h>
00130 #include <ioLib.h>
00131 #include <msgQLib.h>
00132 #else
00133 #include <stdlib.h>
00134 #include <syslog.h>
00135 #include <sys/types.h>
00136 #include <sys/socket.h>
00137 #include <netinet/in.h>
00138 #include <arpa/inet.h>
00139 #include <sys/time.h>
00140 #include <errno.h>
00141 #include <unistd.h>
00142 #include <netdb.h>
00143 #endif
00144
00145 #include "minosDaq.h"
00146 #include "msgLog.h"
00147 #include "msgCommon.h"
00148
00149 #ifndef CLOCK_REALTIME
00150 #include "fake_clock_gettime.h"
00151 #endif
00152
00153
00154 #ifdef VxWorks
00155 #define BOOT_LINE_SIZE 255
00156 #define TASK_SIZE_MSGLOGGER 2000
00157 #define TASK_PRIORITY_MSGLOGGER 1
00158 #define MSGQ_MAX_MSGS 100
00159 #define ENDIAN_SWAP_LONG(w) ( ((w&0xff)<<24) | ((w&0xff00)<<8) | ((w&0xff0000)>>8) | ((w&0xff000000)>>24) )
00160 #define ENDIAN_SWAP_SHORT(w) ( ((w&0xff)<<8) | ((w&0xff00)>>8) )
00161 #define NATIVE_MSG(args...) printf(## args)
00162 #else
00163 #define NATIVE_MSG(args...) syslog(DAQ_LOG_FACILITY|LOG_ERR, ## args)
00164 #endif
00165
00166
00167
00168
00169
00170
00171
00172
00173 static unsigned int lDebugLevel = 0;
00174 static long lMaxRptDt = MAX_DTMSG_BUFFER;
00175
00176 #ifdef USESYSLOG
00177 static mdMsgLogBuffer lBuffer;
00178 #else
00179 #ifdef VxWorks
00180 static MSG_Q_ID lMsgLogQId = NULL;
00181 static int lMsgLogTaskId = NULL;
00182 #endif
00183 static int lSocket = 0;
00184 static struct sockaddr_in lServerAddr;
00185 static mdMsgLogBuffer lBuffer;
00186 static int lMsgLocalEcho = 0;
00187 #endif
00188 static const char logTag[]= "EACEWNID";
00189
00190 static struct timespec lastMessageTime;
00191
00192
00193 void logMessage(int priority, const char *fmt, va_list args);
00194 #ifdef USESYSLOG
00195 #else
00196 #ifdef VxWorks
00197 STATUS tMsgLogger(void);
00198 #endif
00199 static void msgSend(int priority);
00200 #endif
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 void msgLogInit
00219 (
00220 const char *ident
00221 )
00222 {
00223
00224 #ifdef USESYSLOG
00225 #ifdef VxWorks
00226
00227 #else
00228 openlog(ident, LOG_PERROR | LOG_PID, DAQ_LOG_FACILITY);
00229 #endif
00230
00231 #else
00232
00233 const char* ip;
00234 const char* portenv;
00235 int port;
00236 int sockAddrSize;
00237 int pid;
00238
00239
00240
00241 lastMessageTime.tv_sec = 0;
00242 lastMessageTime.tv_nsec = 0;
00243
00244 sockAddrSize = sizeof(struct sockaddr_in);
00245
00246
00247 bzero((char *)&lServerAddr, sockAddrSize);
00248
00249
00250
00251
00252
00253
00254 #ifdef VxWorks
00255 {
00256 char bootString[BOOT_LINE_SIZE];
00257 BOOT_PARAMS bootParams;
00258
00259
00260
00261
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
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
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
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
00302 {
00303 struct hostent *host_ptr;
00304
00305
00306
00307
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
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
00334 pid = getpid();
00335
00336
00337 openlog(ident, LOG_PERROR | LOG_PID, DAQ_LOG_FACILITY);
00338 }
00339 #endif
00340
00341
00342
00343 lServerAddr.sin_family = AF_INET;
00344 lServerAddr.sin_port = htons(port);
00345
00346
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
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
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
00367 lMsgLocalEcho = 0;
00368
00369 #endif
00370
00371
00372 lDebugLevel = 0;
00373
00374 return;
00375
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 void msgLogCleanup(void)
00390 {
00391
00392 #ifdef VxWorks
00393
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
00406 if (lSocket != 0) {
00407 close(lSocket);
00408 lSocket = 0;
00409 }
00410 #endif
00411
00412 }
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426 void msgLogNodeIdSet
00427 (
00428 long nodeId
00429 )
00430 {
00431 #ifdef USESYSLOG
00432
00433 #else
00434 lBuffer.hdr.nodeFrom = nodeId;
00435 #endif
00436 return;
00437
00438 }
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 void msgLogPidSet
00451 (
00452 long pid
00453 )
00454 {
00455 #ifdef USESYSLOG
00456
00457 #else
00458 lBuffer.hdr.pid = pid;
00459 #endif
00460 return;
00461
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474 void msgLogLocalEchoSet
00475 (
00476 int enable
00477 )
00478 {
00479 #ifdef USESYSLOG
00480
00481 #else
00482 lMsgLocalEcho = enable;
00483 #endif
00484 return;
00485
00486 }
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497 void logMaxRepRateSet
00498 (
00499 long max_dt_ns
00500 )
00501 {
00502 lMaxRptDt = max_dt_ns;
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517 void logDebugLevelSet
00518 (
00519 unsigned int level
00520 )
00521 {
00522
00523 lDebugLevel = level;
00524
00525 }
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537 void msgLogResetMsgCount(long count)
00538 {
00539 #ifndef USESYSLOG
00540 #ifndef VxWorks
00541 lBuffer.hdr.count = count;
00542 #endif
00543 #endif
00544 }
00545
00546 long msgLogGetMsgCount()
00547 {
00548 #ifndef USESYSLOG
00549 #ifndef VxWorks
00550 return (lBuffer.hdr.count);
00551 #endif
00552 #else
00553 return -1;
00554 #endif
00555 }
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571 void logCritical(const char *fmt, ...) {
00572
00573 va_list args;
00574 va_start(args, fmt);
00575 logMessage(LOG_CRIT, fmt, args);
00576 va_end(args);
00577
00578 }
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590 void logError(const char *fmt, ...) {
00591
00592 va_list args;
00593 va_start(args, fmt);
00594 logMessage(LOG_ERR, fmt, args);
00595 va_end(args);
00596
00597 }
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609 void logWarn(const char *fmt, ...) {
00610
00611 va_list args;
00612 va_start(args, fmt);
00613 logMessage(LOG_WARNING, fmt, args);
00614 va_end(args);
00615
00616 }
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628 void logNotice(const char *fmt, ...) {
00629
00630 va_list args;
00631 va_start(args, fmt);
00632 logMessage(LOG_NOTICE, fmt, args);
00633 va_end(args);
00634
00635 }
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647 void logInfo(const char *fmt, ...) {
00648
00649 va_list args;
00650 va_start(args, fmt);
00651 logMessage(LOG_INFO, fmt, args);
00652 va_end(args);
00653
00654 }
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667 void logDebug(unsigned int level, const char *fmt, ...) {
00668
00669 va_list args;
00670
00671
00672
00673
00674
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 }
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699 void logMessage(int priority, const char *fmt, va_list args) {
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
00710 #ifdef VxWorks
00711 vsprintf(lBuffer.message, fmt, args);
00712 #else
00713 vsnprintf(lBuffer.message, MSGLOG_MAXMESSAGE, fmt, args);
00714 #endif
00715
00716
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
00726 syslog(DAQ_LOG_FACILITY | priority, "<%c> %s\n", logTag[priority], lBuffer.message);
00727
00728 #endif
00729 #else
00730
00731
00732 len = strncmp(lastMessage, lBuffer.message, MSGLOG_MAXMESSAGE);
00733
00734
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
00740 lastMessageCount++;
00741
00742 } else{
00743
00744 if((len!=0 && lastMessageCount>1) ||
00745 (len==0 && (lastMessageCount>=MAX_RPTMSG_BUFFER || deltat>MAX_DTMSG_BUFFER) )) {
00746 #ifdef MSG_USERPT
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
00759 }
00760
00761 if( len!=0 ) {
00762 strncpy(lastMessage, lBuffer.message, MSGLOG_MAXMESSAGE);
00763 msgSend(priority);
00764 }
00765
00766 lastMessageTime.tv_sec = thisMessageTime.tv_sec;
00767 lastMessageTime.tv_nsec = thisMessageTime.tv_nsec;
00768 lastMessageCount=1;
00769 }
00770
00771 #endif
00772
00773 return;
00774 }
00775
00776 #ifndef USESYSLOG
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789 static void msgSend
00790 (
00791 int priority
00792 )
00793 {
00794
00795 int len=0;
00796
00797
00798 lBuffer.hdr.priority = priority;
00799 lBuffer.hdr.length = strlen(lBuffer.message);
00800 lBuffer.hdr.count++;
00801 lBuffer.hdr.stime = 0;
00802
00803
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
00812 lBuffer.hdr.pid = taskIdSelf();
00813 strncpy(lBuffer.hdr.prcName, (char *)taskName(lBuffer.hdr.pid), MSGLOG_MAXNAME);
00814
00815
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
00824 {
00825 struct timeval msgTime;
00826 if(gettimeofday(&msgTime, 0)==0) {
00827 lBuffer.hdr.stime = msgTime.tv_sec;
00828 }
00829
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
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 }
00846
00847 #ifdef VxWorks
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 STATUS tMsgLogger(void)
00860 {
00861
00862 mdMsgLogBuffer message;
00863 int msgLen, sendLen;
00864
00865
00866 while (1) {
00867 msgLen = msgQReceive(lMsgLogQId, (char *)&message, sizeof(mdMsgLogBuffer), WAIT_FOREVER);
00868 if (msgLen == ERROR) {
00869 NATIVE_MSG("tMsgLogger: msgQReceive failed: %s", strerror(errno));
00870 return ERROR;
00871 } else if (msgLen != sizeof(mdMsgLogBuffer)) {
00872 NATIVE_MSG("tMsgLogger: bad message length: %d", msgLen);
00873 } else {
00874
00875 message.hdr.nodeFrom = ENDIAN_SWAP_LONG(message.hdr.nodeFrom);
00876 message.hdr.priority = ENDIAN_SWAP_SHORT(message.hdr.priority);
00877 message.hdr.pid = ENDIAN_SWAP_LONG(message.hdr.pid);
00878 message.hdr.stime = ENDIAN_SWAP_LONG(message.hdr.stime);
00879 message.hdr.count = ENDIAN_SWAP_LONG(message.hdr.count);
00880 message.hdr.length = ENDIAN_SWAP_LONG(message.hdr.length);
00881
00882
00883 sendLen = sendto(lSocket, (char *)&message, sizeof(mdMsgLogBuffer), MSG_DONTWAIT,
00884 (struct sockaddr *)&lServerAddr, sizeof(struct sockaddr_in));
00885 if (sendLen < 0) {
00886 NATIVE_MSG("tMsgLogger: sendto() failed: %s", strerror(errno));
00887 }
00888 }
00889 }
00890
00891 return OK;
00892
00893 }
00894 #endif
00895 #endif