From a262dc8acd581052ee6b5d5430af69148540a843 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sat, 23 Aug 2025 00:01:31 +0200 Subject: Move to luajit and fix logs --- Makefile | 4 +- src/action.c | 2 +- src/any_log.h | 124 +++++++++++++++++++++++----------------------------------- src/block.c | 2 +- src/comet.c | 20 ++++++++++ src/config.c | 2 +- src/config.h | 2 +- src/display.c | 2 +- src/event.c | 2 +- src/format.c | 2 +- src/layout.c | 2 +- src/log.c | 37 ------------------ src/log.h | 12 ------ src/lua/api.c | 18 ++++++++- src/lua/api.h | 4 ++ src/lua/log.c | 29 ++++++-------- src/lua/log.h | 4 +- src/window.c | 2 +- 18 files changed, 116 insertions(+), 154 deletions(-) delete mode 100644 src/log.c delete mode 100644 src/log.h diff --git a/Makefile b/Makefile index c7c40e0..dff2ac6 100644 --- a/Makefile +++ b/Makefile @@ -11,13 +11,13 @@ PCDEP = xcb \ xcb-errors \ "xcb-randr >= 1.5" \ pangocairo \ - lua5.4 + luajit CFLAGS += $(shell pkg-config --cflags $(PCDEP)) -Wall -Werror LDFLAGS += $(shell pkg-config --libs $(PCDEP)) -lm ifdef RELEASE -CFLAGS += -O2 -D_RELEASE +CFLAGS += -O2 else CFLAGS += -ggdb endif diff --git a/src/action.c b/src/action.c index 29ec142..a09d7e5 100644 --- a/src/action.c +++ b/src/action.c @@ -6,7 +6,7 @@ #include "action.h" #include "block.h" -#include "log.h" +#include "any_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 bccb8bb..beeae37 100644 --- a/src/any_log.h +++ b/src/any_log.h @@ -1,4 +1,4 @@ -// any_log v0.3.2 +// any_log v0.4.0 // // A single-file library that provides a simple and somewhat opinionated // interface for logging and structured logging. @@ -52,19 +52,6 @@ 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). @@ -107,7 +94,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__, __LINE__, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_panic(...) any_log_panic(__FILE__, __LINE__, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) // log_[level] provide normal printf style logging. // @@ -124,20 +111,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_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__) +#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__) #ifdef ANY_LOG_NO_DEBUG #define log_debug(...) #else -#define log_debug(...) any_log_format(ANY_LOG_DEBUG, ANY_LOG_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_debug(...) any_log_format(ANY_LOG_DEBUG, 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_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) +#define log_trace(...) any_log_format(ANY_LOG_TRACE, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__) #endif // log_value_[level] provide structured logging. @@ -197,7 +184,7 @@ typedef enum { // #define ANY_LOG_IMPLEMENT // #define ANY_LOG_NO_GENERIC // -// #define ANY_LOG_VALUE_BEFORE(stream, level, context, module, func, message) +// #define ANY_LOG_VALUE_BEFORE(stream, level, module, func, message) // fprintf(stream, "{\"module\": \"%s\", \"function\": \"%s\", \"level\": \"%s\", \"message\": \"%s\", ", // module, func, any_log_level_strings[level], message) // @@ -225,7 +212,7 @@ typedef enum { // #define ANY_LOG_VALUE_STRING(stream, key, value) // fprintf(stream, "\"%s \": \"%s\"", key, value) // -// #define ANY_LOG_VALUE_AFTER(stream, level, context, module, func, message) +// #define ANY_LOG_VALUE_AFTER(stream, level, module, func, message) // fprintf(stream, "}\n") // // #include "any_log.h" @@ -233,21 +220,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_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) +#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) #ifdef ANY_LOG_NO_DEBUG #define log_value_debug(...) #else -#define log_value_debug(...) any_log_value(ANY_LOG_DEBUG, 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_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_CONTEXT, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) +#define log_value_trace(...) any_log_value(ANY_LOG_TRACE, ANY_LOG_MODULE, ANY_LOG_FUNC, __VA_ARGS__, (char *)NULL) #endif #ifndef ANY_LOG_NO_GENERIC @@ -378,7 +365,6 @@ 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, @@ -405,20 +391,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, 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(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(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_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_NORETURN -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, ...); +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, ...); #endif @@ -529,9 +515,6 @@ 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 @@ -546,14 +529,13 @@ const char *any_log_colors_enabled[ANY_LOG_COLOR_ALL] = { 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 @@ -566,9 +548,8 @@ 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, 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 ? " " : "", \ +#define ANY_LOG_FORMAT_BEFORE(stream, level, module, func) \ + fprintf(stream, "[%s%s%s %s%s%s] %s%s%s: ", \ 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)) @@ -576,12 +557,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, context, module, func) \ +#define ANY_LOG_FORMAT_AFTER(stream, level, module, func) \ fprintf(stream, "\n") #endif -void any_log_format(any_log_level_t level, const char *context, - const char *module, const char *func, const char *format, ...) +void any_log_format(any_log_level_t level, const char *module, + const char *func, const char *format, ...) { if (level > any_log_level) return; @@ -589,21 +570,20 @@ void any_log_format(any_log_level_t level, const char *context, FILE *stream = any_log_streams[level]; ANY_LOG_FLOCK(stream); - ANY_LOG_FORMAT_BEFORE(stream, level, context, module, func); + ANY_LOG_FORMAT_BEFORE(stream, level, module, func); va_list args; va_start(args, format); vfprintf(stream, format, args); va_end(args); - ANY_LOG_FORMAT_AFTER(stream, level, context, module, func); + ANY_LOG_FORMAT_AFTER(stream, level, 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; } @@ -617,9 +597,8 @@ void any_log_format(any_log_level_t level, const char *context, // Format for any_log_value (used at the start) #ifndef ANY_LOG_VALUE_BEFORE -#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 ? " " : "", \ +#define ANY_LOG_VALUE_BEFORE(stream, level, module, func, message) \ + fprintf(stream, "[%s%s%s %s%s%s] %s%s%s: %s [", \ 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) @@ -627,7 +606,7 @@ void any_log_format(any_log_level_t level, const char *context, // Format for any_log_value (used at the end) #ifndef ANY_LOG_VALUE_AFTER -#define ANY_LOG_VALUE_AFTER(stream, level, context, module, func, message) \ +#define ANY_LOG_VALUE_AFTER(stream, level, module, func, message) \ fprintf(stream, "]\n") #endif @@ -704,8 +683,8 @@ void any_log_format(any_log_level_t level, const char *context, // 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 *context, - const char *module, const char *func, const char *message, ...) +void any_log_value(any_log_level_t level, const char *module, + const char *func, const char *message, ...) { if (level > any_log_level) return; @@ -713,7 +692,7 @@ void any_log_value(any_log_level_t level, const char *context, FILE *stream = any_log_streams[level]; ANY_LOG_FLOCK(stream); - ANY_LOG_VALUE_BEFORE(stream, level, context, module, func, message); + ANY_LOG_VALUE_BEFORE(stream, level, module, func, message); va_list args; va_start(args, message); @@ -793,10 +772,9 @@ tdefault:; va_end(args); - ANY_LOG_VALUE_AFTER(stream, level, context, module, func, message); + ANY_LOG_VALUE_AFTER(stream, level, module, func, message); ANY_LOG_FUNLOCK(stream); - (void)context; (void)module; (void)func; (void)message; @@ -810,14 +788,13 @@ tdefault:; // NOTE: This function should never return! // #ifndef ANY_LOG_EXIT -#define ANY_LOG_EXIT(file, line, context, module, func) abort() +#define ANY_LOG_EXIT(file, line, 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, 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 ? " " : "", \ +#define ANY_LOG_PANIC_BEFORE(stream, file, line, module, func) \ + fprintf(stream, "[%s%s%s %s%s%s] %s%s%s: ", \ 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)) @@ -825,7 +802,7 @@ tdefault:; // Format for any_log_panic (used at the end) #ifndef ANY_LOG_PANIC_AFTER -#define ANY_LOG_PANIC_AFTER(stream, file, line, context, module, func) \ +#define ANY_LOG_PANIC_AFTER(stream, file, line, 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)) @@ -834,30 +811,29 @@ 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 *context, - const char *module, const char *func, const char *format, ...) +void any_log_panic(const char *file, int line, 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, context, module, func); + ANY_LOG_PANIC_BEFORE(stream, file, line, module, func); va_list args; va_start(args, format); vfprintf(stream, format, args); va_end(args); - ANY_LOG_PANIC_AFTER(stream, file, line, context, module, func); + ANY_LOG_PANIC_AFTER(stream, file, line, module, func); ANY_LOG_FUNLOCK(stream); (void)file; (void)line; - (void)context; (void)module; (void)func; - ANY_LOG_EXIT(file, line, context, module, func); + ANY_LOG_EXIT(file, line, module, func); // In a way or another, this function shall not return abort(); diff --git a/src/block.c b/src/block.c index c909c59..42a6ebc 100644 --- a/src/block.c +++ b/src/block.c @@ -5,7 +5,7 @@ #include "util.h" #include "action.h" #include "config.h" -#include "log.h" +#include "any_log.h" extern const block_scheme_t block_text_scheme; diff --git a/src/comet.c b/src/comet.c index b418c91..1cf5d69 100644 --- a/src/comet.c +++ b/src/comet.c @@ -11,6 +11,26 @@ #include "block.h" #include "lua/api.h" +#define ANY_LOG_FORMAT_BEFORE(stream, level, module, func) \ + fprintf(stream, "[%s%s%s%s%s%s%s] %s%s%s: ", \ + 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, module, func, message) \ + fprintf(stream, "[%s%s%s%s%s%s%s] %s%s%s: %s [", \ + 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) + +#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 "any_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 8e923e0..86d5ac2 100644 --- a/src/config.c +++ b/src/config.c @@ -5,7 +5,7 @@ #include #include -#include "log.h" +#include "any_log.h" #include "config.h" #include "util.h" #include "block.h" diff --git a/src/config.h b/src/config.h index bb8bf83..c6e5433 100644 --- a/src/config.h +++ b/src/config.h @@ -5,7 +5,7 @@ #include #include "util.h" -#include "log.h" +#include "any_log.h" typedef struct block block_t; diff --git a/src/display.c b/src/display.c index 75937a7..cdde9cd 100644 --- a/src/display.c +++ b/src/display.c @@ -3,7 +3,7 @@ #include #include -#include "log.h" +#include "any_log.h" #include "display.h" void display_init(display_t *display) diff --git a/src/event.c b/src/event.c index a85eeb9..db00e30 100644 --- a/src/event.c +++ b/src/event.c @@ -1,6 +1,6 @@ #include -#include "log.h" +#include "any_log.h" #include "util.h" #include "layout.h" #include "event.h" diff --git a/src/format.c b/src/format.c index 54026f0..73d645e 100644 --- a/src/format.c +++ b/src/format.c @@ -4,7 +4,7 @@ #include #include "format.h" -#include "log.h" +#include "any_log.h" #include "util.h" #define format_grow(need) \ diff --git a/src/layout.c b/src/layout.c index 76e1007..e45526a 100644 --- a/src/layout.c +++ b/src/layout.c @@ -3,7 +3,7 @@ #include #include -#include "log.h" +#include "any_log.h" #include "layout.h" #include "block.h" diff --git a/src/log.c b/src/log.c deleted file mode 100644 index b41c0ab..0000000 --- a/src/log.c +++ /dev/null @@ -1,37 +0,0 @@ -#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 deleted file mode 100644 index bbd5301..0000000 --- a/src/log.h +++ /dev/null @@ -1,12 +0,0 @@ -#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 e2fdd30..47194ad 100644 --- a/src/lua/api.c +++ b/src/lua/api.c @@ -4,12 +4,26 @@ bool lua_api_init(lua_api_t *lua) { lua->state = luaL_newstate(); + if (!lua->state) + return false; + luaL_openlibs(lua->state); - luaL_requiref(lua->state, "log", lua_log_library, 1); - lua_pop(lua->state, 1); + + lua_api_require(lua, "log", lua_log_library); return true; } +void lua_api_require(lua_api_t *lua, const char *name, lua_library_t lib_fn) +{ + lua_pushcfunction(lua->state, lib_fn); + lua_pushstring(lua->state, name); + lua_call(lua->state, 1, 1); + + lua_pushvalue(lua->state, -1); + lua_setglobal(lua->state, name); + lua_pop(lua->state, 1); +} + void lua_api_close(lua_api_t *lua) { lua_close(lua->state); diff --git a/src/lua/api.h b/src/lua/api.h index e817fa1..7b5e742 100644 --- a/src/lua/api.h +++ b/src/lua/api.h @@ -10,8 +10,12 @@ typedef struct { lua_State *state; } lua_api_t; +typedef int (*lua_library_t)(lua_State *state); + bool lua_api_init(lua_api_t *lua); +void lua_api_require(lua_api_t *lua, const char *name, lua_library_t lib_fn); + void lua_api_close(lua_api_t *lua); #endif diff --git a/src/lua/log.c b/src/lua/log.c index 2b6d068..ee85369 100644 --- a/src/lua/log.c +++ b/src/lua/log.c @@ -18,12 +18,7 @@ static void lua_log_debuginfo(lua_State *state, const char **module, const char lua_pushnumber(state, 2); // S: source, l: currentline, n: name - const char *query = -#ifdef _RELEASE - "Sl"; -#else - "Sln"; -#endif + const char *query = "Sln"; lua_pushstring(state, query); if (lua_pcall(state, 2, 1, 0) == LUA_OK) { @@ -33,21 +28,21 @@ static void lua_log_debuginfo(lua_State *state, const char **module, const char 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); + + *func = buffer; + *module = "lua"; } else { snprintf(buffer, size, "%s:%d", shortsrc, line); - } - *module = buffer; + *func = name ? name : "?"; + *module = buffer; + } } else { log_trace("Failed to retrieve Lua debug information"); } @@ -70,11 +65,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); - const char *module = "?", *func = ""; + const char *module, *func; char buffer[4096]; lua_log_debuginfo(state, &module, &func, buffer, sizeof(buffer)); - any_log_format(level, "lua", module, func, "%s", message); + any_log_format(level, module, func, "%s", message); return 0; } @@ -122,11 +117,11 @@ static int lua_log_wrapperv(lua_State *state, any_log_level_t level) lua_remove(state, 1); luaL_checktype(state, 1, LUA_TTABLE); - const char *module = "?", *func = ""; + const char *module, *func; char buffer[4096]; lua_log_debuginfo(state, &module, &func, buffer, sizeof(buffer)); - any_log_value(level, "lua", module, func, message, + any_log_value(level, module, func, message, "g:value", ANY_LOG_FORMATTER(lua_print_value), state, NULL); return 0; @@ -206,7 +201,7 @@ void lua_print_value_at(FILE *stream, lua_State *state, int index) first = false; if (lua_isnumber(state, -2)) { - fprintf(stream, "[%lld] = ", lua_tointeger(state, -2)); + fprintf(stream, "[%ld] = ", lua_tointeger(state, -2)); } else { fprintf(stream, "%s = ", lua_tostring(state, -2)); } diff --git a/src/lua/log.h b/src/lua/log.h index 32c235b..a736355 100644 --- a/src/lua/log.h +++ b/src/lua/log.h @@ -2,12 +2,14 @@ #define COMET_LUA_LOG_H #include "api.h" -#include "../log.h" +#include "../any_log.h" // Print the first value on the stack // void lua_print_value(FILE *stream, lua_State *state); +// Print the index-th value on the stack +// void lua_print_value_at(FILE *stream, lua_State *state, int index); int lua_log_library(lua_State *state); diff --git a/src/window.c b/src/window.c index 911fcbb..de22814 100644 --- a/src/window.c +++ b/src/window.c @@ -15,7 +15,7 @@ #include #include -#include "log.h" +#include "any_log.h" #include "window.h" static void wm_set_size(window_t *window) -- cgit v1.2.3