- 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,50 +39,30 @@
/*-------------------- 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 */
we change the text, so we need to make a local copy. A copy if (strstr(text,"status =") != NULL) {
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;
}
/* 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;
}
}
/* supress status messages */
if(strstr(pText,"status =") != NULL)
{
if(pCopy != NULL){
free(pCopy);
}
return; return;
} }
@ -85,67 +70,139 @@
commandlog work and TRANSACTIONSTART in order to make the logfiles commandlog work and TRANSACTIONSTART in order to make the logfiles
shorter shorter
*/ */
if(strstr(pText,"TRANSACTIONFINISHED") != NULL || if (strstr(text,"TRANSACTIONSTART") != NULL) {
strstr(pText,"TRANSACTIONSTART") != NULL) return;
{
if(pCopy != NULL){
free(pCopy);
} }
if (strstr(text,"TRANSACTIONFINISHED") != NULL) {
return; 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';
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;
}
}
/* create tail buffer as needed */ /* create tail buffer as needed */
if(!pTail) if (!pTail) {
{
pTail = createCircular(MAXTAIL,free); 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 */ /* 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);
}
else
{
fprintf(fd,"%s %s\n",prompt, pText);
} }
fprintf(fd,"%s%s\n", prompt, pCopy);
} }
/* 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);
}
else
{
fprintf(fauto,"%s %s\n",prompt, pText);
} }
fprintf(fauto,"%s%s\n", prompt, strippedText);
} }
/* 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));
{
pPtr = strrchr(pText,'\n');
*pPtr = ' ';
}
setCircular(pTail,strdup(pText));
nextCircular(pTail); nextCircular(pTail);
} }
if(pCopy != NULL){ setCircular(pTail,pCopy);
free(pCopy); nextCircular(pTail);
} }
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) 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); }
SCPrintf(pCon,eValue,"%s.intervall [min] = %d", argv[0], iIntervall);
return 1; return 1;
} }
else else if(strcmp(argv[1],"compact") == 0)
{ {
sprintf(pBueffel,"autolog.intervall = %d", iIntervall); if(argc > 2)
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 */ 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[]);