From 82eeb9b2c3b0f2ffba9c9044dc0a310843fed57c Mon Sep 17 00:00:00 2001 From: Siddharth Chandrasekaran Date: Sun, 4 Aug 2024 14:08:54 +0200 Subject: [PATCH] logger: Log only file name and change the log format a bit Signed-off-by: Siddharth Chandrasekaran --- include/utils/utils.h | 14 ++++++++------ src/logger.c | 28 +++++----------------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/include/utils/utils.h b/include/utils/utils.h index dd97f91..558a778 100644 --- a/include/utils/utils.h +++ b/include/utils/utils.h @@ -133,14 +133,15 @@ extern "C" { #if (defined(_WIN32) || defined(_WIN64)) #include +#define isatty _isatty /* from io.h */ +#define fileno _fileno #define __format_printf(x, y) -#define __noreturn __declspec(noreturn) +#define __noreturn __declspec(noreturn) #define __weak -#define __unreachable() __assume(0) -#define likely(p) (p) -#define unlikely(p) (p) -#define isatty _isatty /* from io.h */ -#define fileno _fileno +#define __unreachable() __assume(0) +#define likely(p) (p) +#define unlikely(p) (p) +#define PATH_SEPARATOR '\\' #else #define __format_printf(x, y) __attribute__((format(printf, x, y))) #define __noreturn __attribute__((noreturn)) @@ -148,6 +149,7 @@ extern "C" { #define __unreachable() __builtin_unreachable() #define likely(p) __builtin_expect(!!(p), 1) #define unlikely(p) __builtin_expect(!!(p), 0) +#define PATH_SEPARATOR '/' #endif /** diff --git a/src/logger.c b/src/logger.c index fee0761..e2f8418 100644 --- a/src/logger.c +++ b/src/logger.c @@ -72,24 +72,6 @@ static inline void logger_log_set_color(logger_t *ctx, const char *color) } } -static const char *get_rel_path(logger_t *ctx, const char *abs_path) -{ - const char *p, *q; - - if (!ctx->root_path || abs_path[0] != '/') - return abs_path; - - p = ctx->root_path; - q = abs_path; - while (*p != '\0' && *q != '\0' && *p == *q) { - p++; - q++; - } - while (*q != '\0' && *q == '/') - q++; - return q; -} - static const char *get_tstamp() { static char time_buf[24]; @@ -120,16 +102,16 @@ int __logger_log(logger_t *ctx, int log_level, const char *file, unsigned long l if (!ctx) ctx = &default_logger; + file = strrchr(file, PATH_SEPARATOR) + 1; if (!ctx->cb) { if (log_level < LOG_EMERG || log_level >= LOG_MAX_LEVEL || log_level > ctx->log_level) return 0; /* print module and log_level prefix */ - len = snprintf(buf, LOG_BUF_LEN, "%s: [%s] [%s] %s:%lu: ", - ctx->name, get_tstamp(), - log_level_names[log_level], - get_rel_path(ctx, file), line); + len = snprintf(buf, LOG_BUF_LEN, "%s: %s %11s:%-4lu [%s] ", + ctx->name, get_tstamp(), file, line, + log_level_names[log_level]); if (len > LOG_BUF_LEN) goto out; } @@ -149,7 +131,7 @@ int __logger_log(logger_t *ctx, int log_level, const char *file, unsigned long l len = terminate_log_line(buf, len); if (ctx->cb) { - ctx->cb(log_level, get_rel_path(ctx, file), line, buf); + ctx->cb(log_level, file, line, buf); } else { logger_log_set_color(ctx, log_level_colors[log_level]); if (ctx->file)