Fixed bug
This commit is contained in:
parent
f0933c40a1
commit
793b9365ce
29
src/log.c
29
src/log.c
@ -40,28 +40,42 @@ static struct {
|
|||||||
|
|
||||||
|
|
||||||
static const char *level_strings[] = {
|
static const char *level_strings[] = {
|
||||||
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
|
"DEBUG", "TRACE", "INFO", "WARN", "ERROR", "FATAL"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
static inline const char *get_level_string(int level)
|
||||||
|
{
|
||||||
|
return level_strings[(level + 32) / 32];
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef LOG_USE_COLOR
|
#ifdef LOG_USE_COLOR
|
||||||
static const char *level_colors[] = {
|
static const char *level_colors[] = {
|
||||||
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
|
"\x1b[36m", "\x1b[94m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static inline const char *get_level_color(int level)
|
||||||
|
{
|
||||||
|
return level_colors[(level + 32) / 32];
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static void stdout_callback(log_Event *ev) {
|
static void stdout_callback(log_Event *ev) {
|
||||||
char buf[16];
|
char buf[16];
|
||||||
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
|
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
|
||||||
#ifdef LOG_USE_COLOR
|
#ifdef LOG_USE_COLOR
|
||||||
fprintf(
|
fprintf(
|
||||||
ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
|
ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
|
||||||
buf, level_colors[ev->level], level_strings[ev->level],
|
buf, get_level_color(ev->level), get_level_string(ev->level),
|
||||||
ev->file, ev->line);
|
ev->file, ev->line);
|
||||||
#else
|
#else
|
||||||
fprintf(
|
fprintf(
|
||||||
ev->udata, "%s %-5s %s:%d: ",
|
ev->udata, "%s %-5s %s:%d: ",
|
||||||
buf, level_strings[ev->level], ev->file, ev->line);
|
buf, get_level_string(ev->level), ev->file, ev->line);
|
||||||
#endif
|
#endif
|
||||||
vfprintf(ev->udata, ev->fmt, ev->ap);
|
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||||
fprintf(ev->udata, "\n");
|
fprintf(ev->udata, "\n");
|
||||||
@ -74,7 +88,7 @@ static void file_callback(log_Event *ev) {
|
|||||||
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
|
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
|
||||||
fprintf(
|
fprintf(
|
||||||
ev->udata, "%s %-5s %s:%d: ",
|
ev->udata, "%s %-5s %s:%d: ",
|
||||||
buf, level_strings[ev->level], ev->file, ev->line);
|
buf, get_level_string(ev->level), ev->file, ev->line);
|
||||||
vfprintf(ev->udata, ev->fmt, ev->ap);
|
vfprintf(ev->udata, ev->fmt, ev->ap);
|
||||||
fprintf(ev->udata, "\n");
|
fprintf(ev->udata, "\n");
|
||||||
fflush(ev->udata);
|
fflush(ev->udata);
|
||||||
@ -91,11 +105,6 @@ static void unlock(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const char* log_level_string(int level) {
|
|
||||||
return level_strings[level];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void log_set_lock(log_LockFn fn, void *udata) {
|
void log_set_lock(log_LockFn fn, void *udata) {
|
||||||
L.lock = fn;
|
L.lock = fn;
|
||||||
L.udata = udata;
|
L.udata = udata;
|
||||||
|
Loading…
Reference in New Issue
Block a user