Skip to content

Commit

Permalink
logger: Log only file name and change the log format a bit
Browse files Browse the repository at this point in the history
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Aug 4, 2024
1 parent 14cf1b2 commit 82eeb9b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
14 changes: 8 additions & 6 deletions include/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,23 @@ extern "C" {

#if (defined(_WIN32) || defined(_WIN64))
#include <io.h>
#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))
#define __weak __attribute__((weak))
#define __unreachable() __builtin_unreachable()
#define likely(p) __builtin_expect(!!(p), 1)
#define unlikely(p) __builtin_expect(!!(p), 0)
#define PATH_SEPARATOR '/'
#endif

/**
Expand Down
28 changes: 5 additions & 23 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down

0 comments on commit 82eeb9b

Please sign in to comment.