-
Notifications
You must be signed in to change notification settings - Fork 8
/
logger.c
144 lines (126 loc) · 3.39 KB
/
logger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*============================ LOGGER =================================*/
/* Part of HUJI-NJE -- lifted from main.c */
/*
| Write a logging line in our logfile. If the loglevel is 1, close the file
| after writing, so we can look in it at any time.
*/
#include <stdio.h>
#include "consts.h"
#include "prototypes.h"
#include "ebcdic.h"
#include <stdarg.h>
#include <time.h>
extern int LogLevel; /* In main() source module of each program */
extern FILE *LogFd; /* In main() source module of each program */
/*============================ LOGGER =================================*/
/*
| Write a logging line in our logfile. If the loglevel is 1, close the file
| after writing, so we can look in it at any time.
*/
void
logger(int lvl, ...)
{
char *local_time();
char *fmt;
va_list pvar;
va_start(pvar, lvl);
/* lvl = va_arg(pvar,int); */
va_end(pvar);
/* Do we have to log it at all ? */
if (lvl > LogLevel) {
return;
}
/* Open the log file */
if (LogFd == 0) { /* Not opened before */
if (strcmp(LOG_FILE,"-")==0) LogFd = stderr;
else if ((LogFd = fopen(LOG_FILE, "a")) == NULL) {
LogFd = NULL;
return;
}
}
va_start(pvar, lvl);
/* lvl = va_arg(pvar,int); */
fmt = va_arg(pvar,char*);
fprintf(LogFd, "%s, ", local_time());
vfprintf(LogFd, fmt, pvar);
va_end(pvar);
fflush(LogFd);
if (LogLevel == 1) { /* Normal run - close file after loging */
if (LogFd != stdout)
fclose(LogFd);
LogFd = 0;
}
}
/*
| Return the time in a printable format; to be used by Bug-Check and Logger.
*/
char *
local_time()
{
static char TimeBuff[80];
struct tm *tm, *localtime();
time_t clock;
time(&clock); /* Get the current time */
tm = localtime(&clock);
sprintf(TimeBuff, "%04d-%02d-%02d %02d:%02d:%02d",
tm->tm_year+1900, (tm->tm_mon + 1), tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return TimeBuff;
}
/*
| Write a hex dump of the buffer passed. Do it only if the level associated
| with it is lower or equal to the current one. After the dump write the
| text of the message in ASCII. WE always try to convert from EBCDIC to
| ASCII as most traces are done at the network level.
*/
#define ADD_TEXT { /* Add the printable text */ \
NextLinePosition = &line[count * 3]; \
*NextLinePosition++ = ' '; \
*NextLinePosition++ = '|'; \
*NextLinePosition++ = ' '; \
while(q <= (unsigned char *)p) { \
c = EBCDIC_ASCII[*q++]; \
if((c >= 32) && (c <= 126)) /* Printable */ \
*NextLinePosition++ = c; \
else \
*NextLinePosition++ = '.'; \
} \
*NextLinePosition = '\0'; \
}
void
trace(p, n, lvl)
const void *p;
const int n, lvl;
{
register int count, i;
char line[LINESIZE];
unsigned char c; /* Point to the beginning of this buffer */
char *line2;
if (lvl > LogLevel) return;
logger(lvl, "Trace called with data size=%d\n", n);
count = 0; /* save beginning of buffer */
i = 0;
while(i < n) {
for (count = 0; count < 12; ++count) {
if (count+i < n)
sprintf(line+count*3, "%02x ", *(((unsigned char *)p)+i+count));
else
strcpy(line+count*3, " ");
}
strcat(line, " | ");
line2 = strlen(line)+line;
for (count = 0; count < 12; ++count) {
if (count+i < n) {
c = EBCDIC_ASCII[*(((unsigned char *)p)+i+count)];
if ((c >= 32) && (c <= 126))
line2[count] = c;
else
line2[count] = '.';
} else
line2[count] = 0;
}
line2[12] = 0;
logger(lvl, "%3d: %s\n", i, line);
i += 12;
}
}