new tecs version M.Z.08.2001
This commit is contained in:
@@ -1,45 +1,35 @@
|
||||
/* switch off ANSI_C_SOURCE for VMS-extended fopen, even when compiling with /ANSI */
|
||||
#ifdef __VMS
|
||||
#ifdef __HIDE_FORBIDDEN_NAMES
|
||||
#undef __HIDE_FORBIDDEN_NAMES
|
||||
#include <stdio.h>
|
||||
#define __HIDE_FORBIDDEN_NAMES
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/timeb.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <assert.h>
|
||||
#include "coc_logfile.h"
|
||||
#include "err_handling.h"
|
||||
#include "str_util.h"
|
||||
#include "myc_time.h"
|
||||
#include "myc_err.h"
|
||||
#include "myc_str.h"
|
||||
|
||||
static FILE *fil=NULL;
|
||||
static char lnam[256]="", filnam[256]="";
|
||||
static char lnam[224]="", filnam[256]="";
|
||||
static char ebuf[20000]="";
|
||||
static char *statusBuf=NULL;
|
||||
static int statusSize;
|
||||
static char *eptr=&ebuf[0];
|
||||
static int lastStamp=0;
|
||||
static int notDated=0;
|
||||
static int dated=0;
|
||||
static int logMask=0;
|
||||
static int wrtMask=0;
|
||||
static int newLine=1;
|
||||
static int logfileStd;
|
||||
static int dirty, writeAll;
|
||||
static int lastline=1;
|
||||
static long int lastpos=0;
|
||||
static int lastStamp=0;
|
||||
static int openTime;
|
||||
static int openDate;
|
||||
|
||||
int ftime (struct timeb *__timeptr); /* for some reason not defined in timeb.h with flag -std1 */
|
||||
void (*logfileOutRtn)(int, char *)=NULL;
|
||||
|
||||
void logfileOpen(int first) {
|
||||
struct tm *tim;
|
||||
struct timeb btim;
|
||||
void logfileOpen(int append) {
|
||||
int year;
|
||||
|
||||
if (logfileStd) {
|
||||
fil=stdout;
|
||||
@@ -47,35 +37,35 @@ void logfileOpen(int first) {
|
||||
return;
|
||||
}
|
||||
assert(fil==NULL);
|
||||
if (first) {
|
||||
ftime(&btim);
|
||||
tim=localtime(&btim.time);
|
||||
if (notDated) {
|
||||
if (lnam[0]=='\0') {
|
||||
str_copy(filnam, "test.log");
|
||||
} else {
|
||||
str_copy(filnam, lnam);
|
||||
str_append(filnam, ".log");
|
||||
}
|
||||
openTime = mycNow();
|
||||
openDate = mycDate(openTime); /* date in yyyymmdd decimal encoding */
|
||||
openTime = openTime % (24*3600); /* seconds since midnight */
|
||||
if (dated) {
|
||||
sprintf(filnam, "%s%02d-%02d.log", lnam, openDate % 10000 / 100, openDate % 100);
|
||||
} else {
|
||||
if (lnam[0]=='\0') {
|
||||
str_copy(filnam, "test.log");
|
||||
} else {
|
||||
sprintf(filnam, "%s%04d-%02d-%02d.log", lnam, tim->tm_year+1900, tim->tm_mon+1, tim->tm_mday);
|
||||
str_copy(filnam, lnam);
|
||||
str_append(filnam, ".log");
|
||||
}
|
||||
}
|
||||
if (dated) {
|
||||
fil=fopen(filnam, "a+");
|
||||
if (fil != NULL) {
|
||||
fseek(fil, 0, SEEK_SET); /* set position to start */
|
||||
year=0;
|
||||
fscanf(fil, "%4d", &year);
|
||||
if (year != openDate / 10000) {
|
||||
fclose(fil);
|
||||
fil=fopen(filnam, "w+"); /* overwrite old logfile */
|
||||
} else {
|
||||
fseek(fil, 0, SEEK_END); /* set position to end */
|
||||
}
|
||||
}
|
||||
} else {
|
||||
assert(filnam[0]!='\0');
|
||||
fil=fopen(filnam, "w+"); /* overwrite last logfile */
|
||||
}
|
||||
#ifdef __VMS
|
||||
if (first && notDated) {
|
||||
fil=fopen(filnam, "w", "SHR=UPD"); /* new version at restart */
|
||||
} else {
|
||||
fil=fopen(filnam, "a", "SHR=UPD");
|
||||
}
|
||||
#else
|
||||
if (first && notDated) {
|
||||
fil=fopen(filnam, "w"); /* overwrite at restart */
|
||||
} else {
|
||||
fil=fopen(filnam, "a");
|
||||
}
|
||||
#endif
|
||||
if (fil==NULL) {
|
||||
printf("Can not open %s\n", filnam);
|
||||
fil=stdout;
|
||||
@@ -83,10 +73,10 @@ void logfileOpen(int first) {
|
||||
return;
|
||||
}
|
||||
ErrSetOutFile(fil);
|
||||
if (first) {
|
||||
fprintf(fil, "%04d-%02d-%02d opened logfile\n"
|
||||
, tim->tm_year+1900, tim->tm_mon+1, tim->tm_mday);
|
||||
}
|
||||
fprintf(fil, "%04d-%02d-%02d %02d:%02d:%02d opened logfile\n"
|
||||
, openDate / 10000, openDate % 10000 / 100, openDate % 100
|
||||
, openTime / 3600, openTime / 60 % 60, openTime % 60);
|
||||
lastStamp = openTime / 60;
|
||||
}
|
||||
|
||||
void logfileStatusBuffer(char *buffer, int bufsize) {
|
||||
@@ -96,22 +86,20 @@ void logfileStatusBuffer(char *buffer, int bufsize) {
|
||||
|
||||
char *logfileInit(char *path, int nodate, int use_stdout, int write_all) {
|
||||
str_copy(lnam, path);
|
||||
lastStamp=-2;
|
||||
logfileStd=use_stdout;
|
||||
writeAll=write_all;
|
||||
notDated=nodate;
|
||||
dated=!nodate;
|
||||
logfileOpen(1);
|
||||
return(filnam);
|
||||
}
|
||||
|
||||
void logfileOut(int mask, const char *fmt, ...)
|
||||
{ va_list ap;
|
||||
char buf[256], *p;
|
||||
char buf[8192], *p;
|
||||
|
||||
assert(mask>0 && mask<32);
|
||||
va_start(ap, fmt);
|
||||
|
||||
if (mask & LOG_MAIN && statusBuf!=NULL) {
|
||||
if (mask & LOG_STAT && statusBuf!=NULL) {
|
||||
if (newLine) {
|
||||
statusBuf[0]='\0';
|
||||
newLine=0;
|
||||
@@ -119,17 +107,22 @@ void logfileOut(int mask, const char *fmt, ...)
|
||||
buf[statusSize]='\0';
|
||||
vsprintf(buf, fmt, ap);
|
||||
assert(buf[statusSize]=='\0'); /* check for overflow */
|
||||
str_ncat(statusBuf, buf, statusSize);
|
||||
if (buf[0]=='@') {
|
||||
str_ncat(statusBuf, buf+1, statusSize);
|
||||
} else {
|
||||
str_ncat(statusBuf, buf, statusSize);
|
||||
}
|
||||
p=strchr(statusBuf, '\n');
|
||||
if (p!=NULL) {
|
||||
newLine=1;
|
||||
*p='\0';
|
||||
}
|
||||
}
|
||||
if (logfileOutRtn!=NULL) {
|
||||
vsprintf(buf, fmt, ap); /* does not check for overflow! */
|
||||
logfileOutRtn(mask, buf);
|
||||
}
|
||||
if (writeAll) {
|
||||
#ifdef __VMS
|
||||
if (fil==NULL) logfileOpen(0);
|
||||
#endif
|
||||
vfprintf(fil, fmt, ap);
|
||||
wrtMask=LOG_ALL;
|
||||
} else {
|
||||
@@ -139,10 +132,14 @@ void logfileOut(int mask, const char *fmt, ...)
|
||||
eptr=NULL;
|
||||
} else {
|
||||
vsprintf(eptr, fmt, ap);
|
||||
assert(NULL==strchr(eptr, '\1'));
|
||||
eptr+=strlen(eptr);
|
||||
p=strchr(eptr, '\1');
|
||||
if (p==NULL) {
|
||||
eptr+=strlen(eptr);
|
||||
} else {
|
||||
eptr=p; /* is in fact an error */
|
||||
}
|
||||
eptr[0]='\1'; /* put \1 as separator between blocks */
|
||||
eptr[1]=mask;
|
||||
eptr[1]=mask & 31;
|
||||
eptr[2]='\0';
|
||||
eptr+=2;
|
||||
}
|
||||
@@ -157,33 +154,35 @@ void logfileMask(int mask) {
|
||||
}
|
||||
|
||||
void logfileStamp(char *text) {
|
||||
struct tm *tim;
|
||||
struct timeb btim;
|
||||
int stamp;
|
||||
int time, date, stamp;
|
||||
|
||||
ftime(&btim);
|
||||
tim=localtime(&btim.time);
|
||||
stamp=tim->tm_hour*60+tim->tm_min;
|
||||
if (stamp<lastStamp) { /* time smaller than last time -> new day -> new logfile */
|
||||
time = mycNow();
|
||||
date = mycDate(time); /* date in yyyymmdd decimal encoding */
|
||||
time = time % (24*3600); /* seconds since midnight */
|
||||
|
||||
stamp=time / 60;
|
||||
if (date != openDate ) { /* day has changed -> new logfile */
|
||||
if (fil!=NULL) { fclose(fil); fil=NULL; }
|
||||
lastpos=0;
|
||||
lastline=1;
|
||||
logfileOpen(1);
|
||||
lastStamp=-2;
|
||||
}
|
||||
if (stamp>lastStamp+1) {
|
||||
#ifdef __VMS
|
||||
if (fil==NULL) logfileOpen(0);
|
||||
#endif
|
||||
lastStamp=stamp;
|
||||
fprintf(fil, "%02d:%02d:%02d --- %s", tim->tm_hour, tim->tm_min, tim->tm_sec, text);
|
||||
dirty=0;
|
||||
if (text==NULL) {
|
||||
if (stamp>lastStamp+1) {
|
||||
fprintf(fil, "---\t%02d:%02d:%02d\n", stamp / 60, stamp % 60, time % 60, text);
|
||||
}
|
||||
} else {
|
||||
fprintf(fil, "\t%02d:%02d:%02d %s", stamp / 60, stamp % 60, time % 60, text);
|
||||
}
|
||||
dirty=0;
|
||||
lastStamp=stamp;
|
||||
}
|
||||
|
||||
void logfileWrite0(int mask) {
|
||||
char *s, *next;
|
||||
|
||||
logMask=logMask | mask;
|
||||
if (dirty) logfileStamp("\n"); /* there was something written since last time */
|
||||
if (dirty) logfileStamp(NULL); /* there was something written since last time */
|
||||
|
||||
s=ebuf;
|
||||
if (writeAll || *s!='\0' && wrtMask & logMask) {
|
||||
@@ -193,10 +192,9 @@ void logfileWrite0(int mask) {
|
||||
next++;
|
||||
if (*next & logMask) {
|
||||
if (*s=='@') { /* write out time */
|
||||
lastStamp-=2; /* force output */
|
||||
logfileStamp(s+1);
|
||||
} else {
|
||||
logfileStamp("\n"); /* write stamp before write something */
|
||||
logfileStamp(NULL); /* write stamp before write something */
|
||||
fprintf(fil, "%s", s);
|
||||
dirty=1;
|
||||
}
|
||||
@@ -211,71 +209,131 @@ void logfileWrite0(int mask) {
|
||||
wrtMask=0;
|
||||
}
|
||||
|
||||
void logfileScan(int date, void (*scanLine)(void*, char*), void *arg) {
|
||||
FILE *sFile;
|
||||
char buf[256], *res;
|
||||
|
||||
if (date == openDate) {
|
||||
sFile=fil;
|
||||
rewind(sFile);
|
||||
} else {
|
||||
sprintf(filnam, "%s%02d-%02d.log", lnam, date % 10000 / 100, date % 100);
|
||||
sFile=fopen(filnam, "r+");
|
||||
if (sFile==NULL) return;
|
||||
}
|
||||
res=fgets(buf, sizeof(buf), sFile);
|
||||
while (res!=NULL) {
|
||||
if (res[0]=='\t') {
|
||||
scanLine(arg, res+1);
|
||||
}
|
||||
res=fgets(buf, sizeof(buf), sFile);
|
||||
}
|
||||
if (sFile==fil) {
|
||||
fseek(fil, 0, SEEK_END);
|
||||
} else {
|
||||
fclose(sFile);
|
||||
}
|
||||
}
|
||||
|
||||
void logfileWrite(int mask) {
|
||||
|
||||
#ifdef __VMS
|
||||
if (fil==NULL) logfileOpen(0);
|
||||
#endif
|
||||
logfileWrite0(mask);
|
||||
#ifdef __VMS
|
||||
if (!logfileStd) { fclose(fil); fil=NULL; }
|
||||
#else
|
||||
fflush(fil);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
void logfileShowErr(char *text)
|
||||
{
|
||||
#ifdef __VMS
|
||||
if (fil==NULL) logfileOpen(0);
|
||||
#endif
|
||||
logfileWrite0(LOG_ALL); /* write all */
|
||||
ErrShow(text);
|
||||
#ifdef __VMS
|
||||
if (!logfileStd) { fclose(fil); fil=NULL; }
|
||||
#else
|
||||
fflush(fil);
|
||||
#endif
|
||||
}
|
||||
|
||||
void logfileClose()
|
||||
{
|
||||
#ifdef __VMS
|
||||
if (fil==NULL) logfileOpen(0);
|
||||
#endif
|
||||
logfileWrite0(LOG_MAIN+LOG_INFO);
|
||||
lastStamp=-2;
|
||||
logfileStamp("\n");
|
||||
logfileOut(LOG_MAIN, "closed\n");
|
||||
logfileStamp(NULL);
|
||||
if (fil!=NULL) { fclose(fil); fil=NULL; }
|
||||
filnam[0]='\0';
|
||||
}
|
||||
|
||||
void logfileOutBuf(int mask, Str_Buf *buf)
|
||||
void logfileOutBuf(int mask, StrBuf *buf)
|
||||
{ char str[256];
|
||||
int rd0, sep;
|
||||
int rd0;
|
||||
char *ret;
|
||||
|
||||
rd0=buf->rdpos;
|
||||
if (buf->rdpos < 0 || buf->rdpos >= buf->dsize
|
||||
|| buf->wrpos < 0 || buf->wrpos >= buf->dsize) {
|
||||
logfileOut(mask, "<buffer corrupt>"); return;
|
||||
}
|
||||
sep=0;
|
||||
while (buf->rdpos < buf->wrpos) {
|
||||
str_get_str(buf, str);
|
||||
if (buf->sep=='\0') {
|
||||
if (sep) logfileOut(mask, " ");
|
||||
if (*str=='\0') {
|
||||
logfileOut(mask, ".");
|
||||
} else {
|
||||
logfileOut(mask, "%s", str);
|
||||
}
|
||||
sep=1;
|
||||
while (!StrEnd(buf)) {
|
||||
ret=StrGet(buf, str, '\0');
|
||||
if (ret==NULL) {
|
||||
ret="...";
|
||||
buf->rdpos=buf->wrpos;
|
||||
}
|
||||
if (ret[0]<32 && ret[0]>0) {
|
||||
logfileOut(mask, "[%d]%s;", ret[0], ret+1);
|
||||
} else {
|
||||
if (sep) logfileOut(mask, "%c", buf->sep);
|
||||
logfileOut(mask, "%s", str);
|
||||
sep=1;
|
||||
logfileOut(mask, "%s;", ret);
|
||||
}
|
||||
}
|
||||
buf->rdpos=rd0;
|
||||
}
|
||||
|
||||
int logfileLength(void) {
|
||||
char lin[256];
|
||||
int l, ll;
|
||||
|
||||
if (logfileStd || fil==NULL) return 0;
|
||||
fseek(fil, lastpos, SEEK_SET);
|
||||
ll=lastpos;
|
||||
l=lastline;
|
||||
while (NULL!=fgets(lin, sizeof(lin), fil)) {
|
||||
lastpos=ll;
|
||||
ll=ftell(fil);
|
||||
lastline=l;
|
||||
l++;
|
||||
}
|
||||
return(lastline);
|
||||
}
|
||||
|
||||
long int logfilePos(int n) {
|
||||
/* set file to the start of n-th last line n, if n<0 then position to the -n-th line */
|
||||
int i;
|
||||
char lin[256];
|
||||
|
||||
if (logfileStd || fil==NULL) return 0;
|
||||
if (n>0) {
|
||||
n=logfileLength()-n+1;
|
||||
} else {
|
||||
n=-n;
|
||||
}
|
||||
fseek(fil, 0, SEEK_SET);
|
||||
for (i=1; i<n; i++) {
|
||||
if (NULL==fgets(lin, sizeof(lin), fil)) return ftell(fil);
|
||||
}
|
||||
return ftell(fil);
|
||||
}
|
||||
|
||||
|
||||
long int logfileGetLines(long int pos, int linmax, char *line, int len_line) {
|
||||
int l,i;
|
||||
if (logfileStd || fil==NULL) {
|
||||
line[0]='\0';
|
||||
return 0;
|
||||
}
|
||||
l=0;
|
||||
fseek(fil, pos, SEEK_SET);
|
||||
for (i=0; i<linmax; i++) {
|
||||
if (NULL==fgets(line+l, len_line-l, fil)) {
|
||||
line[l]='\0';
|
||||
return ftell(fil);
|
||||
}
|
||||
l=strlen(line);
|
||||
}
|
||||
pos=ftell(fil);
|
||||
fseek(fil, 0, SEEK_END);
|
||||
return pos;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user