aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2025-04-14 16:58:04 +0200
committerFederico Angelilli <code@fedang.net>2025-04-14 16:58:04 +0200
commit790af4c6ff4f4202293667da8481f55f812e80b4 (patch)
treeeb612018fc490ccbed3fab41be859fe29c98aa67 /test
parentdad7221c16bb20925076c1925027a89beeb5fff9 (diff)
any_log v0.3.1
Diffstat (limited to 'test')
-rw-r--r--test/log.c46
1 files changed, 44 insertions, 2 deletions
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 <stdbool.h>
#include <stdint.h>
+#if __GNUC__
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <execinfo.h>
+#include <unistd.h>
+
+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;
}