From 49c9622812a1deba46a8bc948c9e88e5d6276e60 Mon Sep 17 00:00:00 2001 From: Muggle Wei Date: Sun, 19 Nov 2023 13:20:48 +0800 Subject: [PATCH] add assert log and abort actively when fatal log --- example/hello/main.c | 10 +++++++++- haclog/haclog_log.h | 25 +++++++++++++++++++++---- haclog/haclog_stacktrace.c | 8 +++++--- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/example/hello/main.c b/example/hello/main.c index b6eb0a7..bda159f 100644 --- a/example/hello/main.c +++ b/example/hello/main.c @@ -78,7 +78,15 @@ int main() LOG_INFO("info message"); LOG_WARNING("warning message"); LOG_ERROR("error message"); - LOG_FATAL("fatal message"); + + // NOTE: will crash when build type is DEBUG + LOG_FATAL("fatal message\n" + "NOTE: fatal log crash when build type is DEBUG"); + int actual = 0; + int expect = 1; + HACLOG_ASSERT(actual == expect); + HACLOG_ASSERT_MSG(actual == expect, "actual: %d, expect: %d", actual, + expect); LOG_INFO("bye"); diff --git a/haclog/haclog_log.h b/haclog/haclog_log.h index 92689f6..4867f63 100644 --- a/haclog/haclog_log.h +++ b/haclog/haclog_log.h @@ -15,6 +15,7 @@ #include "haclog/haclog_vsprintf.h" #include "haclog/haclog_thread_context.h" #include "haclog/handler/haclog_handler.h" +#include "haclog/haclog_stacktrace.h" #include HACLOG_EXTERN_C_BEGIN @@ -85,8 +86,23 @@ static_assert(0, "haclog can't find c or c++ version"); HACLOG_LOG_DEFAULT(HACLOG_LEVEL_WARNING, format, ##__VA_ARGS__) #define HACLOG_ERROR(format, ...) \ HACLOG_LOG_DEFAULT(HACLOG_LEVEL_ERROR, format, ##__VA_ARGS__) -#define HACLOG_FATAL(format, ...) \ - HACLOG_LOG_DEFAULT(HACLOG_LEVEL_FATAL, format, ##__VA_ARGS__) +#define HACLOG_FATAL(format, ...) \ + HACLOG_LOG_DEFAULT(HACLOG_LEVEL_FATAL, format, ##__VA_ARGS__); \ + haclog_debug_break() + +#define HACLOG_ASSERT(x) \ + do { \ + if (!(x)) { \ + HACLOG_FATAL("Assertion: " #x); \ + } \ + } while (0) + +#define HACLOG_ASSERT_MSG(x, format, ...) \ + do { \ + if (!(x)) { \ + HACLOG_FATAL("Assertion: " #x ", " format, ##__VA_ARGS__); \ + } \ + } while (0) #if HACLOG_HOLD_LOG_MACRO @@ -100,8 +116,9 @@ static_assert(0, "haclog can't find c or c++ version"); HACLOG_LOG_DEFAULT(HACLOG_LEVEL_WARNING, format, ##__VA_ARGS__) #define LOG_ERROR(format, ...) \ HACLOG_LOG_DEFAULT(HACLOG_LEVEL_ERROR, format, ##__VA_ARGS__) - #define LOG_FATAL(format, ...) \ - HACLOG_LOG_DEFAULT(HACLOG_LEVEL_FATAL, format, ##__VA_ARGS__) + #define LOG_FATAL(format, ...) \ + HACLOG_LOG_DEFAULT(HACLOG_LEVEL_FATAL, format, ##__VA_ARGS__); \ + haclog_debug_break() #define LOG_LEVEL_TRACE HACLOG_LEVEL_TRACE #define LOG_LEVEL_DEBUG HACLOG_LEVEL_DEBUG diff --git a/haclog/haclog_stacktrace.c b/haclog/haclog_stacktrace.c index ce91c50..d25ce18 100644 --- a/haclog/haclog_stacktrace.c +++ b/haclog/haclog_stacktrace.c @@ -1,4 +1,5 @@ #include "haclog_stacktrace.h" +#include "haclog/haclog_sleep.h" #include #include #include @@ -61,10 +62,11 @@ void haclog_print_stacktrace() void haclog_debug_break() { #if !defined(NDEBUG) - haclog_print_stacktrace(); + haclog_nsleep(5 * 1000 * 1000); + haclog_print_stacktrace(); #if HACLOG_PLATFORM_WINDOWS - __debugbreak(); + __debugbreak(); #endif - assert(0); + abort(); #endif }