forked from starwing/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_log.c
521 lines (478 loc) · 16.8 KB
/
service_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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#define LOKI_MODULE
#include "loki_services.h"
#include "lk_buffer.h"
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
# include <time.h>
#else
# include <errno.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/time.h>
#endif
#ifndef LK_DEFAULT_LOGPATH
# define LK_DEFAULT_LOGPATH "logs/%Y-%M/%S_%Y-%M-%D-%I.log"
#endif
#define LK_MAX_CONFIGNAME 64
#define LK_MAX_CONFIGPATH 256
typedef struct lk_LogHeader {
const char *level;
const char *service;
const char *tag;
const char *key;
const char *msg;
size_t msglen;
struct tm tm;
lk_Buffer buff;
} lk_LogHeader;
typedef enum lk_ConfigMaskFlag {
lk_Mreload = 1 << 0,
lk_Mcolor = 1 << 1,
lk_Minterval = 1 << 2,
lk_Mfilepath = 1 << 3
} lk_ConfigMaskFlag;
typedef struct lk_Dumper {
char name[LK_MAX_CONFIGPATH];
unsigned index : 8;
unsigned interval : 24;
time_t next_update;
FILE *fp;
} lk_Dumper;
typedef struct lk_LogConfig {
char name[LK_MAX_CONFIGNAME];
char filepath[LK_MAX_CONFIGPATH];
unsigned mask : 8;
unsigned color : 8; /* 1248:RGBI, low 4bit: fg, high 4bit: bg */
unsigned interval : 24;
lk_Dumper *dumper;
} lk_LogConfig;
typedef struct lk_LogState {
lk_State *S;
lk_Table config;
lk_Table dump;
lk_MemPool configs;
lk_MemPool dumpers;
} lk_LogState;
#define lkX_readinteger(ls, B, config, key) do { \
char *s; \
lk_resetbuffer(B); \
lk_addfstring(B, "log.%s." #key, config->name); \
config->mask &= ~lk_M##key; \
if ((s = lk_getconfig(ls->S, lk_buffer(B)))) { \
config->key = atoi(s); \
config->mask |= lk_M##key; \
lk_deldata(ls->S, (lk_Data*)s); } } while(0)
#define lkX_readstring(ls, B, config, key) do { \
char *s; \
lk_resetbuffer(B); \
lk_addfstring(B, "log.%s." #key, config->name); \
config->mask &= ~lk_M##key; \
if ((s = lk_getconfig(ls->S, lk_buffer(B)))) { \
lk_strcpy(config->key, lk_buffer(B), \
LK_MAX_CONFIGPATH); \
config->mask |= lk_M##key; \
lk_deldata(ls->S, (lk_Data*)s); } } while(0)
/* config dumper */
static void lkX_localtime (time_t t, struct tm *tm)
#ifdef _MSC_VER
{ localtime_s(tm, &t); }
#elif _POSIX_SOURCE
{ localtime_r(&t, tm); }
#else
{ *tm = *localtime(&t); }
#endif
static void lkX_settime (lk_Dumper* dumper, int interval) {
struct tm tm;
time_t now = time(NULL), daytm;
lkX_localtime(now, &tm);
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
daytm = mktime(&tm);
dumper->interval = interval;
dumper->index = (int)((now - daytm) / interval);
dumper->next_update = daytm + (dumper->index + 1) * interval;
}
static void lkX_createdirs (lk_State *S, const char *path) {
const char *i, *last;
lk_Buffer B;
lk_initbuffer(S, &B);
for (i = last = path; i != '\0'; last = ++i) {
while (*i != '\0' && *i != '/' && *i != '\\')
lk_addchar(&B, *i++);
if (*i == '\0') break;
switch (last - i) {
case 0: continue;
case 1: if (*last == '.') continue; break;
case 2: if (last[0] == '.' && last[1] == '.') continue; break;
}
lk_addchar(&B, '/');
*lk_prepbuffsize(&B, 1) = '\0';
#if _WIN32
if (!CreateDirectoryA(lk_buffer(&B), NULL)
&& GetLastError() != ERROR_ALREADY_EXISTS)
break;
#else
if (mkdir(lk_buffer(&B), 0777) < 0 && errno != EEXIST)
break;
#endif
}
lk_freebuffer(&B);
}
static void lkX_escapefn (lk_Buffer *B, const char *s, int idx) {
struct tm tm;
lkX_localtime(time(NULL), &tm);
for (; *s != '\0'; ++s) {
if (*s != '%') {
lk_addchar(B, *s);
continue;
}
switch (*++s) {
case 'Y': lk_addfstring(B, "%04d", tm.tm_year + 1900); break;
case 'M': lk_addfstring(B, "%02d", tm.tm_mon + 1); break;
case 'D': lk_addfstring(B, "%02d", tm.tm_mday); break;
case 'I': lk_addfstring(B, "%d", idx); break;
case '\0': lk_addchar(B, '%'); --s; break;
default: lk_addchar(B, '%'); /* FALLTHROUGH */
case '%': lk_addchar(B, *s); break;
}
}
lk_addchar(B, '\0');
}
static void lkX_openfile (lk_LogState *ls, lk_Dumper* dumper) {
lk_Buffer B;
lk_initbuffer(ls->S, &B);
lkX_escapefn(&B, dumper->name, dumper->index);
lkX_createdirs(ls->S, lk_buffer(&B));
#ifdef _MSC_VER
fopen_s(&dumper->fp, lk_buffer(&B), "a");
#else
dumper->fp = fopen(lk_buffer(&B), "a");
#endif
lk_freebuffer(&B);
}
static lk_Dumper *lkX_newdumper (lk_LogState *ls, lk_LogConfig *config, lk_LogHeader *hs) {
const char *s;
lk_Buffer B;
lk_Dumper *dumper;
lk_Entry *e;
if (!(config->mask & lk_Mfilepath))
return NULL;
lk_initbuffer(ls->S, &B);
for (s = config->filepath; *s != '\0'; ++s) {
if (*s != '%') {
lk_addchar(&B, *s);
continue;
}
switch (*++s) {
case 'L': lk_addstring(&B, hs->level); break;
case 'S': lk_addstring(&B, hs->service); break;
case 'T': lk_addstring(&B, hs->tag); break;
default: lk_addchar(&B, '%');
lk_addchar(&B, *s); break;
}
}
lk_addchar(&B, '\0');
e = lk_settable(ls->S, &ls->dump, lk_buffer(&B));
if (e->key != lk_buffer(&B)) return (lk_Dumper*)e->key;
dumper = (lk_Dumper*)lk_poolalloc(ls->S, &ls->dumpers);
memset(dumper, 0, sizeof(*dumper));
lk_strcpy(dumper->name, lk_buffer(&B), LK_MAX_CONFIGPATH);
lk_freebuffer(&B);
if (config->interval > 0)
lkX_settime(dumper, config->interval);
lkX_openfile(ls, dumper);
e->key = dumper->name;
return dumper;
}
static int lkX_wheelfile (lk_LogState* ls, lk_Dumper* dumper) {
if (time(NULL) > dumper->next_update) {
if (dumper->fp) fclose(dumper->fp);
lkX_settime(dumper, dumper->interval);
lkX_openfile(ls, dumper);
}
return 0;
}
/* config reader */
static lk_LogConfig *lkX_newconfig (lk_LogState *ls, const char *name) {
lk_Entry *e = lk_settable(ls->S, &ls->config, name);
lk_LogConfig *config = (lk_LogConfig*)e->key;
if (e->key != name) return config;
config = (lk_LogConfig*)lk_poolalloc(ls->S, &ls->configs);
memset(config, 0, sizeof(*config));
lk_strcpy(config->name, name, LK_MAX_CONFIGNAME);
config->mask |= lk_Mreload;
config->color = 0x77;
e->key = config->name;
return config;
}
static lk_LogConfig* lkX_getconfig (lk_LogState *ls, const char *name) {
lk_LogConfig *config = lkX_newconfig(ls, name);
if (config->mask & lk_Mreload) {
lk_Buffer B;
lk_initbuffer(ls->S, &B);
lkX_readinteger(ls, &B, config, color);
lkX_readinteger(ls, &B, config, interval);
lkX_readstring(ls, &B, config, filepath);
if (config->interval > 60 * 60 * 24)
config->interval = 60 * 60 * 24;
lk_freebuffer(&B);
}
return config;
}
static int lkX_mergeconfig (lk_LogConfig *c1, lk_LogConfig *c2) {
if (c1 == NULL || c2 == NULL) return -1;
if (c2->mask & lk_Mcolor) c1->color = c2->color;
if (c2->mask & lk_Minterval) c1->interval = c2->interval;
if (c2->mask & lk_Mfilepath) lk_strcpy(c1->filepath, c2->filepath, LK_MAX_CONFIGPATH);
c1->mask |= c2->mask;
return 0;
}
static lk_LogConfig* lkX_setconfig (lk_LogState *ls, lk_LogHeader *hs) {
lk_LogConfig *config = lkX_getconfig(ls, hs->key);
if (config->mask & lk_Mreload) {
lk_LogConfig *other = lkX_getconfig(ls, hs->level);
if (other->mask != lk_Mreload)
lkX_mergeconfig(config, other);
other = lkX_getconfig(ls, hs->service);
if (other->mask == lk_Mreload)
other = lkX_getconfig(ls, "default_service");
if (other->mask != lk_Mreload)
lkX_mergeconfig(config, other);
if (hs->tag) {
other = lkX_getconfig(ls, hs->tag);
if (other->mask != lk_Mreload)
lkX_mergeconfig(config, other);
}
if ((config->mask & lk_Mfilepath) && config->dumper == NULL)
config->dumper = lkX_newdumper(ls, config, hs);
config->mask &= ~lk_Mreload;
}
return config;
}
/* config parser */
static void lkX_parseheader (lk_LogHeader *hs, const char* service, const char* s, size_t len) {
const char *end = s + len;
size_t offset_key = 0; /* key struct: [level][service][tag] */
hs->level = "info";
hs->service = service;
hs->tag = NULL;
hs->msg = s;
if (len >= 3 && s[1] == '[') {
const char *start = s + 2;
switch (*s) {
default: goto no_tag;
case 'I': break;
case 'T': hs->level = "trace"; break;
case 'V': hs->level = "verbose"; break;
case 'W': hs->level = "warning"; break;
case 'E': hs->level = "error"; break;
}
for (s = start; s < end && *s != ']'; ++s)
;
if (s == end) goto no_tag;
if (s - start != 0) {
lk_addlstring(&hs->buff, start, s - start);
lk_addchar(&hs->buff, '\0');
offset_key = lk_buffsize(&hs->buff);
}
hs->msg = *++s == ' ' ? s + 1 : s;
}
no_tag:
hs->msglen = end - hs->msg;
lk_addfstring(&hs->buff, "[%s][%s][", hs->level, service);
if (offset_key != 0) {
lk_prepbuffsize(&hs->buff, offset_key-1);
lk_addlstring(&hs->buff, lk_buffer(&hs->buff), offset_key-1);
hs->tag = lk_buffer(&hs->buff);
}
lk_addlstring(&hs->buff, "]\0", 2);
hs->key = lk_buffer(&hs->buff) + offset_key;
}
static void lkX_headerdump (lk_LogState *ls, lk_LogHeader *hs, FILE *fp) {
struct tm *tm = &hs->tm;
(void)ls;
if (hs->tag) fprintf(fp, "[%c][%s][%02d:%02d:%02d][%s]: ",
toupper(hs->level[0]), hs->service,
tm->tm_hour, tm->tm_min, tm->tm_sec, hs->tag);
else fprintf(fp, "[%c][%s][%02d:%02d:%02d]: ",
toupper(hs->level[0]), hs->service,
tm->tm_hour, tm->tm_min, tm->tm_sec);
}
static void lkX_filedump (lk_LogState *ls, lk_LogHeader *hs, lk_Dumper *dumper) {
if (dumper->interval > 0 || !dumper->fp)
lkX_wheelfile(ls, dumper);
if (dumper->fp) {
lkX_headerdump(ls, hs, dumper->fp);
fwrite(hs->msg, 1, hs->msglen, dumper->fp);
fputc('\n', dumper->fp);
fflush(dumper->fp);
}
}
static void lkX_screendump (lk_LogState *ls, const char *s, size_t len, int color) {
#ifdef _WIN32
lk_Buffer B;
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
WORD attr = 0, reset = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE;
DWORD written;
LPWSTR buff = NULL;
int bytes, cp = CP_UTF8;
char eol = '\n';
if (color & 0x01) attr |= FOREGROUND_RED;
if (color & 0x02) attr |= FOREGROUND_GREEN;
if (color & 0x04) attr |= FOREGROUND_BLUE;
if (color & 0x08) attr |= FOREGROUND_INTENSITY;
if (color & 0x10) attr |= BACKGROUND_RED;
if (color & 0x20) attr |= BACKGROUND_GREEN;
if (color & 0x40) attr |= BACKGROUND_BLUE;
if (color & 0x80) attr |= BACKGROUND_INTENSITY;
bytes = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, s, (int)len, NULL, 0);
lk_initbuffer(ls->S, &B);
if (bytes != 0) {
buff = (LPWSTR)lk_prepbuffsize(&B, bytes * sizeof(WCHAR));
bytes = MultiByteToWideChar(cp, 0, s, (int)len, buff, bytes);
}
SetConsoleTextAttribute(h, attr);
if (buff) WriteConsoleW(h, buff, bytes, &written, NULL);
else WriteConsoleA(h, s, (DWORD)len, &written, NULL);
SetConsoleTextAttribute(h, reset);
lk_freebuffer(&B);
WriteConsoleA(h, &eol, 1, &written, NULL);
#else
int fg = 30 + (color & 0x7), bg = 40 + ((color & 0x70)>>4);
(void)ls;
if (color & 0x08) fg += 60;
if (color & 0x80) bg += 60;
fprintf(stdout, "\x1B[%d;%dm%.*s\x1B[0m\n", fg, bg, (int)len, s);
fflush(stdout);
#endif
}
static int lkX_writelog (lk_LogState *ls, const char* service_name, const char* s, size_t len) {
lk_LogConfig *config;
lk_LogHeader hs;
lk_initbuffer(ls->S, &hs.buff);
lkX_parseheader(&hs, service_name, s, len);
config = lkX_setconfig(ls, &hs);
lkX_localtime(time(NULL), &hs.tm);
if (config->dumper)
lkX_filedump(ls, &hs, config->dumper);
if (config->color != 0) {
lkX_headerdump(ls, &hs, stdout);
lkX_screendump(ls, hs.msg, hs.msglen, config->color);
}
lk_freebuffer(&hs.buff);
return 0;
}
/* config initialize */
static lk_LogState *lkX_newstate (lk_State *S) {
lk_LogState *ls = (lk_LogState*)lk_malloc(S, sizeof(lk_LogState));
lk_LogConfig *config;
ls->S = S;
lk_inittable(&ls->config, sizeof(lk_Entry));
lk_inittable(&ls->dump, sizeof(lk_Entry));
lk_initpool(&ls->configs, sizeof(lk_LogConfig));
lk_initpool(&ls->dumpers, sizeof(lk_Dumper));
/* initialize config */
lk_resizetable(S, &ls->config, 32);
config = lkX_newconfig(ls, "info");
config->color = 0x07;
config->mask = lk_Mcolor;
config = lkX_newconfig(ls, "trace");
config->color = 0x0F;
config->mask = lk_Mcolor;
config = lkX_newconfig(ls, "verbose");
config->color = 0x70;
config->mask = lk_Mcolor;
config = lkX_newconfig(ls, "warning");
config->color = 0x0B;
config->mask = lk_Mcolor;
config = lkX_newconfig(ls, "error");
config->color = 0x9F;
config->mask = lk_Mcolor;
config = lkX_newconfig(ls, "default_service");
lk_strcpy(config->filepath, LK_DEFAULT_LOGPATH, LK_MAX_CONFIGPATH);
config->interval = 3600;
config->mask = lk_Mfilepath|lk_Minterval;
return ls;
}
static void lkX_delstate (lk_LogState* ls) {
lk_Entry *e = NULL;
while (lk_nextentry(&ls->config, &e)) {
lk_LogConfig *config = (lk_LogConfig*)e->key;
lk_poolfree(&ls->configs, config);
}
while (lk_nextentry(&ls->dump, &e)) {
lk_Dumper *dumper = (lk_Dumper*)e->key;
if (dumper && dumper->fp) fclose(dumper->fp);
lk_poolfree(&ls->dumpers, dumper);
}
lk_freetable(ls->S, &ls->config);
lk_freetable(ls->S, &ls->dump);
lk_freepool(ls->S, &ls->configs);
lk_freepool(ls->S, &ls->dumpers);
lk_free(ls->S, ls, sizeof(lk_LogState));
}
static int lkX_update (lk_State *S, lk_Slot *slot, lk_Signal *sig) {
lk_LogState *ls = (lk_LogState*)lk_data(slot);
lk_Entry *e = NULL;
(void)S, (void)sig;
while (lk_nextentry(&ls->config, &e)) {
lk_LogConfig *config = (lk_LogConfig*)e->key;
config->mask |= lk_Mreload;
config->dumper = NULL;
}
while (lk_nextentry(&ls->dump, &e)) {
lk_Dumper *dumper = (lk_Dumper*)e->key;
if (dumper && dumper->fp) fclose(dumper->fp);
lk_poolfree(&ls->dumpers, dumper);
e->key = NULL;
}
return LK_OK;
}
static int lkX_launch (lk_State *S, lk_Slot *sender, lk_Signal *sig) {
lk_LogState *ls = (lk_LogState*)lk_data(lk_current(S));
const char *msg = (const char*)sig->data;
lk_Data *data = lk_newfstring(S, "V[] service '%s'(%p) launched", msg, msg);
lkX_writelog(ls, (const char*)sender, (const char*)data, lk_len(data));
lk_deldata(S, data);
return LK_OK;
}
static int lkX_close (lk_State *S, lk_Slot *sender, lk_Signal *sig) {
lk_LogState *ls = (lk_LogState*)lk_data(lk_current(S));
const char *msg = (const char*)sig->data;
lk_Data *data = lk_newfstring(S, "V[] service '%s'(%p) closed", msg, msg);
lkX_writelog(ls, (const char*)sender, (const char*)data, lk_len(data));
lk_deldata(S, data);
return LK_OK;
}
LKMOD_API int loki_service_log (lk_State *S, lk_Slot *sender, lk_Signal *sig) {
lk_LogState *ls = (lk_LogState*)lk_userdata(S);
if (sender == NULL) {
ls = lkX_newstate(S);
lk_newslot(S, "update", lkX_update, ls);
lk_newslot(S, LK_SLOTNAME_LAUNCH, lkX_launch, ls);
lk_newslot(S, LK_SLOTNAME_CLOSE, lkX_close, ls);
lk_setdata(lk_current(S), ls);
return LK_WEAK;
}
else if (sig == NULL) {
const char *msg = (const char*)lk_self(S);
lk_Data *data = lk_newfstring(S, "V[] service '%s'(%p) closed", msg, msg);
lkX_writelog(ls, (const char*)sender, (const char*)data, lk_len(data));
lk_deldata(S, data);
lkX_delstate(ls);
}
else {
const char *msg = (const char*)sig->data;
size_t len = sig->isdata ? lk_len((lk_Data*)sig->data) : strlen(msg);
lkX_writelog(ls, (const char*)sender, msg, len);
}
return LK_OK;
}
/* win32cc: flags+='-s -mdll -xc' output='loki.dll' libs+='-lws2_32'
* unixcc: flags+='-fPIC -shared -xc' output='loki.so'
* cc: flags+='-Wextra -O3' input='service_*.c lokilib.c' */