From 0fb4519876d284711018942b2339c9e0a71fe311 Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Thu, 3 Jul 2014 15:28:05 +1000 Subject: [PATCH] Changes to logging Add 'L' to 'M', 'N', 'D' log flags (for consistency) Replace colon (':') in logfilenames with minus ('-') Suppress spurious blank lines in logfile from line terminators Add filename to open/close messages for chaining convenience --- conman.c | 19 +++++++++------ servlog.c | 73 +++++++++++++++++++++++++++++++++++++------------------ servlog.h | 2 ++ 3 files changed, 62 insertions(+), 32 deletions(-) diff --git a/conman.c b/conman.c index dfda2404..6afdc5cc 100644 --- a/conman.c +++ b/conman.c @@ -789,17 +789,17 @@ static int isOK(const char *buffer) /*--------------------------------------------------------------------------*/ static void testAndWriteSICSLog(SConnection * self, char *buffer, int iOut) { - char *tp; + char tp; /* We don't want to log the "OK" messages */ if (isOK(buffer)) return; if (SCinMacro(self)) { - tp = "M"; + tp = 'M'; if (sicsdebug()) - tp = "D"; + tp = 'D'; } else - tp = "N"; + tp = 'N'; switch (iOut) { case eStatus: case eStart: @@ -812,22 +812,25 @@ static void testAndWriteSICSLog(SConnection * self, char *buffer, int iOut) case eError: case eWarning: /* Log it */ - SICSLogPrintf(iOut, "%s:%s", tp, buffer); + SICSLogWriteCode(buffer, iOut, tp); return; case eValue: if (sicsdebug() || !SCinMacro(self)) { /* Log it */ - SICSLogPrintf(iOut, "%s:%s", tp, buffer); + SICSLogWriteCode(buffer, iOut, tp); return; } else { /* Suppressed */ - /* SICSLogPrintf(iOut, "%s!:%s", tp, buffer); */ +#if 0 + tp = tolower(tp); + SICSLogWriteCode(buffer, iOut, tp); +#endif return; } break; default: printf("Unrecognized ouput code %d in testAndWriteSICSLog: FIXME!!!\n", iOut); - SICSLogPrintf(iOut, "%s:%s", tp, buffer); + SICSLogWriteCode(buffer, iOut, tp); return; } } diff --git a/servlog.c b/servlog.c index b27bc156..72cf3825 100644 --- a/servlog.c +++ b/servlog.c @@ -239,7 +239,7 @@ static int HasLineFeed(char *pText) } /*---------------------------------------------------------------------------*/ -static const char* timestamp(struct timeval *tp) { +static const char* timestamp(struct timeval *tp, char delim) { static char ts[80]; int year, month, day; int hour, min, sec, usec; @@ -257,8 +257,8 @@ static const char* timestamp(struct timeval *tp) { min = time->tm_min; sec = time->tm_sec; usec = (int) tv.tv_usec; - snprintf(ts, 80, "%04d-%02d-%02dT%02d:%02d:%02d.%06d", - year, month, day, hour, min, sec, usec); + snprintf(ts, 80, "%04d-%02d-%02dT%02d%c%02d%c%02d.%06d", + year, month, day, hour, delim, min, delim, sec, usec); return ts; } @@ -270,12 +270,13 @@ static FILE *fLogFile = NULL; static int iFile = 0; static int iLineCount = 0; static int iLogUsable = 1; +static char filnam[512]; +static char prevfilnam[512]; /*---------------------------------------------------------------------------*/ int OpenVerifyLogFile() { char pFile[256]; - char filnam[512]; char *pChar = NULL; char fPath[1024]; @@ -289,7 +290,11 @@ int OpenVerifyLogFile() strlcpy(pFile, pChar, 255); } - snprintf(filnam, 511, "%s%s_%19.19s.%02d.log", fPath, pFile, timestamp(NULL), iFile); + snprintf(filnam, 511, "%s%s_%19.19s.%02d.log", fPath, pFile, timestamp(NULL, '-'), iFile); + iFile++; + if (iFile >= MAXFILES) { + iFile = 0; + } fLogFile = fopen(filnam, "w"); if (!fLogFile) { @@ -337,26 +342,32 @@ static void SICSLogWriteFile(char *pText, OutCode eOut, struct timeval *tp) return; } iLogUsable = OpenVerifyLogFile(); + iLineCount = 0; } /* switch file if too many lines */ if (iLineCount >= MAXLOG) { - fprintf(fLogFile, "%s: <<>>\n", timestamp(NULL)); - fclose(fLogFile); - fLogFile = NULL; - iFile++; - iLineCount = 0; - if (iFile >= MAXFILES) { - iFile = 0; - } + strcpy(prevfilnam, filnam); + FILE *prevlog = fLogFile; + iLogUsable = OpenVerifyLogFile(); + iLineCount = 0; + fprintf(prevlog, "%s: <<>> to %s\n", + timestamp(NULL, ':'), + filnam); + fclose(prevlog); + if (1 == iLogUsable) { + fprintf(fLogFile, "%s: <<>> from %s\n", + timestamp(NULL, ':'), + prevfilnam); + iLineCount++; + } } if (1 == iLogUsable) { - if (iLineCount == 0) - fprintf(fLogFile, "%s: <<>>\n", timestamp(NULL)); - fprintf(fLogFile, "%s:%s:", timestamp(tp), OutCodeToTxt(eOut)); + fprintf(fLogFile, "%s: <<>>\n", timestamp(NULL, ':')); + fprintf(fLogFile, "%s:%s:", timestamp(tp, ':'), OutCodeToTxt(eOut)); fprintf(fLogFile, "%s", pText); if (text_len < 1 || pText[text_len - 1] != '\n') fprintf(fLogFile, "\n"); @@ -365,10 +376,10 @@ static void SICSLogWriteFile(char *pText, OutCode eOut, struct timeval *tp) } } -void SICSLogWriteTime(char *pText, OutCode eOut, struct timeval *tp) +void SICSLogWriteTimeCode(char *pText, OutCode eOut, struct timeval *tp, char code) { char buf[200]; - const char *cp = pText; + const char *cp; int idx = 0; struct timeval tv; @@ -376,6 +387,11 @@ void SICSLogWriteTime(char *pText, OutCode eOut, struct timeval *tp) gettimeofday(&tv, NULL); tp = &tv; } + if (isalnum(code)) { + buf[idx++] = code; + buf[idx++] = ':'; + } + cp = pText; while (*cp) { if (*cp == '\n') { buf[idx++] = '\n'; @@ -409,6 +425,9 @@ void SICSLogWriteTime(char *pText, OutCode eOut, struct timeval *tp) buf[idx++] = ':'; } } + /* Suppress the spurious blank lines caused by trailing newlines */ + if (idx == 2 && buf[0] == '*' && buf[1] == ':') + return; if (idx > 0) { buf[idx++] = '\n'; buf[idx++] = '\0'; @@ -416,11 +435,19 @@ void SICSLogWriteTime(char *pText, OutCode eOut, struct timeval *tp) } } +void SICSLogWriteCode(char *pText, OutCode eOut, char code) +{ + SICSLogWriteTimeCode(pText, eOut, NULL, code); +} + +void SICSLogWriteTime(char *pText, OutCode eOut, struct timeval *tp) +{ + SICSLogWriteTimeCode(pText, eOut, tp, 'L'); +} + void SICSLogWrite(char *pText, OutCode eOut) { - struct timeval tv; - gettimeofday(&tv, NULL); - SICSLogWriteTime(pText, eOut, &tv); + SICSLogWriteTime(pText, eOut, NULL); } void SICSLogWriteHexTime(const char *text, int count, OutCode eOut, @@ -493,9 +520,7 @@ void SICSLogWriteHexTime(const char *text, int count, OutCode eOut, void SICSLogWriteHex(const char *text, int count, OutCode eOut) { - struct timeval tv; - gettimeofday(&tv, NULL); - SICSLogWriteHexTime(text, count, eOut, &tv); + SICSLogWriteHexTime(text, count, eOut, NULL); } void SICSLogTimePrintf(OutCode eOut, struct timeval *tp, const char *fmt, diff --git a/servlog.h b/servlog.h index 08c147e7..ce5af4fb 100644 --- a/servlog.h +++ b/servlog.h @@ -17,6 +17,8 @@ #include "Scommon.h" void SICSLogWrite(char *ptext, OutCode eOut); void SICSLogWriteTime(char *ptext, OutCode eOut, struct timeval *tp); +void SICSLogWriteCode(char *ptext, OutCode eOut, char code); +void SICSLogWriteTimeCode(char *ptext, OutCode eOut, struct timeval *tp, char code); void SICSLogWriteHex(const char *text, int count, OutCode eOut); void SICSLogWriteHexTime(const char *text, int count, OutCode eOut, struct timeval *tp);