From 6b27a7349177bf71210219c74a96ec33af96ea29 Mon Sep 17 00:00:00 2001 From: zolliker Date: Fri, 18 Jan 2008 07:20:09 +0000 Subject: [PATCH] - changed logging functionality (CompactCommandLog option) --- commandlog.c | 278 ++++++++++++++++++++++++++++++--------------------- commandlog.h | 3 + 2 files changed, 169 insertions(+), 112 deletions(-) diff --git a/commandlog.c b/commandlog.c index f1825126..1f315529 100644 --- a/commandlog.c +++ b/commandlog.c @@ -11,6 +11,11 @@ Added a tail facility Mark Koennecke, October 1999 + + Added compact mode: + - timestamps look different and are omitted if no other text is written + - socket number information is written on the timestamp line + --------------------------------------------------------------------------*/ #include #include @@ -34,118 +39,170 @@ /*-------------------- the tail buffer ---------------------------------*/ static pCircular pTail = NULL; #define MAXTAIL 1000 -/*----------------------------------------------------------------------*/ +#define NOID -1964 + static time_t lastStamp = 0; + static time_t iCompact = 0; static time_t tLastWrite = 0; + char *cmdPrompt=">"; + static int lastId=NOID; + + static time_t tLogfile = 0; + static time_t tStamp = 0; + static int iEnd = 1; + static int iAutoActive = 0; + static int iIntervall = 60; /*----------------------------------------------------------------------*/ - void WriteToCommandLog(char *prompt,char *text) + void WriteToCommandLogId(char *prompt, int id, char *text) { - int iNL = 0, iPos; - char *pPtr = NULL, *pCopy = NULL, *pText = NULL; - char myBuffer[1024]; + int l, iPos; + char *pPtr = NULL, *pCopy = NULL, *strippedText = text; + struct tm *nowTm; + time_t now; + char stamp1[32], stamp2[32], buffer[80]; + int doStamp, doStampId; + + /* suppress status messages */ + if (strstr(text,"status =") != NULL) { + return; + } - /* - we change the text, so we need to make a local copy. A copy - is dynamically allocated only if it does not fit into - myBuffer. - */ - if(strlen(text) > 1023){ - pCopy = (char *)malloc((strlen(text)+2)*sizeof(char)); - if(pCopy == NULL){ - return; - } - memset(pCopy,0,(strlen(text)+2)*sizeof(char)); - strcpy(pCopy,text); - pText = pCopy; - } else { - strcpy(myBuffer,text); - pText = myBuffer; - } + /* suppress TRANSACTIONFINISHED as well in order to make the WWW + commandlog work and TRANSACTIONSTART in order to make the logfiles + shorter + */ + if (strstr(text,"TRANSACTIONSTART") != NULL) { + return; + } + if (strstr(text,"TRANSACTIONFINISHED") != NULL) { + return; + } + + /* we make a local copy, stripping off the newline at the + end. We anyway need a copy later for the circular buffer */ + l = strlen(text); + pPtr = strrchr(text,'\n'); + if (pPtr != NULL && (pPtr[1]=='\0' || pPtr[2] == '\0')) { + l = pPtr - text; + } + pCopy = malloc(l+1); + if (pCopy == NULL) return; + strncpy(pCopy, text, l); + pCopy[l]='\0'; - /* figure out if we have to do a newline with pText as well */ - pPtr = strrchr(pText,'\n'); - if(pPtr != NULL) - { - iPos = pPtr - pText; - if(iPos >= (strlen(pText) - 2) ) - { - iNL = 1; - } - } + if (prompt == cmdPrompt && iCompact) { + pPtr = strstr(pCopy, "fulltransact "); + if (pPtr && pPtr < pCopy+3) { + strippedText = pPtr + 13; + } + pPtr = strstr(pCopy, "transact "); + if (pPtr && pPtr < pCopy+3) { + strippedText = pPtr + 9; + } + } - /* supress status messages */ - if(strstr(pText,"status =") != NULL) - { - if(pCopy != NULL){ - free(pCopy); - } - return; - } - - /* suppress TRANSACTIONFINISHED as well in order to make the WWW - commandlog work and TRANSACTIONSTART in order to make the logfiles - shorter - */ - if(strstr(pText,"TRANSACTIONFINISHED") != NULL || - strstr(pText,"TRANSACTIONSTART") != NULL) - { - if(pCopy != NULL){ - free(pCopy); - } - return; - } + /* create tail buffer as needed */ + if (!pTail) { + pTail = createCircular(MAXTAIL,free); + } - /* create tail buffer as needed */ - if(!pTail) - { - pTail = createCircular(MAXTAIL,free); - } + now = time(NULL); + + doStamp = 0; + doStampId = 0; + + if (id == NOID) { + if (!prompt) { + prompt=""; + } else { + snprintf(buffer, sizeof buffer, "%s ", prompt); + prompt = buffer; + } + } else if (iCompact == 0) { + if (!prompt) { + snprintf(buffer, sizeof buffer, "To sock %d : ", id); + } else { + snprintf(buffer, sizeof buffer, "sock %d>%s ", id, prompt); + } + prompt = buffer; + } else { + if (id != lastId) { + lastId = id; + doStampId = 1; + } + if (!prompt) { + prompt=""; + } else { + snprintf(buffer, sizeof buffer, "%s ", prompt); + prompt = buffer; + } + } + + if (iCompact > 0) { /* write time stamp */ + if (now/iCompact != lastStamp/iCompact) { + doStamp = 1; + doStampId = 1; + } + if (doStampId) { + lastStamp = now; + nowTm = localtime(&now); + strftime(stamp1, sizeof stamp1, "=== %H:%M:%S ===", nowTm); + if (id != NOID) { + snprintf(stamp2, sizeof stamp2, " socket %d ===", id); + } else { + stamp2[0] = '\0'; + } + } + } /* user file */ - if(fd != NULL) - { - if(iNL) - { - fprintf(fd,"%s %s",prompt, pText); - } - else - { - fprintf(fd,"%s %s\n",prompt, pText); - } + if (fd != NULL) { + if (doStampId) { + fprintf(fd,"%s %s\n", stamp1, stamp2); + } + fprintf(fd,"%s%s\n", prompt, pCopy); } + /* automatic file */ - if(fauto != NULL) - { - time(&tLastWrite); - if(iNL) - { - fprintf(fauto,"%s %s",prompt, pText); - } - else - { - fprintf(fauto,"%s %s\n",prompt, pText); - } + if (fauto != NULL) { + tLastWrite = now; + if (doStampId) { + fprintf(fauto,"%s%s\n", stamp1, stamp2); + } + fprintf(fauto,"%s%s\n", prompt, strippedText); } /* to all listening sockets. The check is necessary to resolve a shutdown problem */ - if(pServ->pTasker != NULL) - { - TaskSignal(pServ->pTasker,COMLOG,pText); + if (pServ->pTasker != NULL) { + if (doStamp) { + TaskSignal(pServ->pTasker,COMLOG,stamp1); + } + TaskSignal(pServ->pTasker,COMLOG,pCopy); } /* tail buffer */ - if(pTail != NULL) - { - if(iNL) - { - pPtr = strrchr(pText,'\n'); - *pPtr = ' '; - } - setCircular(pTail,strdup(pText)); - nextCircular(pTail); + if (pTail != NULL) { + if (doStamp) { + setCircular(pTail,strdup(stamp1)); + nextCircular(pTail); + } + setCircular(pTail,pCopy); + nextCircular(pTail); } - if(pCopy != NULL){ - free(pCopy); - } + lastId = id; + } +/*------------------------------------------------------------------------*/ + void WriteToCommandLog(char *prompt, char *text) + { + WriteToCommandLogId(prompt, NOID, text); + } +/*------------------------------------------------------------------------*/ + void WriteToCommandLogCmd(int id, char *text) + { + WriteToCommandLogId(cmdPrompt, id, text); + } +/*------------------------------------------------------------------------*/ + int CompactCommandLog(void) { + return iCompact > 0; } /*------------------------------------------------------------------------*/ static void PrintTail(int iNum, SConnection *pCon) @@ -233,9 +290,9 @@ pInst = FindVariable(pServ->pSics,"instrument"); if(pInst) { - sprintf(pBueffel,"Logfile started at instument %s at %s", + sprintf(pBueffel,"Logfile started at instrument %s at %s", pInst->text,pTime); - WriteToCommandLog("SYS>> ", pBueffel); + WriteToCommandLog("SYS>>", pBueffel); } /* if a file to execute is configured, execute it */ @@ -259,12 +316,6 @@ AutoTask puts a time stamp into the auto log file any hour and creates a new log file any 24 hours */ - static time_t tLogfile = 0; - static time_t tStamp = 0; - static int iEnd = 1; - static int iAutoActive = 0; - static int iIntervall = 60; - static int AutoTask(void *pData) { time_t tNow; @@ -296,7 +347,7 @@ if(tLogfile < 0) tLogfile = tNow + 60*60*24; } - if(tNow > tStamp) + if(tNow > tStamp && iIntervall > 0) { CLFormatTime(pTime,79); WriteToCommandLog("TIMESTAMP>> ",pTime); @@ -319,11 +370,10 @@ fflush(fauto); } - if (fauto && tLastWrite != 0 && tNow > tLastWrite) { + if (fauto && tLastWrite > 0 && tNow > tLastWrite) { fflush(fauto); tLastWrite = 0; - } - + } return iEnd; } /*----------- a command to configure the log --------------------------*/ @@ -450,15 +500,19 @@ return 0; } iIntervall = iVal; - SCSendOK(pCon); - return 1; } - else + SCPrintf(pCon,eValue,"%s.intervall [min] = %d", argv[0], iIntervall); + return 1; + } + else if(strcmp(argv[1],"compact") == 0) + { + if(argc > 2) { - sprintf(pBueffel,"autolog.intervall = %d", iIntervall); - SCWrite(pCon,pBueffel,eValue); - return 1; + iCompact = atoi(argv[2]); + if (iCompact > 0) iIntervall = 0; } + SCPrintf(pCon,eValue,"%s.compact [sec] = %d", argv[0], iCompact); + return 1; } else if(strcmp(argv[1],"close") == 0) /* close command */ { diff --git a/commandlog.h b/commandlog.h index 9e4472d3..704a264a 100644 --- a/commandlog.h +++ b/commandlog.h @@ -10,6 +10,9 @@ #ifndef COMMANDLOG #define COMMANDLOG void WriteToCommandLog(char *prompt,char *pText); + void WriteToCommandLogId(char *prompt, int id, char *text); + void WriteToCommandLogCmd(int id, char *text); + int CompactCommandLog(void); int CommandLog(SConnection *pCon, SicsInterp *pSics, void *pData, int argc, char *argv[]);