Skip to content

Commit

Permalink
add assert log and abort actively when fatal log
Browse files Browse the repository at this point in the history
  • Loading branch information
MuggleWei committed Nov 19, 2023
1 parent 5133039 commit 49c9622
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
10 changes: 9 additions & 1 deletion example/hello/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
25 changes: 21 additions & 4 deletions haclog/haclog_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <assert.h>

HACLOG_EXTERN_C_BEGIN
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions haclog/haclog_stacktrace.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "haclog_stacktrace.h"
#include "haclog/haclog_sleep.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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
}

0 comments on commit 49c9622

Please sign in to comment.