Fixed thread race condition

This commit is contained in:
Davide Paro 2022-01-04 18:20:35 +01:00
parent d2f9d62d19
commit a885027ff1
2 changed files with 79 additions and 107 deletions

183
src/log.c
View File

@ -25,159 +25,132 @@
#define MAX_CALLBACKS 32 #define MAX_CALLBACKS 32
typedef struct { typedef struct {
log_LogFn fn; log_LogFn fn;
void *udata; void *udata;
int level; int level;
} Callback; } Callback;
static struct { static struct {
void *udata; void *udata;
log_LockFn lock; log_LockFn lock;
int level; int level;
bool quiet; bool quiet;
Callback callbacks[MAX_CALLBACKS]; Callback callbacks[MAX_CALLBACKS];
} L = { } L = {
NULL, NULL, NULL, RXI_LOGC_DEFAULT_LEVEL, false, {0},
NULL,
RXI_LOGC_DEFAULT_LEVEL,
false,
{0},
}; };
static const char *level_strings[] = {"[[DEBUG]]", "[[TRACE]]", "[[ INFO]]",
"[[ WARN]]", "[[ERROR]]", "[[FATAL]]"};
static const char *level_strings[] = { static inline const char *get_level_string(int level) {
"[[DEBUG]]", "[[TRACE]]", "[[ INFO]]", "[[ WARN]]", "[[ERROR]]", "[[FATAL]]"
};
static inline const char *get_level_string(int level)
{
return level_strings[(level + 32) / 32]; 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[36m", "\x1b[94m", "\x1b[32m",
"\x1b[36m", "\x1b[94m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" "\x1b[33m", "\x1b[31m", "\x1b[35m"};
};
static inline const char *get_level_color(int level) static inline const char *get_level_color(int level) {
{
return level_colors[(level + 32) / 32]; 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%-7s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", buf,
ev->udata, "%s %s%-7s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", get_level_color(ev->level), get_level_string(ev->level), ev->file,
buf, get_level_color(ev->level), get_level_string(ev->level), ev->line);
ev->file, ev->line);
#else #else
fprintf( fprintf(ev->udata, "%s %-7s %s:%d: ", buf, get_level_string(ev->level),
ev->udata, "%s %-7s %s:%d: ", 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");
fflush(ev->udata); fflush(ev->udata);
} }
static void file_callback(log_Event *ev) { static void file_callback(log_Event *ev) {
char buf[64]; char buf[64];
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 %-7s %s:%d: ", buf, get_level_string(ev->level),
ev->udata, "%s %-7s %s:%d: ", 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);
} }
static void lock(void) {
static void lock(void) { if (L.lock) {
if (L.lock) { L.lock(true, L.udata); } L.lock(true, L.udata);
}
} }
static void unlock(void) { static void unlock(void) {
if (L.lock) { L.lock(false, L.udata); } if (L.lock) {
L.lock(false, L.udata);
}
} }
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;
} }
void log_set_level(int level) { L.level = level; }
void log_set_level(int level) { void log_set_quiet(bool enable) { L.quiet = enable; }
L.level = level;
}
void log_set_quiet(bool enable) {
L.quiet = enable;
}
int log_add_callback(log_LogFn fn, void *udata, int level) { int log_add_callback(log_LogFn fn, void *udata, int level) {
for (int i = 0; i < MAX_CALLBACKS; i++) { for (int i = 0; i < MAX_CALLBACKS; i++) {
if (!L.callbacks[i].fn) { if (!L.callbacks[i].fn) {
L.callbacks[i] = (Callback) { fn, udata, level }; L.callbacks[i] = (Callback){fn, udata, level};
return 0; return 0;
}
} }
} return -1;
return -1;
} }
int log_add_fp(FILE *fp, int level) { int log_add_fp(FILE *fp, int level) {
return log_add_callback(file_callback, fp, level); return log_add_callback(file_callback, fp, level);
} }
static void init_event(log_Event *ev, void *udata) { static void init_event(log_Event *ev, void *udata) {
if (!ev->time) { if (!ev->time) {
time_t t = time(NULL); time_t t = time(NULL);
ev->time = localtime(&t); localtime_r(&t, &ev->time_buf);
} }
ev->udata = udata; ev->udata = udata;
} }
void log_log(int level, const char *file, int line, const char *fmt, ...) { void log_log(int level, const char *file, int line, const char *fmt, ...) {
log_Event ev = { log_Event ev = {
.fmt = fmt, .fmt = fmt,
.file = file, .file = file,
.line = line, .line = line,
.level = level, .level = level,
}; };
lock(); lock();
if (!L.quiet && level >= L.level) { if (!L.quiet && level >= L.level) {
init_event(&ev, stderr); init_event(&ev, stderr);
va_start(ev.ap, fmt); va_start(ev.ap, fmt);
stdout_callback(&ev); stdout_callback(&ev);
va_end(ev.ap); va_end(ev.ap);
}
for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) {
Callback *cb = &L.callbacks[i];
if (level >= cb->level) {
init_event(&ev, cb->udata);
va_start(ev.ap, fmt);
cb->fn(&ev);
va_end(ev.ap);
} }
}
unlock(); for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) {
Callback *cb = &L.callbacks[i];
if (level >= cb->level) {
init_event(&ev, cb->udata);
va_start(ev.ap, fmt);
cb->fn(&ev);
va_end(ev.ap);
}
}
unlock();
} }

View File

@ -39,6 +39,7 @@ typedef struct {
const char *fmt; const char *fmt;
const char *file; const char *file;
struct tm *time; struct tm *time;
struct tm time_buf;
void *udata; void *udata;
int line; int line;
int level; int level;
@ -47,8 +48,6 @@ typedef struct {
typedef void (*log_LogFn)(log_Event *ev); typedef void (*log_LogFn)(log_Event *ev);
typedef void (*log_LockFn)(bool lock, void *udata); typedef void (*log_LockFn)(bool lock, void *udata);
#define log_debug(...) \ #define log_debug(...) \
do { \ do { \
if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) \ if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) \