- added FindCommandDescriptor to SCinter.*
- modified support for dynamic objects - improved logger system - various fixes
This commit is contained in:
198
logger.c
198
logger.c
@ -20,21 +20,16 @@ struct Logger {
|
||||
char *old;
|
||||
int oldsize;
|
||||
int period;
|
||||
time_t last;
|
||||
int lastLn; /* 0 or the number chars counted from the second
|
||||
last newline character. */
|
||||
int overwrite; /* overwrite last line when value has not changed */
|
||||
time_t last, lastWrite;
|
||||
int numeric;
|
||||
int exact;
|
||||
double oldFloat, newFloat;
|
||||
time_t newTime;
|
||||
Logger *next;
|
||||
};
|
||||
|
||||
static char *dir = NULL;
|
||||
static Logger *list;
|
||||
static time_t lastLife = 0;
|
||||
|
||||
static time_t lastWritten = 0;
|
||||
/*--------------------------------------------------------------------------*/
|
||||
char *LoggerName(Logger *log) {
|
||||
return log->name;
|
||||
@ -60,45 +55,55 @@ Logger *LoggerFind(const char *name) {
|
||||
/*--------------------------------------------------------------------------*/
|
||||
#define LASTLOGTXT "#last logging entry at:\n"
|
||||
|
||||
void LoggerSetDir(char *dirarg) {
|
||||
time_t LoggerGetLastLife(char *dirarg) {
|
||||
char path[256], line[32];
|
||||
FILE *fil;
|
||||
|
||||
dir = dirarg;
|
||||
snprintf(path, sizeof path, "%s/lastlife.dat", dir);
|
||||
time_t t = 0;
|
||||
|
||||
if (dirarg == NULL) {
|
||||
return lastWritten;
|
||||
}
|
||||
snprintf(path, sizeof path, "%s/lastlife.dat", dirarg);
|
||||
fil = fopen(path, "r");
|
||||
if (fil) {
|
||||
fgets(line, sizeof line, fil);
|
||||
if (strcmp(line, LASTLOGTXT) == 0) {
|
||||
fgets(line, sizeof line, fil);
|
||||
lastLife = atol(line);
|
||||
if (lastLife < 1000000000) {
|
||||
printf("bad lastLife %ld\n", (long)lastLife);
|
||||
t = atol(line);
|
||||
if (t < 1000000000) {
|
||||
printf("bad lastLife %ld\n", (long)t);
|
||||
}
|
||||
}
|
||||
fclose(fil);
|
||||
} else {
|
||||
printf("can not read %s\n", path);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
time_t LoggerSetDir(char *dirarg) {
|
||||
dir = dirarg;
|
||||
lastLife = LoggerGetLastLife(dir);
|
||||
return lastLife;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
char *LoggerGetDir(void) {
|
||||
char path[256];
|
||||
FILE *fil;
|
||||
time_t now;
|
||||
static time_t last;
|
||||
|
||||
now = time(NULL);
|
||||
if (now != last) {
|
||||
lastWritten = time(NULL);
|
||||
if (lastWritten != last) { /* do not write more than once per second */
|
||||
snprintf(path, sizeof path, "%s/lastlife.dat", dir);
|
||||
fil = fopen(path, "w");
|
||||
if (fil) {
|
||||
fprintf(fil, "%s%ld\n", LASTLOGTXT, (long)now);
|
||||
fprintf(fil, "%s%ld\n", LASTLOGTXT, (long)lastWritten);
|
||||
fclose(fil);
|
||||
} else {
|
||||
printf("can not write %s\n", path);
|
||||
}
|
||||
last = now;
|
||||
last = lastWritten;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
@ -122,123 +127,80 @@ int LoggerVarPath(char *dir, char *path, int pathLen, char *name) {
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
int LoggerWrite0(Logger *log, time_t now, int period, char *value) {
|
||||
char path[256], stim[32], buf[32], chr;
|
||||
struct tm *tm;
|
||||
int yday;
|
||||
int isdst;
|
||||
int l, ext, skip, changed;
|
||||
char path[256], stim[32], buf[32];
|
||||
struct tm tm, lasttm;
|
||||
int l, ext, writeInfo;
|
||||
FILE *fil;
|
||||
long endPos, pos1, p;
|
||||
char info[80];
|
||||
time_t beforenow;
|
||||
|
||||
LoggerGetDir();
|
||||
if (dir == NULL) return 0;
|
||||
if (now == 0) {
|
||||
printf("now==0\n");
|
||||
}
|
||||
tm = localtime(&log->last);
|
||||
isdst = tm->tm_isdst; /* last isdst */
|
||||
yday = tm->tm_yday; /* last day */
|
||||
tm = localtime(&now);
|
||||
if (tm->tm_yday != yday) {
|
||||
log->period = -1;
|
||||
}
|
||||
lasttm = *localtime(&log->last);
|
||||
tm = *localtime(&now);
|
||||
l = LoggerVarPath(dir, path, sizeof path, log->name);
|
||||
|
||||
strftime(path + l, sizeof path - l, "%m-%d.log", tm);
|
||||
strftime(stim, sizeof stim, "#%Y-%m-%d %H:%M:%S", tm);
|
||||
strftime(path + l, sizeof path - l, "%m-%d.log", &tm);
|
||||
strftime(stim, sizeof stim, "#%Y-%m-%d %H:%M:%S", &tm);
|
||||
|
||||
fil = fopen(path, "r+");
|
||||
skip = 0;
|
||||
if (period <= 0) period = 1;
|
||||
if (fil == NULL) { /* create new file */
|
||||
fil = fopen(path, "w+");
|
||||
if (fil == NULL) return 0;
|
||||
fprintf(fil, "%s isdst %d period %d exact %d\n", stim, tm->tm_isdst, period, log->exact);
|
||||
log->period = period;
|
||||
isdst = tm->tm_isdst;
|
||||
endPos = ftell(fil);
|
||||
fseek(fil, -1, SEEK_CUR); /* position to newline */
|
||||
} else { /* check if file is from today */
|
||||
fgets(buf, sizeof buf, fil);
|
||||
if (0 != strncmp(buf, stim, 11)) {
|
||||
fclose(fil);
|
||||
fil=fopen(path, "w+"); /* overwrite old logfile */
|
||||
writeInfo = (tm.tm_isdst != lasttm.tm_isdst ||
|
||||
tm.tm_yday != lasttm.tm_yday ||
|
||||
(period != log->period && log->numeric));
|
||||
if (strcmp(value, log->old) != 0 || writeInfo) {
|
||||
|
||||
fil = fopen(path, "r+");
|
||||
if (fil == NULL) { /* create new file */
|
||||
fil = fopen(path, "w+");
|
||||
if (fil == NULL) return 0;
|
||||
fprintf(fil, "%s isdst %d period %d exact %d\n", stim, tm->tm_isdst, period, log->exact);
|
||||
log->period = period;
|
||||
isdst = tm->tm_isdst;
|
||||
endPos = ftell(fil);
|
||||
fseek(fil, -1, SEEK_CUR); /* position to newline */
|
||||
} else {
|
||||
fseek(fil, -log->lastLn, SEEK_END); /* set position to last line */
|
||||
endPos = ftell(fil) + log->lastLn;
|
||||
if (endPos > 0 && log->lastLn == 0) {
|
||||
/* find last newline in newly opened file */
|
||||
fseek(fil, -1, SEEK_CUR);
|
||||
chr = fgetc(fil);
|
||||
while (chr != '\n' && ftell(fil) >= 2) {
|
||||
fseek(fil, -2, SEEK_CUR);
|
||||
chr = fgetc(fil);
|
||||
fprintf(fil, "%s isdst %d period %d exact %d\n", stim, tm.tm_isdst, period, log->exact);
|
||||
} else { /* check if file is from today */
|
||||
fgets(buf, sizeof buf, fil);
|
||||
if (0 != strncmp(buf, stim, 11)) {
|
||||
fclose(fil); /* it was file from an earlier year */
|
||||
fil=fopen(path, "w+"); /* overwrite old logfile */
|
||||
if (fil == NULL) return 0;
|
||||
fprintf(fil, "%s isdst %d period %d exact %d\n", stim, tm.tm_isdst, period, log->exact);
|
||||
} else {
|
||||
fseek(fil, 0, SEEK_END); /* set position to end */
|
||||
if (writeInfo) {
|
||||
fprintf(fil, "#isdst %d period %d exact %d\n", tm.tm_isdst, period, log->exact);
|
||||
}
|
||||
if (log->lastWrite != 0 && now >= log->lastWrite + 2 * period) {
|
||||
/* this is only useful for direct access of the log files */
|
||||
beforenow = now - period;
|
||||
lasttm = *localtime(&beforenow);
|
||||
if (lasttm.tm_yday == tm.tm_yday) {
|
||||
strftime(stim, sizeof stim,"%H:%M:%S", &lasttm);
|
||||
} else {
|
||||
snprintf(stim, sizeof stim, "00:00:00");
|
||||
}
|
||||
fprintf(fil, "%s\t%s\n", stim, log->old);
|
||||
}
|
||||
fseek(fil, -1, SEEK_CUR); /* position to newline */
|
||||
} else if (now != log->last) {
|
||||
skip = 1;
|
||||
}
|
||||
}
|
||||
strftime(stim, sizeof stim,"%H:%M:%S", &tm);
|
||||
fprintf(fil, "%s\t%s\n", stim, value);
|
||||
log->lastWrite = now;
|
||||
fclose(fil);
|
||||
|
||||
}
|
||||
changed = (0 != strcmp(value, log->old));
|
||||
info[0]='\0';
|
||||
if (period != log->period) {
|
||||
log->period = period;
|
||||
if (log->numeric) {
|
||||
changed = 1;
|
||||
snprintf(info, sizeof info, "period %d exact %d ", period, log->exact);
|
||||
}
|
||||
}
|
||||
if (log->overwrite && !changed) {
|
||||
skip = 0;
|
||||
}
|
||||
|
||||
/* go to next newline */
|
||||
chr = fgetc(fil);
|
||||
while (chr != EOF) {
|
||||
if (chr == '\n') {
|
||||
skip--;
|
||||
if (skip < 0) break;
|
||||
}
|
||||
chr = fgetc(fil);
|
||||
}
|
||||
if (chr == EOF) {
|
||||
fputs("\n#no newline found\n", fil);
|
||||
}
|
||||
|
||||
if (tm->tm_isdst != isdst || info[0] != '\0') {
|
||||
fprintf(fil, "#isdst %d %s\n", tm->tm_isdst, info);
|
||||
}
|
||||
pos1 = ftell(fil);
|
||||
strftime(stim, sizeof stim,"%H:%M:%S", tm);
|
||||
fprintf(fil, "%s\t%s\n", stim, value);
|
||||
for (p = ftell(fil); p < endPos; p++) { /* overwrite dirt after last line */
|
||||
fprintf(fil, " ");
|
||||
}
|
||||
endPos = ftell(fil);
|
||||
fclose(fil);
|
||||
log->lastLn = (endPos - pos1) + 1;
|
||||
/* overwrite next time when value has not changed */
|
||||
log->overwrite = ! changed;
|
||||
log->period = period;
|
||||
|
||||
l = strlen(value);
|
||||
if (l >= log->oldsize) {
|
||||
if (l >= log->oldsize) { /* increase log->old size, optimized for linux/i386 */
|
||||
ext = ((l - log->oldsize)/16 + 1) * 16;
|
||||
if (ext < log->oldsize / 4) ext += (log->oldsize / 64) * 16;
|
||||
log->oldsize += ext;
|
||||
free(log->old);
|
||||
log->old = calloc(1, log->oldsize);
|
||||
assert(log->old);
|
||||
assert(l < log->oldsize);
|
||||
}
|
||||
assert(log->old);
|
||||
assert(l < log->oldsize);
|
||||
strcpy(log->old, value);
|
||||
log->old[l] = '\0';
|
||||
assert(log->old[l] == '\0');
|
||||
log->last = now;
|
||||
return 1;
|
||||
}
|
||||
@ -263,6 +225,7 @@ int LoggerWrite(Logger *log, time_t now, int period, char *value) {
|
||||
|
||||
yday = tm->tm_yday;
|
||||
|
||||
/* -- debug logging if dir/debug exists */
|
||||
l = LoggerVarPath(dir, path, sizeof path, "debug");
|
||||
strftime(path + l, sizeof path - l, "%m-%d.log", tm);
|
||||
fil=fopen(path, "a");
|
||||
@ -274,15 +237,16 @@ int LoggerWrite(Logger *log, time_t now, int period, char *value) {
|
||||
/* log old values (forced midnight log) */
|
||||
p = list;
|
||||
while (p != NULL) {
|
||||
if (fil) {
|
||||
|
||||
if (fil) { /* debug logging */
|
||||
strftime(tim, sizeof tim, "last %m-%d %H:%M:%S, ", localtime(&p->last));
|
||||
fputs(tim, fil);
|
||||
fprintf(fil, "period %d, name %s, old %s\n", p->period, p->name, p->old);
|
||||
}
|
||||
|
||||
if (p->last < h0 && p->last != 0 &&
|
||||
p->period >= 0 && p->old[0] != '\0') {
|
||||
LoggerWrite0(p, h0, p->period, p->old);
|
||||
p->overwrite = 0;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
@ -302,6 +266,10 @@ int LoggerPeriod(Logger *log) {
|
||||
return log->period;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
void LoggerSetPeriod(Logger *log, int period) {
|
||||
LoggerWrite0(log, time(NULL), period, log->old);
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
void LoggerKill(Logger *log) {
|
||||
/* we do not really free the logger, it might be reused
|
||||
for the same variable later. We set the value to undefined */
|
||||
@ -344,7 +312,7 @@ Logger *LoggerMake(char *name, int period, int exact) {
|
||||
log->old = calloc(1,12);
|
||||
log->oldsize = 12;
|
||||
log->last = 0;
|
||||
log->lastLn = 0;
|
||||
log->lastWrite = 0;
|
||||
log->numeric = 1;
|
||||
log->next = list;
|
||||
list = log;
|
||||
|
Reference in New Issue
Block a user