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
This commit is contained in:
Douglas Clowes
2014-07-03 15:28:05 +10:00
parent 45a1102b59
commit 0fb4519876
3 changed files with 62 additions and 32 deletions

View File

@ -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;
}
}

View File

@ -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: <<<close logfile>>>\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: <<<close logfile>>> to %s\n",
timestamp(NULL, ':'),
filnam);
fclose(prevlog);
if (1 == iLogUsable) {
fprintf(fLogFile, "%s: <<<open logfile>>> from %s\n",
timestamp(NULL, ':'),
prevfilnam);
iLineCount++;
}
}
if (1 == iLogUsable) {
if (iLineCount == 0)
fprintf(fLogFile, "%s: <<<open logfile>>>\n", timestamp(NULL));
fprintf(fLogFile, "%s:%s:", timestamp(tp), OutCodeToTxt(eOut));
fprintf(fLogFile, "%s: <<<open logfile>>>\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,

View File

@ -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);