-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.c
405 lines (351 loc) · 10.1 KB
/
log.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
20230815
Jan Mojzis
Public domain.
The 'log' library is used to write log messages
to standard error output including source file,
function and line number.
Non-printable characters are escaped.
Log format:
time: name: level: ip: message (error){file:line}[id]
time .......... optional
ip ............ optional
name .......... optional
{file:line} ... in debug mode
[id] .......... optional
*/
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include "e.h"
#include "randommod.h"
#include "log.h"
int log_level = log_level_FATAL;
static const char *logname = 0;
static int logtime = 0;
static long long loglimit = 200;
static const char *logipstr = 0;
static const char *logid = "";
static char logidbuf[9];
void log_set_level(int level) {
log_level = level;
if (level < log_level_USAGE) log_level = log_level_USAGE;
if (level > log_level_TRACING) log_level = log_level_TRACING;
}
void log_inc_level(int signal) {
(void) signal;
log_set_level(log_level + 1);
}
void log_dec_level(int signal) {
(void) signal;
log_set_level(log_level - 1);
}
void log_set_name(const char *name) { logname = name; }
void log_set_time(int flag) { logtime = flag; }
void log_set_ip(const char *ip) { logipstr = ip; }
void log_set_limit(long long limit) { loglimit = limit; }
const char *log_get_id(void) { return logid; }
static char buf[256];
static unsigned long long buflen = 0;
static void flush(void) {
char *b = buf;
long long r;
while (buflen > 0) {
r = write(2, b, buflen);
if (r < 0) {
if (errno == EINTR) continue;
if (errno == EAGAIN) continue;
if (errno == EWOULDBLOCK) continue;
break;
}
if (r == 0) break;
b += r;
buflen -= r;
}
buflen = 0;
}
static void outch(const char x) {
if (buflen >= sizeof buf) flush();
buf[buflen++] = x;
}
static void outsescape(const char *x, int flaglf, long long *counter) {
long long i;
for (i = 0; x[i]; ++i) {
if (counter && ++*counter > loglimit) {
outch('.');
outch('.');
outch('.');
break;
}
if (x[i] == '\n') {
if (flaglf) { outch('\n'); }
else {
outch('\\');
outch('n');
}
}
else if (x[i] == '\r') {
outch('\\');
outch('r');
}
else if (x[i] == '\t') {
outch('\\');
outch('t');
}
else if (x[i] < 32 || x[i] > 126) {
outch('\\');
outch('x');
outch("0123456789abcdef"[(x[i] >> 4) & 15]);
outch("0123456789abcdef"[(x[i] >> 0) & 15]);
}
else { outch(x[i]); }
}
}
#define outs(x) outsescape((x), 1, 0);
static char *numtostr(char *strbuf, long long strbuflen, long long n,
long long cnt) {
long long len = 0;
unsigned long long n1, n2;
int flagsign = 0;
if (cnt > strbuflen - 1) cnt = strbuflen - 1;
n1 = n2 = (unsigned long long) n;
if (n < 0) {
n1 = -n1;
n2 = -n2;
flagsign = 1;
}
do {
n1 /= 10;
++len;
} while (n1);
if (flagsign) ++len;
strbuf += len;
if (cnt > len) strbuf += cnt - len;
*strbuf = 0;
do {
*--strbuf = '0' + (n2 % 10);
n2 /= 10;
} while (n2);
while (cnt > len) {
*--strbuf = '0';
--cnt;
}
if (flagsign) *--strbuf = '-';
return strbuf;
}
#define STATICBUFSIZE 67
static void outnum(unsigned long long n, unsigned long long cnt) {
char numbuf[STATICBUFSIZE];
outs(numtostr(numbuf, sizeof numbuf, n, cnt));
}
void log_9_(int level, int flagerror, const char *f, unsigned long long l,
const char *s0, const char *s1, const char *s2, const char *s3,
const char *s4, const char *s5, const char *s6, const char *s7,
const char *s8) {
const char *s[9];
long long i;
const char *m;
long long counter = 0;
long long *counterptr = &counter;
if (level > log_level) return;
if (log_level <= 2) counterptr = 0;
s[0] = s0;
s[1] = s1;
s[2] = s2;
s[3] = s3;
s[4] = s4;
s[5] = s5;
s[6] = s6;
s[7] = s7;
s[8] = s8;
switch (level) {
case 1:
if (flagerror == 2)
m = "bug";
else
m = "fatal";
break;
case 2:
if (flagerror == 1)
m = "error";
else if (flagerror == 2)
m = "warning";
else
m = "info";
break;
case 3:
m = "debug";
break;
case 4:
m = "tracing";
break;
default:
m = "unknown";
break;
}
/* time: name: level: ip: message (error){file:line}[id] */
/* 'time:' */
do {
struct tm *t;
int saved_errno = errno;
time_t secs = time(0);
if (!level) break; /* don't print in usage level */
if (!logtime) break; /* don't print when logtime = 0 */
t = localtime(&secs);
outnum(t->tm_year + 1900, 4);
outs("-");
outnum(t->tm_mon + 1, 2);
outs("-");
outnum(t->tm_mday, 2);
outs(" ");
outnum(t->tm_hour, 2);
outs(":");
outnum(t->tm_min, 2);
outs(":");
outnum(t->tm_sec, 2);
outs(": ");
errno = saved_errno;
} while (0);
/* 'name:' */
do {
if (!level) break; /* don't print in usage level */
if (!logname) break; /* don't print when logname = 0 */
outsescape(logname, 0, counterptr);
outs(": ");
} while (0);
/* 'level:' */
do {
if (!level) break; /* don't print in usage level */
outs(m);
outs(": ");
} while (0);
/* 'ip:' */
do {
if (!level) break; /* don't print in usage level */
if (!logipstr) break; /* don't print when logipstr = 0 */
outsescape(logipstr, 0, counterptr);
outs(": ");
} while (0);
/* 'message' */
for (i = 0; i < 9 && s[i]; ++i) outsescape(s[i], !level, counterptr);
outs(" ");
/* '(error)' */
do {
if (!level) break; /* don't print in usage level */
if (!errno) break; /* don't print when errno = 0 */
if (!flagerror) break; /* don't print when disabled */
if (level >= 3) break; /* don't print in debug message */
outs("(");
outs(e_str(errno));
outs(")");
} while (0);
/* {file:line} */
do {
if (!f) break; /* don't print when no f */
if (!l) break; /* don't print when no l */
if (!level) break; /* don't print in usage level */
if (log_level <= 2) break; /* print only when debug verbosity is set */
outs("{");
outs(f);
outs(":");
outnum(l, 0);
outs("}");
} while (0);
/* [id] */
do {
if (log_level <= 1) break; /* don't print in usage, fatal level */
if (!logid) break; /* don't print when logid = 0 */
if (logid[0] == 0) break; /* don't print when logid = "" */
outs("[");
outsescape(logid, 0, counterptr);
outs("]");
} while (0);
outs("\n");
flush();
return;
}
static char staticbuf[9][STATICBUFSIZE];
static int staticbufcounter = 0;
char *log_ip(unsigned char *ip) {
staticbufcounter = (staticbufcounter + 1) % 9;
if (memcmp(ip, "\0\0\0\0\0\0\0\0\0\0\377\377", 12)) {
struct sockaddr_in6 sa;
memcpy(&(sa.sin6_addr), ip, 16);
inet_ntop(AF_INET6, &(sa.sin6_addr), staticbuf[staticbufcounter],
STATICBUFSIZE);
}
else {
struct sockaddr_in sa;
memcpy(&(sa.sin_addr), ip + 12, 4);
inet_ntop(AF_INET, &(sa.sin_addr), staticbuf[staticbufcounter],
STATICBUFSIZE);
}
return staticbuf[staticbufcounter];
}
char *log_port(unsigned char *port) {
staticbufcounter = (staticbufcounter + 1) % 9;
return numtostr(staticbuf[staticbufcounter], STATICBUFSIZE,
port[0] << 8 | port[1], 0);
}
char *log_num(long long num) {
staticbufcounter = (staticbufcounter + 1) % 9;
return numtostr(staticbuf[staticbufcounter], STATICBUFSIZE, num, 0);
}
char *log_num0(long long num, long long cnt) {
staticbufcounter = (staticbufcounter + 1) % 9;
return numtostr(staticbuf[staticbufcounter], STATICBUFSIZE, num, cnt);
}
static void tohex(char *x, long long xlen, unsigned char *y, long long ylen) {
long long i;
for (i = 0; i < ylen; ++i) {
if (i == (xlen - 3) / 2) {
x[2 * i] = '.';
x[2 * i + 1] = '.';
x[2 * i + 2] = 0;
return;
}
x[2 * i] = "0123456789abcdef"[(y[i] >> 4) & 15];
x[2 * i + 1] = "0123456789abcdef"[(y[i] >> 0) & 15];
}
x[2 * i] = 0;
}
char *log_hex(unsigned char *y, long long ylen) {
char *x;
staticbufcounter = (staticbufcounter + 1) % 9;
x = staticbuf[staticbufcounter];
tohex(x, STATICBUFSIZE, y, ylen);
return x;
}
char *log_argv(char **argv) {
if (!argv) return 0;
return argv[0];
}
void log_set_id(const char *id) {
if (!id) id = getenv("LOG_ID");
if (!id) {
static char chars[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ0123456789";
unsigned long long i;
for (i = 0; i < sizeof logidbuf; ++i) {
logidbuf[i] = chars[randommod(sizeof chars - 1)];
}
logidbuf[sizeof logidbuf - 1] = 0;
id = logidbuf;
}
logid = id;
(void) setenv("LOG_ID", id, 1);
}
void log_set_hexid(const unsigned char *x, long long xlen) {
long long i;
for (i = 0; i < (long long) sizeof(logidbuf); ++i) logidbuf[i] = 0;
for (i = 0; i < xlen; ++i) {
logidbuf[2 * i] = "0123456789abcdef"[(x[i] >> 4) & 15];
logidbuf[2 * i + 1] = "0123456789abcdef"[(x[i] >> 0) & 15];
if (i == (sizeof(logidbuf) / 2) - 1) break;
}
logid = logidbuf;
}