diff options
| author | Federico Angelilli <code@fedang.net> | 2025-04-14 20:02:53 +0200 |
|---|---|---|
| committer | Federico Angelilli <code@fedang.net> | 2025-04-14 22:12:58 +0200 |
| commit | efdf4a4883937c8a2fad278fbd4ffc207ef981dc (patch) | |
| tree | 061e90f703ebd2b447e9b12d39c5868a972ceb4a /src | |
| parent | dc9e9c06421eb9156baf425ac14ec8f50cb8b5ae (diff) | |
Update logging
Diffstat (limited to 'src')
| -rw-r--r-- | src/action.c | 2 | ||||
| -rw-r--r-- | src/any_log.h | 151 | ||||
| -rw-r--r-- | src/block.c | 2 | ||||
| -rw-r--r-- | src/comet.c | 16 | ||||
| -rw-r--r-- | src/config.c | 2 | ||||
| -rw-r--r-- | src/config.h | 2 | ||||
| -rw-r--r-- | src/display.c | 2 | ||||
| -rw-r--r-- | src/event.c | 2 | ||||
| -rw-r--r-- | src/format.c | 2 | ||||
| -rw-r--r-- | src/layout.c | 2 | ||||
| -rw-r--r-- | src/log.c | 37 | ||||
| -rw-r--r-- | src/log.h | 12 | ||||
| -rw-r--r-- | src/lua/api.c | 3 | ||||
| -rw-r--r-- | src/lua/log.c | 34 | ||||
| -rw-r--r-- | src/window.c | 2 |
15 files changed, 176 insertions, 95 deletions
diff --git a/src/action.c b/src/action.c index a09d7e5..29ec142 100644 --- a/src/action.c +++ b/src/action.c @@ -6,7 +6,7 @@ #include "action.h" #include "block.h" -#include "any_log.h" +#include "log.h" bool action_perform(action_t *action, block_t *block, config_t *config) { diff --git a/src/any_log.h b/src/any_log.h index 4d4dc17..bccb8bb 100644 --- a/src/any_log.h +++ b/src/any_log.h @@ -1,4 +1,4 @@ -// any_log v0.3.1 +// any_log v0.3.2 // // A single-file library that provides a simple and somewhat opinionated // interface for logging and structured logging. @@ -52,6 +52,19 @@ typedef enum { ANY_LOG_ALL, } any_log_level_t; +// The value of ANY_LOG_CONTEXT should be used to pass some +// extra information or context to the logging functions. +// By default it is empty. +// +// Before including the header simply define your custom ANY_LOG_CONTEXT. +// +// #define ANY_LOG_CONTEXT get_thread_name() +// #include "any_log.h" +// +#ifndef ANY_LOG_CONTEXT +#define ANY_LOG_CONTEXT "" +#endif + // The value of ANY_LOG_MODULE is used to indicate the current module. // By default it is defined as __FILE__, which should expand to the // source file path (relative to the compiler cwd). @@ -62,19 +75,25 @@ typedef enum { // #define ANY_LOG_MODULE "my-library" // #include "any_log.h" // +// If you want to also include the current line, you could do something +// like the following: +// +// #define STR2(x) #x +// #define STR(x) STR2(x) +// #define ANY_LOG_MODULE __FILE__ ":" STR(__LINE__) +// #include "any_log.h" +// #ifndef ANY_LOG_MODULE #define ANY_LOG_MODULE __FILE__ #endif -// C99 and later define the __func__ variable +// C99 and later defines the __func__ variable to hold the name of +// the current function. +// #ifndef ANY_LOG_FUNC #define ANY_LOG_FUNC __func__ #endif -#ifndef ANY_LOG_LINE -#define ANY_LOG_LINE __LINE__ -#endif - // log_panic is implemented with the function any_log_panic, which takes // some extra parameters compared with the other log levels. This way we can // include as many information as possible for identifying fatal errors. @@ -88,7 +107,7 @@ typedef enum { // NOTE: log_panic will always terminate the program and should be used only // for non recoverable situations! For normal errors just use log_error. // -#define log_panic(...) any_log_panic(__FILE__, ANY_LOG_LINE, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_panic(...) any_log_panic(__FILE__, __LINE__, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) // log_[level] provide normal printf style logging. // @@ -105,20 +124,20 @@ typedef enum { // respectively. As this will work only if they are defined before every header // include, it is recommended to define this from the compiler. // -#define log_error(...) any_log_format(ANY_LOG_ERROR, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) -#define log_warn(...) any_log_format(ANY_LOG_WARN, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) -#define log_info(...) any_log_format(ANY_LOG_INFO, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_error(...) any_log_format(ANY_LOG_ERROR, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_warn(...) any_log_format(ANY_LOG_WARN, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_info(...) any_log_format(ANY_LOG_INFO, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) #ifdef ANY_LOG_NO_DEBUG #define log_debug(...) #else -#define log_debug(...) any_log_format(ANY_LOG_DEBUG, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_debug(...) any_log_format(ANY_LOG_DEBUG, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) #endif #ifdef ANY_LOG_NO_TRACE #define log_trace(...) #else -#define log_trace(...) any_log_format(ANY_LOG_TRACE, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_trace(...) any_log_format(ANY_LOG_TRACE, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) #endif // log_value_[level] provide structured logging. @@ -178,7 +197,7 @@ typedef enum { // #define ANY_LOG_IMPLEMENT // #define ANY_LOG_NO_GENERIC // -// #define ANY_LOG_VALUE_BEFORE(stream, level, module, func, message) +// #define ANY_LOG_VALUE_BEFORE(stream, level, context, module, func, message) // fprintf(stream, "{\"module\": \"%s\", \"function\": \"%s\", \"level\": \"%s\", \"message\": \"%s\", ", // module, func, any_log_level_strings[level], message) // @@ -206,7 +225,7 @@ typedef enum { // #define ANY_LOG_VALUE_STRING(stream, key, value) // fprintf(stream, "\"%s \": \"%s\"", key, value) // -// #define ANY_LOG_VALUE_AFTER(stream, level, module, func, message) +// #define ANY_LOG_VALUE_AFTER(stream, level, context, module, func, message) // fprintf(stream, "}\n") // // #include "any_log.h" @@ -214,21 +233,21 @@ typedef enum { // As with log_trace and log_debug, log_value_trace and log_value_debug can be // disabled by defining ANY_LOG_NO_TRACE and ANY_LOG_NO_DEBUG respectively. // -#define log_value_error(...) any_log_value(ANY_LOG_ERROR, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) -#define log_value_warn(...) any_log_value(ANY_LOG_WARN, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) -#define log_value_info(...) any_log_value(ANY_LOG_INFO, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) -#define log_value_debug(...) any_log_value(ANY_LOG_DEBUG, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) +#define log_value_error(...) any_log_value(ANY_LOG_ERROR, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) +#define log_value_warn(...) any_log_value(ANY_LOG_WARN, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) +#define log_value_info(...) any_log_value(ANY_LOG_INFO, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) +#define log_value_debug(...) any_log_value(ANY_LOG_DEBUG, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) #ifdef ANY_LOG_NO_DEBUG #define log_value_debug(...) #else -#define log_value_debug(...) any_log_value(ANY_LOG_DEBUG, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) +#define log_value_debug(...) any_log_value(ANY_LOG_DEBUG, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) #endif #ifdef ANY_LOG_NO_TRACE #define log_value_trace(...) #else -#define log_value_trace(...) any_log_value(ANY_LOG_TRACE, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) +#define log_value_trace(...) any_log_value(ANY_LOG_TRACE, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) #endif #ifndef ANY_LOG_NO_GENERIC @@ -359,6 +378,7 @@ typedef enum { ANY_LOG_COLOR_INFO, ANY_LOG_COLOR_DEBUG, ANY_LOG_COLOR_TRACE, + ANY_LOG_COLOR_CONTEXT, ANY_LOG_COLOR_MODULE, ANY_LOG_COLOR_FUNC, ANY_LOG_COLOR_RESET, @@ -385,20 +405,20 @@ extern const char *any_log_colors_disabled[ANY_LOG_COLOR_ALL]; // NOTE: You should never call the functions below directly! // See the above explanations on how to use logging. -ANY_LOG_ATTRIBUTE(format(printf, 4, 5)) -ANY_LOG_ATTRIBUTE(nonnull(2, 3, 4)) -void any_log_format(any_log_level_t level, const char *module, - const char *func, const char *format, ...); +ANY_LOG_ATTRIBUTE(format(printf, 5, 6)) +ANY_LOG_ATTRIBUTE(nonnull(2, 3, 4, 5)) +void any_log_format(any_log_level_t level, const char *context, + const char *module, const char *func, const char *format, ...); -ANY_LOG_ATTRIBUTE(nonnull(2, 3, 4)) -void any_log_value(any_log_level_t level, const char *module, - const char *func, const char *message, ...); +ANY_LOG_ATTRIBUTE(nonnull(2, 3, 4, 5)) +void any_log_value(any_log_level_t level, const char *context, + const char *module, const char *func, const char *message, ...); ANY_LOG_NORETURN -ANY_LOG_ATTRIBUTE(format(printf, 5, 6)) -ANY_LOG_ATTRIBUTE(nonnull(1, 3, 4, 5)) -void any_log_panic(const char *file, int line, const char *module, - const char *func, const char *format, ...); +ANY_LOG_ATTRIBUTE(format(printf, 6, 7)) +ANY_LOG_ATTRIBUTE(nonnull(1, 3, 4, 5, 6)) +void any_log_panic(const char *file, int line, const char *context, + const char *module, const char *func, const char *format, ...); #endif @@ -509,6 +529,9 @@ const char **any_log_colors = any_log_colors_enabled; #ifndef ANY_LOG_COLOR_RESET_DEFAULT #define ANY_LOG_COLOR_RESET_DEFAULT "\x1b[0m" #endif +#ifndef ANY_LOG_COLOR_CONTEXT_DEFAULT +#define ANY_LOG_COLOR_CONTEXT_DEFAULT "" +#endif #ifndef ANY_LOG_COLOR_MODULE_DEFAULT #define ANY_LOG_COLOR_MODULE_DEFAULT "" #endif @@ -516,20 +539,21 @@ const char **any_log_colors = any_log_colors_enabled; #define ANY_LOG_COLOR_FUNC_DEFAULT "\x1b[1m" #endif -const char *any_log_colors_enabled[ANY_LOG_ALL + 3] = { +const char *any_log_colors_enabled[ANY_LOG_COLOR_ALL] = { ANY_LOG_COLOR_PANIC_DEFAULT, ANY_LOG_COLOR_ERROR_DEFAULT, ANY_LOG_COLOR_WARN_DEFAULT, ANY_LOG_COLOR_INFO_DEFAULT, ANY_LOG_COLOR_DEBUG_DEFAULT, ANY_LOG_COLOR_TRACE_DEFAULT, + ANY_LOG_COLOR_CONTEXT_DEFAULT, ANY_LOG_COLOR_MODULE_DEFAULT, ANY_LOG_COLOR_FUNC_DEFAULT, ANY_LOG_COLOR_RESET_DEFAULT, }; const char *any_log_colors_disabled[ANY_LOG_COLOR_ALL] = { - "", "", "", "", "", "", "", "", "" + "", "", "", "", "", "", "", "", "", "", }; #else @@ -542,8 +566,9 @@ const char *any_log_colors_disabled[ANY_LOG_COLOR_ALL] = { // Format for any_log_format (used at the start) #ifndef ANY_LOG_FORMAT_BEFORE -#define ANY_LOG_FORMAT_BEFORE(stream, level, module, func) \ - fprintf(stream, "[%s%s%s %s%s%s] %s%s%s: ", \ +#define ANY_LOG_FORMAT_BEFORE(stream, level, context, module, func) \ + fprintf(stream, "[%s%s%s%s%s%s%s %s%s%s] %s%s%s: ", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_CONTEXT), context, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *context ? " " : "", \ ANY_LOG_COLOR_GET(ANY_LOG_COLOR_MODULE), module, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ ANY_LOG_COLOR_GET(ANY_LOG_COLOR_FUNC), func, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ ANY_LOG_COLOR_GET(level), any_log_level_strings[level], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET)) @@ -551,12 +576,12 @@ const char *any_log_colors_disabled[ANY_LOG_COLOR_ALL] = { // Format for any_log_format (used at the end) #ifndef ANY_LOG_FORMAT_AFTER -#define ANY_LOG_FORMAT_AFTER(stream, level, module, func) \ +#define ANY_LOG_FORMAT_AFTER(stream, level, context, module, func) \ fprintf(stream, "\n") #endif -void any_log_format(any_log_level_t level, const char *module, - const char *func, const char *format, ...) +void any_log_format(any_log_level_t level, const char *context, + const char *module, const char *func, const char *format, ...) { if (level > any_log_level) return; @@ -564,19 +589,21 @@ void any_log_format(any_log_level_t level, const char *module, FILE *stream = any_log_streams[level]; ANY_LOG_FLOCK(stream); - ANY_LOG_FORMAT_BEFORE(stream, level, module, func); + ANY_LOG_FORMAT_BEFORE(stream, level, context, module, func); va_list args; va_start(args, format); vfprintf(stream, format, args); va_end(args); - ANY_LOG_FORMAT_AFTER(stream, level, module, func); + ANY_LOG_FORMAT_AFTER(stream, level, context, module, func); ANY_LOG_FUNLOCK(stream); // NOTE: Suppress compiler warning if the user customizes the format string // and doesn't use these values in it + + (void)context; (void)module; (void)func; } @@ -590,8 +617,9 @@ void any_log_format(any_log_level_t level, const char *module, // Format for any_log_value (used at the start) #ifndef ANY_LOG_VALUE_BEFORE -#define ANY_LOG_VALUE_BEFORE(stream, level, module, func, message) \ - fprintf(stream, "[%s%s%s %s%s%s] %s%s%s: %s [", \ +#define ANY_LOG_VALUE_BEFORE(stream, level, context, module, func, message) \ + fprintf(stream, "[%s%s%s%s%s%s%s %s%s%s] %s%s%s: %s [", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_CONTEXT), context, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *context ? " " : "", \ ANY_LOG_COLOR_GET(ANY_LOG_COLOR_MODULE), module, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ ANY_LOG_COLOR_GET(ANY_LOG_COLOR_FUNC), func, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ ANY_LOG_COLOR_GET(level), any_log_level_strings[level], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), message) @@ -599,7 +627,7 @@ void any_log_format(any_log_level_t level, const char *module, // Format for any_log_value (used at the end) #ifndef ANY_LOG_VALUE_AFTER -#define ANY_LOG_VALUE_AFTER(stream, level, module, func, message) \ +#define ANY_LOG_VALUE_AFTER(stream, level, context, module, func, message) \ fprintf(stream, "]\n") #endif @@ -676,8 +704,8 @@ void any_log_format(any_log_level_t level, const char *module, // NOTE: This function should be called with at least one parameter after message. // The log_value_[level] macros automatically add a NULL. // -void any_log_value(any_log_level_t level, const char *module, - const char *func, const char *message, ...) +void any_log_value(any_log_level_t level, const char *context, + const char *module, const char *func, const char *message, ...) { if (level > any_log_level) return; @@ -685,7 +713,7 @@ void any_log_value(any_log_level_t level, const char *module, FILE *stream = any_log_streams[level]; ANY_LOG_FLOCK(stream); - ANY_LOG_VALUE_BEFORE(stream, level, module, func, message); + ANY_LOG_VALUE_BEFORE(stream, level, context, module, func, message); va_list args; va_start(args, message); @@ -765,9 +793,10 @@ tdefault:; va_end(args); - ANY_LOG_VALUE_AFTER(stream, level, module, func, message); + ANY_LOG_VALUE_AFTER(stream, level, context, module, func, message); ANY_LOG_FUNLOCK(stream); + (void)context; (void)module; (void)func; (void)message; @@ -781,22 +810,23 @@ tdefault:; // NOTE: This function should never return! // #ifndef ANY_LOG_EXIT -#define ANY_LOG_EXIT(file, line, module, func) abort() +#define ANY_LOG_EXIT(file, line, context, module, func) abort() #endif // Format for any_log_panic (used at the start) #ifndef ANY_LOG_PANIC_BEFORE -#define ANY_LOG_PANIC_BEFORE(stream, file, line, module, func) \ - fprintf(stream, "[%s%s%s %s%s%s] %s%s%s: ", \ +#define ANY_LOG_PANIC_BEFORE(stream, file, line, context, module, func) \ + fprintf(stream, "[%s%s%s%s%s%s%s %s%s%s] %s%s%s: ", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_CONTEXT), context, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *context ? " " : "", \ ANY_LOG_COLOR_GET(ANY_LOG_COLOR_MODULE), module, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ ANY_LOG_COLOR_GET(ANY_LOG_COLOR_FUNC), func, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ ANY_LOG_COLOR_GET(ANY_LOG_PANIC), any_log_level_strings[ANY_LOG_PANIC], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET)) #endif -// Format for any_log_panic (used at the start) +// Format for any_log_panic (used at the end) #ifndef ANY_LOG_PANIC_AFTER -#define ANY_LOG_PANIC_AFTER(stream, file, line, module, func) \ - fprintf(stream, "\n%spanic was invoked from%s %s:%d (module %s%s%s)\n", \ +#define ANY_LOG_PANIC_AFTER(stream, file, line, context, module, func) \ + fprintf(stream, "\n%spanic was invoked from%s %s:%d (%s%s%s)\n", \ ANY_LOG_COLOR_GET(ANY_LOG_PANIC), ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), file, line, \ ANY_LOG_COLOR_GET(ANY_LOG_COLOR_MODULE), module, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET)) #endif @@ -804,29 +834,30 @@ tdefault:; // NOTE: This function *exceptionally* gets more location information // because we want to be specific at least for fatal errors // -void any_log_panic(const char *file, int line, const char *module, - const char *func, const char *format, ...) +void any_log_panic(const char *file, int line, const char *context, + const char *module, const char *func, const char *format, ...) { FILE *stream = any_log_streams[ANY_LOG_PANIC]; ANY_LOG_FLOCK(stream); - ANY_LOG_PANIC_BEFORE(stream, file, line, module, func); + ANY_LOG_PANIC_BEFORE(stream, file, line, context, module, func); va_list args; va_start(args, format); vfprintf(stream, format, args); va_end(args); - ANY_LOG_PANIC_AFTER(stream, file, line, module, func); + ANY_LOG_PANIC_AFTER(stream, file, line, context, module, func); ANY_LOG_FUNLOCK(stream); - (void)module; - (void)func; (void)file; (void)line; + (void)context; + (void)module; + (void)func; - ANY_LOG_EXIT(file, line, module, func); + ANY_LOG_EXIT(file, line, context, module, func); // In a way or another, this function shall not return abort(); diff --git a/src/block.c b/src/block.c index 42a6ebc..c909c59 100644 --- a/src/block.c +++ b/src/block.c @@ -5,7 +5,7 @@ #include "util.h" #include "action.h" #include "config.h" -#include "any_log.h" +#include "log.h" extern const block_scheme_t block_text_scheme; diff --git a/src/comet.c b/src/comet.c index f74cfda..b418c91 100644 --- a/src/comet.c +++ b/src/comet.c @@ -11,22 +11,6 @@ #include "block.h" #include "lua/api.h" -#ifdef RELEASE -#define ANY_LOG_FORMAT_BEFORE(stream, level, module, func) \ - fprintf(stream, "[%s] %s%s%s: ", \ - func, ANY_LOG_COLOR_GET(level), any_log_level_strings[level], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET)) - -#define ANY_LOG_VALUE_BEFORE(stream, level, module, func, message) \ - fprintf(stream, "[%s] %s%s%s: %s [", \ - func, ANY_LOG_COLOR_GET(level), any_log_level_strings[level], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), message) -#endif - -#define ANY_LOG_VALUE_STRING(stream, key, value) \ - fprintf(stream, "%s=\"%s\"", key, value ? value : "(null)") - -#define ANY_LOG_IMPLEMENT -#include "log.h" - static sig_atomic_t running = true; static void signal_quit(int status) diff --git a/src/config.c b/src/config.c index 86d5ac2..8e923e0 100644 --- a/src/config.c +++ b/src/config.c @@ -5,7 +5,7 @@ #include <string.h> #include <assert.h> -#include "any_log.h" +#include "log.h" #include "config.h" #include "util.h" #include "block.h" diff --git a/src/config.h b/src/config.h index c6e5433..bb8bf83 100644 --- a/src/config.h +++ b/src/config.h @@ -5,7 +5,7 @@ #include <stdint.h> #include "util.h" -#include "any_log.h" +#include "log.h" typedef struct block block_t; diff --git a/src/display.c b/src/display.c index cdde9cd..75937a7 100644 --- a/src/display.c +++ b/src/display.c @@ -3,7 +3,7 @@ #include <assert.h> #include <string.h> -#include "any_log.h" +#include "log.h" #include "display.h" void display_init(display_t *display) diff --git a/src/event.c b/src/event.c index db00e30..a85eeb9 100644 --- a/src/event.c +++ b/src/event.c @@ -1,6 +1,6 @@ #include <assert.h> -#include "any_log.h" +#include "log.h" #include "util.h" #include "layout.h" #include "event.h" diff --git a/src/format.c b/src/format.c index 73d645e..54026f0 100644 --- a/src/format.c +++ b/src/format.c @@ -4,7 +4,7 @@ #include <assert.h> #include "format.h" -#include "any_log.h" +#include "log.h" #include "util.h" #define format_grow(need) \ diff --git a/src/layout.c b/src/layout.c index e45526a..76e1007 100644 --- a/src/layout.c +++ b/src/layout.c @@ -3,7 +3,7 @@ #include <string.h> #include <assert.h> -#include "any_log.h" +#include "log.h" #include "layout.h" #include "block.h" diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..b41c0ab --- /dev/null +++ b/src/log.c @@ -0,0 +1,37 @@ +#ifdef _RELEASE + +#define ANY_LOG_FORMAT_BEFORE(stream, level, context, module, func) \ + fprintf(stream, "[%s%s%s%s%s%s%s] %s%s%s: ", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_CONTEXT), context, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *context ? " " : "", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_MODULE), *func ? func : module, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ + ANY_LOG_COLOR_GET(level), any_log_level_strings[level], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET)) + +#define ANY_LOG_VALUE_BEFORE(stream, level, context, module, func, message) \ + fprintf(stream, "[%s%s%s%s%s%s%s] %s%s%s: %s [", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_CONTEXT), context, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *context ? " " : "", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_MODULE), *func ? func : module, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ + ANY_LOG_COLOR_GET(level), any_log_level_strings[level], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), message) +#else + +#define ANY_LOG_FORMAT_BEFORE(stream, level, context, module, func) \ + fprintf(stream, "[%s%s%s%s%s%s%s%s%s%s%s] %s%s%s: ", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_CONTEXT), context, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *context ? " " : "", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_MODULE), module, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *func ? " " : "", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_FUNC), func, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ + ANY_LOG_COLOR_GET(level), any_log_level_strings[level], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET)) + +#define ANY_LOG_VALUE_BEFORE(stream, level, context, module, func, message) \ + fprintf(stream, "[%s%s%s%s%s%s%s%s%s%s%s] %s%s%s: %s [", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_CONTEXT), context, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *context ? " " : "", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_MODULE), module, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), *func ? " " : "", \ + ANY_LOG_COLOR_GET(ANY_LOG_COLOR_FUNC), func, ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), \ + ANY_LOG_COLOR_GET(level), any_log_level_strings[level], ANY_LOG_COLOR_GET(ANY_LOG_COLOR_RESET), message) +#endif + +#define ANY_LOG_VALUE_STRING(stream, key, value) \ + fprintf(stream, "%s=\"%s\"", key, value ? value : "(null)") + +#define ANY_LOG_COLOR_CONTEXT_DEFAULT "\x1b[34m" + +#define ANY_LOG_IMPLEMENT +#include "log.h" diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..bbd5301 --- /dev/null +++ b/src/log.h @@ -0,0 +1,12 @@ +#ifndef COMET_LOG_H +#define COMET_LOG_H + +// Thin wrapper over any_log.h + +#ifdef RELEASE +#define ANY_LOG_NO_TRACE +#endif + +#include "any_log.h" + +#endif diff --git a/src/lua/api.c b/src/lua/api.c index 3f68b1e..e2fdd30 100644 --- a/src/lua/api.c +++ b/src/lua/api.c @@ -1,6 +1,5 @@ #include "api.h" - -int lua_log_library(lua_State *state); +#include "log.h" bool lua_api_init(lua_api_t *lua) { diff --git a/src/lua/log.c b/src/lua/log.c index 6811ec3..2b6d068 100644 --- a/src/lua/log.c +++ b/src/lua/log.c @@ -9,7 +9,8 @@ static lua_CFunction lua_log_stringformat = NULL; static lua_CFunction lua_log_tostring = NULL; -static inline void lua_log_debuginfo(lua_State *state, char buffer[], size_t size) +static void lua_log_debuginfo(lua_State *state, const char **module, const char **func, + char buffer[], size_t size) { lua_pushcfunction(state, lua_log_getinfo); @@ -17,7 +18,13 @@ static inline void lua_log_debuginfo(lua_State *state, char buffer[], size_t siz lua_pushnumber(state, 2); // S: source, l: currentline, n: name - lua_pushstring(state, "Sl"); + const char *query = +#ifdef _RELEASE + "Sl"; +#else + "Sln"; +#endif + lua_pushstring(state, query); if (lua_pcall(state, 2, 1, 0) == LUA_OK) { lua_getfield(state, -1, "short_src"); @@ -26,12 +33,21 @@ static inline void lua_log_debuginfo(lua_State *state, char buffer[], size_t siz lua_getfield(state, -2, "currentline"); int line = lua_tointeger(state, -1); +#ifndef _RELEASE + lua_getfield(state, -3, "name"); + const char *name = lua_tostring(state, -1); + if (name) + *func = name; +#endif + if (!strncmp(shortsrc, "[string ", 8)) { int length = strlen(shortsrc); snprintf(buffer, size, "%.*s", length - 9, shortsrc + 8); } else { snprintf(buffer, size, "%s:%d", shortsrc, line); } + + *module = buffer; } else { log_trace("Failed to retrieve Lua debug information"); } @@ -54,10 +70,11 @@ static int lua_log_wrapper(lua_State *state, int skip_args, any_log_level_t leve lua_call(state, n_args, 1); const char *message = lua_tostring(state, -1); - char buffer[4096] = { "lua?" }; - lua_log_debuginfo(state, buffer, sizeof(buffer)); + const char *module = "?", *func = ""; + char buffer[4096]; + lua_log_debuginfo(state, &module, &func, buffer, sizeof(buffer)); - any_log_format(level, "lua_api", buffer, "%s", message); + any_log_format(level, "lua", module, func, "%s", message); return 0; } @@ -105,10 +122,11 @@ static int lua_log_wrapperv(lua_State *state, any_log_level_t level) lua_remove(state, 1); luaL_checktype(state, 1, LUA_TTABLE); - char buffer[4096] = { "lua?" }; - lua_log_debuginfo(state, buffer, sizeof(buffer)); + const char *module = "?", *func = ""; + char buffer[4096]; + lua_log_debuginfo(state, &module, &func, buffer, sizeof(buffer)); - any_log_value(level, "lua_api", buffer, message, + any_log_value(level, "lua", module, func, message, "g:value", ANY_LOG_FORMATTER(lua_print_value), state, NULL); return 0; diff --git a/src/window.c b/src/window.c index de22814..911fcbb 100644 --- a/src/window.c +++ b/src/window.c @@ -15,7 +15,7 @@ #include <xcb/randr.h> #include <xcb/shape.h> -#include "any_log.h" +#include "log.h" #include "window.h" static void wm_set_size(window_t *window) |
