- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
287
logreader.c
287
logreader.c
@ -26,10 +26,10 @@ typedef struct {
|
||||
int exact;
|
||||
CompType type;
|
||||
time_t step;
|
||||
time_t tlim; /* 0: initial state */
|
||||
Point best; /* best point, to be written if tlim > 0 */
|
||||
Point written; /* last written point */
|
||||
Point last; /* last point */
|
||||
time_t tlim; /* 0: initial state */
|
||||
Point best; /* best point, to be written if tlim > 0 */
|
||||
Point written; /* last written point */
|
||||
Point last; /* last point */
|
||||
char buf[LLEN];
|
||||
int np;
|
||||
char *none;
|
||||
@ -38,62 +38,70 @@ typedef struct {
|
||||
int omitEqual;
|
||||
} Compressor;
|
||||
|
||||
static char *dirs[MAX_DIRS] = {NULL};
|
||||
static int ndirs=0;
|
||||
static char *dirs[MAX_DIRS] = { NULL };
|
||||
static int ndirs = 0;
|
||||
|
||||
static void InitCompressor(Compressor *c, SConnection *pCon, time_t step) {
|
||||
static void InitCompressor(Compressor * c, SConnection * pCon, time_t step)
|
||||
{
|
||||
c->pCon = pCon;
|
||||
c->step = step;
|
||||
c->tlim = 0;
|
||||
c->last.y = LOGGER_NAN;
|
||||
c->last.t = 0;
|
||||
c->buf[0]='\0';
|
||||
c->buf[0] = '\0';
|
||||
c->written.t = 0;
|
||||
c->written.y = LOGGER_NAN;
|
||||
c->omitEqual = 1;
|
||||
}
|
||||
|
||||
static void OutFloat(Compressor *c, Point p) {
|
||||
static void OutFloat(Compressor * c, Point p)
|
||||
{
|
||||
char *value;
|
||||
|
||||
|
||||
if (p.y == LOGGER_NAN) {
|
||||
if (c->omitEqual && c->written.y == LOGGER_NAN) return;
|
||||
if (c->omitEqual && c->written.y == LOGGER_NAN)
|
||||
return;
|
||||
if (c->none) {
|
||||
value = c->none;
|
||||
} else {
|
||||
value = "";
|
||||
}
|
||||
} else {
|
||||
if (c->omitEqual && c->written.y == p.y) return;
|
||||
if (c->omitEqual && c->written.y == p.y)
|
||||
return;
|
||||
snprintf(c->buf, sizeof(c->buf), "%g", p.y);
|
||||
value = c->buf;
|
||||
}
|
||||
SCPrintf(c->pCon, eWarning, "%ld %s", (long)(p.t - c->written.t), value);
|
||||
SCPrintf(c->pCon, eWarning, "%ld %s", (long) (p.t - c->written.t),
|
||||
value);
|
||||
c->written = p;
|
||||
c->np--;
|
||||
}
|
||||
|
||||
static void WriteHeader(Compressor *c) {
|
||||
static void WriteHeader(Compressor * c)
|
||||
{
|
||||
if (c->header) {
|
||||
SCPrintf(c->pCon, eWarning, "*%s period %ld\n", c->header, c->period);
|
||||
c->header = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void PutValue(Compressor *c, time_t t, char *value) {
|
||||
static void PutValue(Compressor * c, time_t t, char *value)
|
||||
{
|
||||
char *p;
|
||||
Point new;
|
||||
|
||||
|
||||
WriteHeader(c);
|
||||
if (c->type == NUMERIC) {
|
||||
new.y = strtod(value, &p);
|
||||
if (p == value) {
|
||||
new.y = LOGGER_NAN;
|
||||
} else {
|
||||
if (new.y == LOGGER_NAN) new.y *= 1.0000002;
|
||||
if (new.y == LOGGER_NAN)
|
||||
new.y *= 1.0000002;
|
||||
}
|
||||
new.t = t;
|
||||
if (t >= c->tlim) { /* a new interval starts */
|
||||
if (t >= c->tlim) { /* a new interval starts */
|
||||
if (c->tlim == 0) {
|
||||
c->tlim = t;
|
||||
} else if (c->best.y != c->written.y) {
|
||||
@ -107,29 +115,31 @@ static void PutValue(Compressor *c, time_t t, char *value) {
|
||||
}
|
||||
c->tlim = t + c->step;
|
||||
}
|
||||
} else { /* not the first point */
|
||||
} else { /* not the first point */
|
||||
if (fabs(new.y - c->best.y) > fabs(c->written.y - c->best.y)) {
|
||||
c->best = new;
|
||||
}
|
||||
}
|
||||
c->last = new;
|
||||
} else if (c->type == TEXT) {
|
||||
if (0 != strncmp(value, c->buf, sizeof(c->buf)-1)) {
|
||||
if (0 != strncmp(value, c->buf, sizeof(c->buf) - 1)) {
|
||||
snprintf(c->buf, sizeof(c->buf), "%s", value);
|
||||
SCPrintf(c->pCon, eWarning, "%ld %s\n", (long)(t - c->written.t), value);
|
||||
SCPrintf(c->pCon, eWarning, "%ld %s\n", (long) (t - c->written.t),
|
||||
value);
|
||||
c->written.t = t;
|
||||
c->np--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void PutFinish(Compressor *c, time_t now) {
|
||||
static void PutFinish(Compressor * c, time_t now)
|
||||
{
|
||||
char value[32];
|
||||
|
||||
if (c->tlim != 0) { /* there is data already */
|
||||
|
||||
if (c->tlim != 0) { /* there is data already */
|
||||
c->omitEqual = 0;
|
||||
if (c->type == NUMERIC) {
|
||||
if (now > c->last.t + c->period) { /* copy last value to the actual time */
|
||||
if (now > c->last.t + c->period) { /* copy last value to the actual time */
|
||||
if (c->last.y != LOGGER_NAN) {
|
||||
snprintf(value, sizeof value, "%g", c->last.y);
|
||||
PutValue(c, now, value);
|
||||
@ -138,16 +148,19 @@ static void PutFinish(Compressor *c, time_t now) {
|
||||
OutFloat(c, c->best); /* write last buffered value */
|
||||
}
|
||||
}
|
||||
} if (now > c->last.t) {
|
||||
}
|
||||
if (now > c->last.t) {
|
||||
PutValue(c, now, c->buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static long getOpt(char *line, int *isdst, int *exact) {
|
||||
static long getOpt(char *line, int *isdst, int *exact)
|
||||
{
|
||||
long lxs;
|
||||
char *opt;
|
||||
|
||||
|
||||
opt = strstr(line, "isdst");
|
||||
if (opt) {
|
||||
sscanf(opt, "isdst %d", isdst);
|
||||
@ -164,67 +177,69 @@ static long getOpt(char *line, int *isdst, int *exact) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]) {
|
||||
static int LogReader(SConnection * pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
/* Usage:
|
||||
graph <start time> <end time> [ none <none value> ] np <number of points> <variable> [<variable> ...]
|
||||
graph <start time> <end time> text <variable>
|
||||
graph <start time> <end time> <step> <variable> [<variable> ...]
|
||||
|
||||
<start time> and <end time> are seconds since epoch (unix time) or, if the value
|
||||
is below one day, a time relative to the actual time
|
||||
|
||||
The <none value> is optional. if not given, unknown values are returned as empty strings
|
||||
|
||||
<number of points> is the maximal number of points to be returned. If more values
|
||||
are present and the values are numeric, the data is reduced. If the data is not
|
||||
numeric, the last values may be skipped in order to avoid overflow.
|
||||
|
||||
<variable> is the name of a variable (several vaiables may be given).
|
||||
Note that slashes are converted to dots, and that the first slash is ignored.
|
||||
|
||||
The seconds variant is for text values, which can not be reduced. In any case, all values
|
||||
are returned.
|
||||
|
||||
The third variant is old style and can be replaced by the first variant, where
|
||||
<number of points> = (<start time> - <end time>) / <step> + 2
|
||||
|
||||
|
||||
graph <start time> <end time> [ none <none value> ] np <number of points> <variable> [<variable> ...]
|
||||
graph <start time> <end time> text <variable>
|
||||
graph <start time> <end time> <step> <variable> [<variable> ...]
|
||||
|
||||
<start time> and <end time> are seconds since epoch (unix time) or, if the value
|
||||
is below one day, a time relative to the actual time
|
||||
|
||||
The <none value> is optional. if not given, unknown values are returned as empty strings
|
||||
|
||||
<number of points> is the maximal number of points to be returned. If more values
|
||||
are present and the values are numeric, the data is reduced. If the data is not
|
||||
numeric, the last values may be skipped in order to avoid overflow.
|
||||
|
||||
<variable> is the name of a variable (several vaiables may be given).
|
||||
Note that slashes are converted to dots, and that the first slash is ignored.
|
||||
|
||||
The seconds variant is for text values, which can not be reduced. In any case, all values
|
||||
are returned.
|
||||
|
||||
The third variant is old style and can be replaced by the first variant, where
|
||||
<number of points> = (<start time> - <end time>) / <step> + 2
|
||||
|
||||
|
||||
Output format:
|
||||
First line: returning the actual time on the server (this time is used for relative times)
|
||||
|
||||
For each variable which has data in the given interval,
|
||||
the variable name is returned preceeded by a '*', followed by some infos* separated with
|
||||
blanks.
|
||||
After the header line for every data point a line follows with
|
||||
a time relative to the last point and the value.
|
||||
The first time value relative to zero, e.g. absolute.
|
||||
The data value is valid until the next datapoint. Empty values are returned as an
|
||||
empty string or as the <none value>.
|
||||
|
||||
At the very end *<loss> <overflow> is returned.
|
||||
<loss> is 1, when the data had to be reduced, 0 else
|
||||
<overflow> is 1, when overflow occured, 0 else. Overflow may happen only
|
||||
when np is given and a text variable was demanded.
|
||||
|
||||
*actually only one info exists: period <value>. This is the update rate in seconds.
|
||||
As equal values are not transmitted, two points (t1,y1) and (t2,y2) with a distance
|
||||
(t2 - t1) > period should not be connected directly. The plot software should generate
|
||||
an intermediate point at (t2-period,y1).
|
||||
|
||||
*/
|
||||
First line: returning the actual time on the server (this time is used for relative times)
|
||||
|
||||
For each variable which has data in the given interval,
|
||||
the variable name is returned preceeded by a '*', followed by some infos* separated with
|
||||
blanks.
|
||||
After the header line for every data point a line follows with
|
||||
a time relative to the last point and the value.
|
||||
The first time value relative to zero, e.g. absolute.
|
||||
The data value is valid until the next datapoint. Empty values are returned as an
|
||||
empty string or as the <none value>.
|
||||
|
||||
At the very end *<loss> <overflow> is returned.
|
||||
<loss> is 1, when the data had to be reduced, 0 else
|
||||
<overflow> is 1, when overflow occured, 0 else. Overflow may happen only
|
||||
when np is given and a text variable was demanded.
|
||||
|
||||
*actually only one info exists: period <value>. This is the update rate in seconds.
|
||||
As equal values are not transmitted, two points (t1,y1) and (t2,y2) with a distance
|
||||
(t2 - t1) > period should not be connected directly. The plot software should generate
|
||||
an intermediate point at (t2-period,y1).
|
||||
|
||||
*/
|
||||
time_t from, to, step, xs, lastt;
|
||||
char *p, *varp;
|
||||
int i, j, iarg, pathLen, iret, loss, np;
|
||||
int inRange;
|
||||
int yday=0;
|
||||
int yday = 0;
|
||||
time_t t, startim;
|
||||
struct tm tm;
|
||||
char stim[32], path[LLEN], line[LLEN], lastval[LLEN];
|
||||
char *lin, *val, *stp;
|
||||
FILE *fil;
|
||||
Compressor c={0};
|
||||
Compressor c = { 0 };
|
||||
float yy, lasty;
|
||||
CompType type0;
|
||||
DIR *dr;
|
||||
@ -237,15 +252,18 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
char *dirPath;
|
||||
int idir;
|
||||
char *colon;
|
||||
|
||||
|
||||
/* argtolower(argc, argv); */
|
||||
if (argc < 4) goto illarg;
|
||||
if (argc < 4)
|
||||
goto illarg;
|
||||
now = time(NULL);
|
||||
from = strtol(argv[1], &p, 0); /* unix time, not year 2038 safe */
|
||||
if (p == argv[1]) goto illarg;
|
||||
from = strtol(argv[1], &p, 0); /* unix time, not year 2038 safe */
|
||||
if (p == argv[1])
|
||||
goto illarg;
|
||||
to = strtol(argv[2], &p, 0);
|
||||
if (p == argv[2]) goto illarg;
|
||||
if (from < ONE_YEAR) {
|
||||
if (p == argv[2])
|
||||
goto illarg;
|
||||
if (from < ONE_YEAR) {
|
||||
from += now;
|
||||
}
|
||||
if (to < ONE_YEAR) {
|
||||
@ -253,24 +271,28 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
}
|
||||
iarg = 3;
|
||||
while (1) {
|
||||
if (iarg>=argc) goto illarg;
|
||||
if (strcasecmp(argv[iarg],"text") == 0) { /* non-numeric values */
|
||||
if (iarg >= argc)
|
||||
goto illarg;
|
||||
if (strcasecmp(argv[iarg], "text") == 0) { /* non-numeric values */
|
||||
iarg++;
|
||||
step = 1;
|
||||
type0 = TEXT;
|
||||
np = to - from + 2;
|
||||
break;
|
||||
} else if (strcasecmp(argv[iarg],"none") == 0) { /* none */
|
||||
} else if (strcasecmp(argv[iarg], "none") == 0) { /* none */
|
||||
iarg++;
|
||||
if (iarg >= argc) goto illarg;
|
||||
if (iarg >= argc)
|
||||
goto illarg;
|
||||
c.none = argv[iarg];
|
||||
iarg++;
|
||||
} else if (strcasecmp(argv[iarg],"np") == 0) { /* max. number of points */
|
||||
} else if (strcasecmp(argv[iarg], "np") == 0) { /* max. number of points */
|
||||
iarg++;
|
||||
if (iarg >= argc) goto illarg;
|
||||
if (iarg >= argc)
|
||||
goto illarg;
|
||||
type0 = NUMERIC;
|
||||
np = strtol(argv[iarg], &p, 0);
|
||||
if (p == argv[iarg]) goto illarg;
|
||||
if (p == argv[iarg])
|
||||
goto illarg;
|
||||
iarg++;
|
||||
if (to <= from) {
|
||||
step = 1;
|
||||
@ -282,20 +304,23 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
break;
|
||||
} else {
|
||||
step = strtol(argv[iarg], &p, 0);
|
||||
if (p == argv[iarg]) goto illarg;
|
||||
if (p == argv[iarg])
|
||||
goto illarg;
|
||||
iarg++;
|
||||
if (step <= 0) step = 1;
|
||||
if (step <= 0)
|
||||
step = 1;
|
||||
type0 = NUMERIC;
|
||||
np = (from - to) / step + 2;
|
||||
}
|
||||
}
|
||||
if (step <= 0) step = 1;
|
||||
|
||||
snprintf(line, sizeof line, "%ld\n", (long)now);
|
||||
if (step <= 0)
|
||||
step = 1;
|
||||
|
||||
snprintf(line, sizeof line, "%ld\n", (long) now);
|
||||
SCWrite(pCon, line, eWarning);
|
||||
|
||||
dirPath = IFindOption(pSICSOptions, "LogReaderPath");
|
||||
if (dirPath == NULL) { /* for compatibility, check */
|
||||
if (dirPath == NULL) { /* for compatibility, check */
|
||||
dirs[0] = IFindOption(pSICSOptions, "LoggerDir");
|
||||
if (dirs[0] == NULL) {
|
||||
SCWrite(pCon, "ERROR: LoggerPath not found", eError);
|
||||
@ -327,22 +352,23 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
dirPath = colon + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
loss = 0;
|
||||
overflow = 0;
|
||||
for (i=iarg; i<argc; i++) {
|
||||
for (i = iarg; i < argc; i++) {
|
||||
startim = from;
|
||||
t = 0;
|
||||
lastt = 0;
|
||||
inRange = 0;
|
||||
xs = step;
|
||||
InitCompressor(&c, pCon, step);
|
||||
|
||||
|
||||
/* convert slashes to dots */
|
||||
varp = argv[i];
|
||||
|
||||
if (*varp == '/') varp++;
|
||||
for (j = 0; j < sizeof(var)-1 && *varp != 0; j++, varp++) {
|
||||
if (*varp == '/')
|
||||
varp++;
|
||||
for (j = 0; j < sizeof(var) - 1 && *varp != 0; j++, varp++) {
|
||||
if (*varp == '/') {
|
||||
var[j] = '.';
|
||||
} else {
|
||||
@ -371,16 +397,16 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
while (startim <= to && c.last.t <= to) {
|
||||
tm = *localtime(&startim);
|
||||
if (tm.tm_yday != yday) {
|
||||
if (fil != NULL) { /* close file if day changed */
|
||||
if (fil != NULL) { /* close file if day changed */
|
||||
fclose(fil);
|
||||
fil=NULL;
|
||||
fil = NULL;
|
||||
}
|
||||
}
|
||||
if (fil == NULL) {
|
||||
yday = tm.tm_yday;
|
||||
strftime(path + pathLen, sizeof path - pathLen, "%m-%d.log", &tm);
|
||||
fil = fopen(path, "r");
|
||||
if (fil != NULL) { /* check if file is from the given year */
|
||||
if (fil != NULL) { /* check if file is from the given year */
|
||||
strftime(stim, sizeof stim, "#%Y-%m-%d", &tm);
|
||||
fgets(line, sizeof line, fil);
|
||||
if (0 != strncmp(line, stim, 11)) {
|
||||
@ -409,8 +435,9 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
} else {
|
||||
do {
|
||||
lin = fgets(line, sizeof line, fil);
|
||||
/* printf("%s\n", line); */
|
||||
if (lin == NULL) break;
|
||||
/* printf("%s\n", line); */
|
||||
if (lin == NULL)
|
||||
break;
|
||||
if (line[0] == '#') {
|
||||
c.period = getOpt(line, &isdst, &c.exact);
|
||||
if (c.period >= 0) {
|
||||
@ -425,10 +452,14 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
xs = step;
|
||||
}
|
||||
}
|
||||
lin[0]='\0';
|
||||
lin[0] = '\0';
|
||||
} else {
|
||||
p = strchr(line, '\n'); if (p) *p='\0';
|
||||
p = strchr(line, '#'); if (p) *p='\0';
|
||||
p = strchr(line, '\n');
|
||||
if (p)
|
||||
*p = '\0';
|
||||
p = strchr(line, '#');
|
||||
if (p)
|
||||
*p = '\0';
|
||||
}
|
||||
} while (lin[0] == '\0');
|
||||
}
|
||||
@ -436,15 +467,15 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
/* printf(" %s\n", line); */
|
||||
p = strchr(line, '\t');
|
||||
if (p) {
|
||||
*p='\0';
|
||||
val = p+1;
|
||||
*p = '\0';
|
||||
val = p + 1;
|
||||
} else {
|
||||
val = "";
|
||||
}
|
||||
p = strchr(val, '\t');
|
||||
if (p) {
|
||||
stp = p+1;
|
||||
*p='\0';
|
||||
stp = p + 1;
|
||||
*p = '\0';
|
||||
iret = sscanf(stp, "%ld", &c.period);
|
||||
if (iret == 1) {
|
||||
if (c.period == 0) {
|
||||
@ -459,19 +490,20 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
}
|
||||
}
|
||||
}
|
||||
iret = sscanf(line, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
|
||||
iret =
|
||||
sscanf(line, "%d:%d:%d", &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
|
||||
if (iret != 3) {
|
||||
lin = NULL;
|
||||
} else {
|
||||
tm.tm_isdst = isdst;
|
||||
t=mktime(&tm);
|
||||
t = mktime(&tm);
|
||||
if (!inRange) {
|
||||
if (t < startim) {
|
||||
lastval[0]='\0';
|
||||
lastval[0] = '\0';
|
||||
strncat(lastval, val, sizeof lastval - 1);
|
||||
lastt = t;
|
||||
} else {
|
||||
inRange=1;
|
||||
inRange = 1;
|
||||
if (lastt != 0) {
|
||||
PutValue(&c, lastt, lastval);
|
||||
}
|
||||
@ -483,11 +515,11 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
}
|
||||
}
|
||||
if (lin == NULL) {
|
||||
tm.tm_hour = 24; /* try next day */
|
||||
tm.tm_hour = 24; /* try next day */
|
||||
tm.tm_min = 0;
|
||||
tm.tm_sec = 0;
|
||||
tm.tm_isdst = -1;
|
||||
startim=mktime(&tm);
|
||||
startim = mktime(&tm);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -496,13 +528,14 @@ static int LogReader(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
PutValue(&c, lastt, lastval);
|
||||
}
|
||||
}
|
||||
WriteHeader(&c); /* write header, if not already done */
|
||||
WriteHeader(&c); /* write header, if not already done */
|
||||
PutFinish(&c, nowi);
|
||||
if (fil) {
|
||||
fclose(fil);
|
||||
fil = NULL;
|
||||
}
|
||||
if (c.np < 0) overflow = 1;
|
||||
if (c.np < 0)
|
||||
overflow = 1;
|
||||
}
|
||||
snprintf(line, sizeof line, "*%d %d\n", loss, overflow);
|
||||
SCWrite(pCon, line, eWarning);
|
||||
@ -511,14 +544,18 @@ illarg:
|
||||
SCWrite(pCon, "illegal argument(s)", eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
static void KillLogReader(void *data) {
|
||||
static void KillLogReader(void *data)
|
||||
{
|
||||
Logger *p, *next;
|
||||
|
||||
|
||||
KillDummy(data);
|
||||
LoggerFreeAll();
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
void LogReaderInit(void) {
|
||||
AddCommand(pServ->pSics,"Graph",LogReader,KillLogReader,NULL);
|
||||
void LogReaderInit(void)
|
||||
{
|
||||
AddCommand(pServ->pSics, "Graph", LogReader, KillLogReader, NULL);
|
||||
}
|
||||
|
Reference in New Issue
Block a user