-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulog_utils.h
78 lines (70 loc) · 2.02 KB
/
ulog_utils.h
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
#ifndef ULOG_UTILS_H
#define ULOG_UTILS_H
#include "ulog.h"
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef __cplusplus
extern "C" {
#endif
static int _ulog_default_log(void *userdata, int tag, const char *line)
{
int rc = printf("%s", line);
if (userdata) {
FILE *fp = userdata;
fputs(line, fp);
fflush(fp);
}
return rc;
}
// init ulog with default configuration.
// will log to stdout and a log file in current work directory.
// app: argv[0] program name, using for log file name. 'app.log.date_time'
// using filter if ENV var ULOG_LEVEL set Trace/Debug/Info/Warn/Error.
static void ulog_init_default(const char *app)
{
int lv = ULOG_LV_ALL;
char *env = getenv("ULOG_LEVEL");
if (env) {
if (strcasecmp(env, "Trace") == 0) {
lv = ULOG_LV_TRACE;
} else if (strcasecmp(env, "Debug") == 0) {
lv = ULOG_LV_DEBUG;
} else if (strcasecmp(env, "Info") == 0) {
lv = ULOG_LV_INFO;
} else if (strcasecmp(env, "Warn") == 0) {
lv = ULOG_LV_WARN;
} else if (strcasecmp(env, "Error") == 0) {
lv = ULOG_LV_ERROR;
} else {
printf("invalid ulog ENV '%s' = '%s\n', using default",
"ULOG_LEVEL", env);
env = "Trace";
}
printf("ulog level=%s\n", env);
}
for (const char *p = app; *p; p++) {
if (*p == '/' || *p == '\\') {
app = p + 1;
}
}
time_t now = time(NULL);
char tim[32];
strftime(tim, sizeof(tim), "%y%m%d_%H%M", localtime(&now));
char buf[64];
snprintf(buf, sizeof(buf), "%s.log.%s", app, tim);
FILE* logf = fopen(buf, "w");
if (logf) {
printf("ulog create log file '%s' OK\n", buf);
ulog_init(_ulog_default_log, logf, lv);
} else {
printf("ulog create log file '%s' FAILED, %s\n", buf, strerror(errno));
ulog_init(NULL, NULL, lv);
}
}
#ifdef __cplusplus
}
#endif
#endif // ULOG_UTILS_H