add fullpath option for write file's path

This commit is contained in:
Allenhe123 2021-08-18 20:45:33 +08:00
parent f9ea34994b
commit 4dfcde69ef
2 changed files with 7 additions and 1 deletions

View File

@ -21,6 +21,7 @@
*/
#include "log.h"
#include <libgen.h>
#define MAX_CALLBACKS 32
@ -35,6 +36,7 @@ static struct {
log_LockFn lock;
int level;
bool quiet;
bool fullpath;
Callback callbacks[MAX_CALLBACKS];
} L;
@ -111,6 +113,9 @@ void log_set_quiet(bool enable) {
L.quiet = enable;
}
void log_set_fullpath(bool enable) {
L.fullpath = enable;
}
int log_add_callback(log_LogFn fn, void *udata, int level) {
for (int i = 0; i < MAX_CALLBACKS; i++) {
@ -140,7 +145,7 @@ static void init_event(log_Event *ev, void *udata) {
void log_log(int level, const char *file, int line, const char *fmt, ...) {
log_Event ev = {
.fmt = fmt,
.file = file,
.file = L.fullpath ? file : basename((char*)file),
.line = line,
.level = level,
};

View File

@ -41,6 +41,7 @@ const char* log_level_string(int level);
void log_set_lock(log_LockFn fn, void *udata);
void log_set_level(int level);
void log_set_quiet(bool enable);
void log_set_fullpath(bool enable);
int log_add_callback(log_LogFn fn, void *udata, int level);
int log_add_fp(FILE *fp, int level);