- changed logging functionality (CompactCommandLog option)

This commit is contained in:
zolliker
2008-01-18 07:20:09 +00:00
parent 07b3a08784
commit 6b27a73491
2 changed files with 169 additions and 112 deletions

View File

@@ -11,6 +11,11 @@
Added a tail facility Added a tail facility
Mark Koennecke, October 1999 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 <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
@@ -34,118 +39,170 @@
/*-------------------- the tail buffer ---------------------------------*/ /*-------------------- the tail buffer ---------------------------------*/
static pCircular pTail = NULL; static pCircular pTail = NULL;
#define MAXTAIL 1000 #define MAXTAIL 1000
/*----------------------------------------------------------------------*/ #define NOID -1964
static time_t lastStamp = 0;
static time_t iCompact = 0;
static time_t tLastWrite = 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; int l, iPos;
char *pPtr = NULL, *pCopy = NULL, *pText = NULL; char *pPtr = NULL, *pCopy = NULL, *strippedText = text;
char myBuffer[1024]; 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;
}
/* /* suppress TRANSACTIONFINISHED as well in order to make the WWW
we change the text, so we need to make a local copy. A copy commandlog work and TRANSACTIONSTART in order to make the logfiles
is dynamically allocated only if it does not fit into shorter
myBuffer. */
*/ if (strstr(text,"TRANSACTIONSTART") != NULL) {
if(strlen(text) > 1023){ return;
pCopy = (char *)malloc((strlen(text)+2)*sizeof(char)); }
if(pCopy == NULL){ if (strstr(text,"TRANSACTIONFINISHED") != NULL) {
return; return;
} }
memset(pCopy,0,(strlen(text)+2)*sizeof(char));
strcpy(pCopy,text); /* we make a local copy, stripping off the newline at the
pText = pCopy; end. We anyway need a copy later for the circular buffer */
} else { l = strlen(text);
strcpy(myBuffer,text); pPtr = strrchr(text,'\n');
pText = myBuffer; 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 */ if (prompt == cmdPrompt && iCompact) {
pPtr = strrchr(pText,'\n'); pPtr = strstr(pCopy, "fulltransact ");
if(pPtr != NULL) if (pPtr && pPtr < pCopy+3) {
{ strippedText = pPtr + 13;
iPos = pPtr - pText; }
if(iPos >= (strlen(pText) - 2) ) pPtr = strstr(pCopy, "transact ");
{ if (pPtr && pPtr < pCopy+3) {
iNL = 1; strippedText = pPtr + 9;
} }
} }
/* supress status messages */ /* create tail buffer as needed */
if(strstr(pText,"status =") != NULL) if (!pTail) {
{ pTail = createCircular(MAXTAIL,free);
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 */ now = time(NULL);
if(!pTail)
{ doStamp = 0;
pTail = createCircular(MAXTAIL,free); 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 */ /* user file */
if(fd != NULL) if (fd != NULL) {
{ if (doStampId) {
if(iNL) fprintf(fd,"%s %s\n", stamp1, stamp2);
{ }
fprintf(fd,"%s %s",prompt, pText); fprintf(fd,"%s%s\n", prompt, pCopy);
}
else
{
fprintf(fd,"%s %s\n",prompt, pText);
}
} }
/* automatic file */ /* automatic file */
if(fauto != NULL) if (fauto != NULL) {
{ tLastWrite = now;
time(&tLastWrite); if (doStampId) {
if(iNL) fprintf(fauto,"%s%s\n", stamp1, stamp2);
{ }
fprintf(fauto,"%s %s",prompt, pText); fprintf(fauto,"%s%s\n", prompt, strippedText);
}
else
{
fprintf(fauto,"%s %s\n",prompt, pText);
}
} }
/* to all listening sockets. The check is necessary to resolve a shutdown problem */ /* to all listening sockets. The check is necessary to resolve a shutdown problem */
if(pServ->pTasker != NULL) if (pServ->pTasker != NULL) {
{ if (doStamp) {
TaskSignal(pServ->pTasker,COMLOG,pText); TaskSignal(pServ->pTasker,COMLOG,stamp1);
}
TaskSignal(pServ->pTasker,COMLOG,pCopy);
} }
/* tail buffer */ /* tail buffer */
if(pTail != NULL) if (pTail != NULL) {
{ if (doStamp) {
if(iNL) setCircular(pTail,strdup(stamp1));
{ nextCircular(pTail);
pPtr = strrchr(pText,'\n'); }
*pPtr = ' '; setCircular(pTail,pCopy);
} nextCircular(pTail);
setCircular(pTail,strdup(pText));
nextCircular(pTail);
} }
if(pCopy != NULL){ lastId = id;
free(pCopy); }
} /*------------------------------------------------------------------------*/
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) static void PrintTail(int iNum, SConnection *pCon)
@@ -233,9 +290,9 @@
pInst = FindVariable(pServ->pSics,"instrument"); pInst = FindVariable(pServ->pSics,"instrument");
if(pInst) if(pInst)
{ {
sprintf(pBueffel,"Logfile started at instument %s at %s", sprintf(pBueffel,"Logfile started at instrument %s at %s",
pInst->text,pTime); pInst->text,pTime);
WriteToCommandLog("SYS>> ", pBueffel); WriteToCommandLog("SYS>>", pBueffel);
} }
/* if a file to execute is configured, execute it */ /* 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 AutoTask puts a time stamp into the auto log file any hour and
creates a new log file any 24 hours 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) static int AutoTask(void *pData)
{ {
time_t tNow; time_t tNow;
@@ -296,7 +347,7 @@
if(tLogfile < 0) if(tLogfile < 0)
tLogfile = tNow + 60*60*24; tLogfile = tNow + 60*60*24;
} }
if(tNow > tStamp) if(tNow > tStamp && iIntervall > 0)
{ {
CLFormatTime(pTime,79); CLFormatTime(pTime,79);
WriteToCommandLog("TIMESTAMP>> ",pTime); WriteToCommandLog("TIMESTAMP>> ",pTime);
@@ -319,11 +370,10 @@
fflush(fauto); fflush(fauto);
} }
if (fauto && tLastWrite != 0 && tNow > tLastWrite) { if (fauto && tLastWrite > 0 && tNow > tLastWrite) {
fflush(fauto); fflush(fauto);
tLastWrite = 0; tLastWrite = 0;
} }
return iEnd; return iEnd;
} }
/*----------- a command to configure the log --------------------------*/ /*----------- a command to configure the log --------------------------*/
@@ -450,15 +500,19 @@
return 0; return 0;
} }
iIntervall = iVal; 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); iCompact = atoi(argv[2]);
SCWrite(pCon,pBueffel,eValue); if (iCompact > 0) iIntervall = 0;
return 1;
} }
SCPrintf(pCon,eValue,"%s.compact [sec] = %d", argv[0], iCompact);
return 1;
} }
else if(strcmp(argv[1],"close") == 0) /* close command */ else if(strcmp(argv[1],"close") == 0) /* close command */
{ {

View File

@@ -10,6 +10,9 @@
#ifndef COMMANDLOG #ifndef COMMANDLOG
#define COMMANDLOG #define COMMANDLOG
void WriteToCommandLog(char *prompt,char *pText); 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 CommandLog(SConnection *pCon, SicsInterp *pSics, void *pData,
int argc, char *argv[]); int argc, char *argv[]);