-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.c
141 lines (115 loc) · 2.78 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
#include <stdlib.h>
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "utils.h"
#include "log.h"
static enum u_loglevel loglevel = U_LOG_INFO;
static bool is_tty;
void u_log_init(void)
{
const char *s;
is_tty = isatty(STDERR_FILENO);
s = getenv("LOGLEVEL");
if (!s)
return;
if (streq(s, "debug"))
loglevel = U_LOG_DEBUG;
else if (streq(s, "info"))
loglevel = U_LOG_INFO;
else if (streq(s, "warn") || streq(s, "warning"))
loglevel = U_LOG_WARN;
else if (streq(s, "err") || streq(s, "error"))
loglevel = U_LOG_ERR;
else
u_log(WARN, "unknown loglevel '%s'", s);
}
#define COLOR_START "\x1b[38;5;%dm"
#define COLOR_END "\x1b[0m"
#define COLOR_GREEN 40
#define COLOR_BLUE 75
#define COLOR_RED 196
#define COLOR_ORANGE 202
bool check_loglevel(enum u_loglevel level)
{
if (u_likely(level > loglevel))
return false;
return true;
}
void _u_log(enum u_loglevel level, const char *path, const char *func, int line, const char *format, ...)
{
int _;
if (check_loglevel(level) == false)
return;
const char *file = strrchr(path, '/');
if (file)
file = file + 1;
else
file = path;
char buf[128];
if (is_tty) {
static const int color_map[] = {
[U_LOG_ERR] = COLOR_RED,
[U_LOG_WARN] = COLOR_ORANGE,
[U_LOG_INFO] = COLOR_GREEN,
[U_LOG_DEBUG] = COLOR_BLUE
};
_ = snprintf(buf, sizeof(buf), COLOR_START "%s:%s:%d\t%s" COLOR_END "\n",
color_map[level],
file, func, line,
format);
} else {
static const char *level_map[] = {
[U_LOG_ERR] = "ERR",
[U_LOG_WARN] = "WRN",
[U_LOG_INFO] = "INF",
[U_LOG_DEBUG] = "DBG"
};
_ = snprintf(buf, sizeof(buf), "%s %s:%s:%d %s\n",
level_map[level],
file, func, line,
format);
}
if (_ < 0) {
fputs("error formatting log message", stderr);
return;
}
va_list args;
va_start(args, format);
vfprintf(stderr, buf, args);
va_end(args);
}
void hexdump(void *addr, size_t len)
{
size_t i;
unsigned char buff[17];
unsigned char *pc = (unsigned char*)addr;
// Process every byte in the data.
for (i = 0; i < len; i++) {
// Multiple of 16 means new line (with line offset).
if ((i % 16) == 0) {
// Just don't print ASCII for the zeroth line.
if (i != 0)
printf (" %s\n", buff);
// Output the offset.
printf (" %04zx ", i);
}
// Now the hex code for the specific character.
printf (" %02x", pc[i]);
// And store a printable ASCII character for later.
if ((pc[i] < 0x20) || (pc[i] > 0x7e))
buff[i % 16] = '.';
else
buff[i % 16] = pc[i];
buff[(i % 16) + 1] = '\0';
}
// Pad out last line if not exactly 16 characters.
while ((i % 16) != 0) {
printf (" ");
i++;
}
// And print the final ASCII bit.
printf (" %s\n", buff);
}