forked from cesanta/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.c
More file actions
67 lines (57 loc) · 1.68 KB
/
Copy pathlog.c
File metadata and controls
67 lines (57 loc) · 1.68 KB
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
#include "log.h"
#include "util.h"
#if MG_ENABLE_LOG
static void mg_log_stdout(const void *buf, size_t len, void *userdata) {
(void) userdata, (void) buf, (void) len;
#if MG_ENABLE_FILE
fwrite(buf, 1, len, stdout);
#endif
}
static const char *s_spec = "2";
static void (*s_fn)(const void *, size_t, void *) = mg_log_stdout;
static void *s_fn_param = NULL;
void mg_log_set(const char *spec) {
MG_DEBUG(("Setting log level to %s", spec));
s_spec = spec;
}
bool mg_log_prefix(int level, const char *file, int line, const char *fname) {
// static unsigned long seq;
int max = MG_LL_INFO;
struct mg_str k, v, s = mg_str(s_spec);
const char *p = strrchr(file, '/');
if (s_fn == NULL) return false;
if (p == NULL) p = strrchr(file, '\\');
p = p == NULL ? file : p + 1;
while (mg_commalist(&s, &k, &v)) {
if (v.len == 0) max = atoi(k.ptr);
if (v.len > 0 && strncmp(p, k.ptr, k.len) == 0) max = atoi(v.ptr);
}
if (level <= max) {
char buf[41];
size_t n = mg_snprintf(buf, sizeof(buf), "%llx %d %s:%d:%s", mg_millis(),
level, p, line, fname);
if (n > sizeof(buf) - 2) n = sizeof(buf) - 2;
while (n < sizeof(buf)) buf[n++] = ' ';
buf[sizeof(buf) - 1] = '\0';
s_fn(buf, n - 1, s_fn_param);
return true;
} else {
return false;
}
}
void mg_log(const char *fmt, ...) {
char mem[256], *buf = mem;
va_list ap;
size_t len;
va_start(ap, fmt);
len = mg_vasprintf(&buf, sizeof(mem), fmt, ap);
va_end(ap);
s_fn(buf, len, s_fn_param);
s_fn("\n", 1, s_fn_param);
if (buf != mem) free(buf);
}
void mg_log_set_callback(void (*fn)(const void *, size_t, void *), void *fnd) {
s_fn = fn;
s_fn_param = fnd;
}
#endif