From 790af4c6ff4f4202293667da8481f55f812e80b4 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Mon, 14 Apr 2025 16:58:04 +0200 Subject: any_log v0.3.1 --- test/log.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/log.c b/test/log.c index c376ae2..a795ef7 100644 --- a/test/log.c +++ b/test/log.c @@ -1,9 +1,42 @@ +#define _POSIX_SOURCE + #include #include +#if __GNUC__ + +#include +#include +#include +#include + +void print_backtrace(FILE *stream) +{ + int nptrs; + void *buffer[100]; + + nptrs = backtrace(buffer, 100); + fprintf(stream, "\nbacktrace() returned %d addresses\n", nptrs); + backtrace_symbols_fd(buffer, nptrs, fileno(stream)); +} + +#define ANY_LOG_PANIC_AFTER(stream, file, line, module, func) \ + do { \ + print_backtrace(stream); \ + fprintf(stream, "%spanic was invoked from%s %s:%d (module %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)); \ + } while (false) + +#endif + //#define ANY_LOG_LOCKING #define ANY_LOG_IMPLEMENT -#define ANY_LOG_MODULE "test" +//#define ANY_LOG_MODULE "test" + +#define STR2(x) #x +#define STR(x) STR2(x) +#define ANY_LOG_SOURCE __FILE__ ":" STR(__LINE__) // Print in a JSON like way @@ -64,6 +97,15 @@ void pairs_format(FILE *stream, struct pair *pairs) fprintf(stream, "]"); } +void adios(int x) +{ + if (x == 0) { + log_panic("Adios"); + } + + adios(x - 1); +} + int main() { any_log_init(ANY_LOG_TRACE, stdout); @@ -141,7 +183,7 @@ int main() log_warn("Hello"); log_error("Hello"); - log_panic("Adios"); + adios(10); return 0; } -- cgit v1.2.3