From 3827da65c5d4be55a2d3cb6d6a18c5e579a111b8 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Thu, 4 Feb 2021 10:25:53 +0100 Subject: [PATCH] LOG: Also display file in the log line --- inc/lektor/common.h | 13 +++++++------ src/base/common.c | 14 ++++++++++---- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/inc/lektor/common.h b/inc/lektor/common.h index 00f213ad..3bfe7e42 100644 --- a/inc/lektor/common.h +++ b/inc/lektor/common.h @@ -89,12 +89,13 @@ enum log_level { __LAST_UNUSED_LOG_LEVEL, /* Usefull to enable all by default */ }; extern enum log_level __log_level; -void __lkt_log(enum log_level, const char *section, const char *func, uint64_t line, const char *format, ...); -#define LOG_DEBUG(section, ...) __lkt_log(DEBUG, section, __func__, __LINE__, __VA_ARGS__) -#define LOG_INFO( section, ...) __lkt_log(INFO, section, __func__, __LINE__, __VA_ARGS__) -#define LOG_WARN( section, ...) __lkt_log(WARN, section, __func__, __LINE__, __VA_ARGS__) -#define LOG_ERROR(section, ...) __lkt_log(ERROR, section, __func__, __LINE__, __VA_ARGS__) -#define LOG_FATAL(...) { __lkt_log(ERROR, "FATAL", __func__, __LINE__, __VA_ARGS__); exit(1); } +void __lkt_log(enum log_level, const char *section, const char *func, const char *file, + uint64_t line, const char *format, ...); +#define LOG_DEBUG(sec, ...) __lkt_log(DEBUG, sec, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define LOG_INFO( sec, ...) __lkt_log(INFO, sec, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define LOG_WARN( sec, ...) __lkt_log(WARN, sec, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define LOG_ERROR(sec, ...) __lkt_log(ERROR, sec, __func__, __FILE__, __LINE__, __VA_ARGS__) +#define LOG_FATAL(...) { LOG_ERROR("FATAL", __VA_ARGS__); exit(1); } #define SELF_EXECUTABLE_LINUX "/proc/self/exe" #define SELF_EXECUTABLE_FREEBSD "/proc/curproc/file" diff --git a/src/base/common.c b/src/base/common.c index c297695b..3026d0fd 100644 --- a/src/base/common.c +++ b/src/base/common.c @@ -53,8 +53,8 @@ __set_assert(void) enum log_level __log_level = __LAST_UNUSED_LOG_LEVEL; /* All by default */ void -__lkt_log(enum log_level level, const char *section, const char *func, uint64_t line_number, - const char *format, ...) +__lkt_log(enum log_level level, const char *section, const char *func, const char *file, + uint64_t line_number, const char *format, ...) { char line[LKT_MESSAGE_MAX]; va_list ap; @@ -67,13 +67,19 @@ __lkt_log(enum log_level level, const char *section, const char *func, uint64_t sec = timeval->tm_sec; } + size_t skip_dir = 0; + do { + file += skip_dir + 1; + skip_dir = strcspn(file, "/"); + } while (file[skip_dir] && file[skip_dir + 1]); + if (level > __log_level) return; - safe_snprintf(line, LKT_MESSAGE_MAX, " %c [%02d:%02d:%02d] %-10s %s(+%ld): %s\n", + safe_snprintf(line, LKT_MESSAGE_MAX, " %c [%02d:%02d:%02d] %-10s %s(%s+%ld): %s\n", level == ERROR ? '!' : level == WARN ? '*' : level == INFO ? '.' : ' ', hour, min, sec, - section, func, line_number, format); + section, func, file, line_number, format); va_start(ap, format); vfprintf(stderr, line, ap); -- GitLab