Merge commit 'a8ffd1c6'

* commit 'a8ffd1c6':
  Forgot this line in previous commit
  Fixed thread race condition
This commit is contained in:
Davide Paro 2022-01-05 17:05:31 +01:00
commit e388f20bb5
2 changed files with 79 additions and 107 deletions

View File

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

View File

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