Merge pull request #1 from dparo/master

GCC style
This commit is contained in:
Rostislav Stříbrný 2022-11-04 11:24:56 +01:00 committed by GitHub
commit 223a2e6db7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 146 additions and 115 deletions

184
src/log.c
View File

@ -25,144 +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, RXI_LOGC_DEFAULT_LEVEL, false, {0},
static const char *level_strings[] = {
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
}; };
static const char *level_strings[] = {"[[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[36m", "\x1b[94m", "\x1b[32m",
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" "\x1b[33m", "\x1b[31m", "\x1b[35m"};
};
#endif
static inline const char *get_level_color(int level) {
return level_colors[(level + 32) / 32];
}
#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%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", get_level_color(ev->level), get_level_string(ev->level), ev->file,
buf, level_colors[ev->level], level_strings[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 %-5s %s:%d: ", ev->file, ev->line);
buf, level_strings[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 %-5s %s:%d: ", ev->file, ev->line);
buf, level_strings[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);
}
} }
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;
} }
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); ev->time = 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

@ -15,35 +15,78 @@
#define LOG_VERSION "0.1.0" #define LOG_VERSION "0.1.0"
#ifndef RXI_LOGC_DEFAULT_LEVEL
#define RXI_LOGC_DEFAULT_LEVEL LOG_TRACE
#endif
#if defined __GNUC__
#define RXI_LOGC_PRINTF_ATTRIB(n, m) __attribute__((format(printf, n, m)))
#else
#define RXI_LOGC_PRINTF_ATTRIB(n, m)
#endif
enum {
LOG_DEBUG = -32,
LOG_TRACE = 0,
LOG_INFO = 32,
LOG_WARN = 64,
LOG_ERROR = 96,
LOG_FATAL = 128,
};
typedef struct { typedef struct {
va_list ap; va_list ap;
const char *fmt; const char *fmt;
const char *file; const char *file;
struct tm *time; struct tm *time;
void *udata; struct tm time_buf;
int line; void *udata;
int level; int line;
int level;
} log_Event; } log_Event;
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);
enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL }; #define log_debug(...) \
do { \
if (LOG_DEBUG >= RXI_LOGC_DEFAULT_LEVEL) \
log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
#define log_trace(...) \
do { \
if (LOG_TRACE >= RXI_LOGC_DEFAULT_LEVEL) \
log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
#define log_info(...) \
do { \
if (LOG_INFO >= RXI_LOGC_DEFAULT_LEVEL) \
log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
#define log_warn(...) \
do { \
if (LOG_WARN >= RXI_LOGC_DEFAULT_LEVEL) \
log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
#define log_error(...) \
do { \
if (LOG_ERROR >= RXI_LOGC_DEFAULT_LEVEL) \
log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
#define log_fatal(...) \
do { \
if (LOG_FATAL >= RXI_LOGC_DEFAULT_LEVEL) \
log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
#define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) const char *log_level_string(int level);
#define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
#define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
#define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
#define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
const char* log_level_string(int level);
void log_set_lock(log_LockFn fn, void *udata); void log_set_lock(log_LockFn fn, void *udata);
void log_set_level(int level); void log_set_level(int level);
void log_set_quiet(bool enable); void log_set_quiet(bool enable);
int log_add_callback(log_LogFn fn, void *udata, int level); int log_add_callback(log_LogFn fn, void *udata, int level);
int log_add_fp(FILE *fp, int level); int log_add_fp(FILE *fp, int level);
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, ...)
RXI_LOGC_PRINTF_ATTRIB(4, 5);
#endif #endif