From 6104956a2766d47550e904e3fb7806b778bc2ea1 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 7 Dec 2023 15:37:54 +1100 Subject: [PATCH] [common] reformat the debug output to be more useful --- common/include/common/debug.h | 30 +++++++++++-------- common/src/debug.c | 46 ++++++++++++++++++++++++----- common/src/platform/linux/debug.c | 2 +- common/src/platform/windows/debug.c | 2 +- 4 files changed, 59 insertions(+), 21 deletions(-) diff --git a/common/include/common/debug.h b/common/include/common/debug.h index 1293266d..44cf3629 100644 --- a/common/include/common/debug.h +++ b/common/include/common/debug.h @@ -44,6 +44,9 @@ extern const char ** debug_lookup; void debug_init(void); +// platform specific debug initialization +void platform_debugInit(void); + #ifdef ENABLE_BACKTRACE void printBacktrace(void); #define DEBUG_PRINT_BACKTRACE() printBacktrace() @@ -65,6 +68,19 @@ void printBacktrace(void); #define DEBUG_UNREACHABLE_MARKER() #endif +void debug_level(enum DebugLevel level, const char * file, unsigned int line, + const char * function, const char * format, ...) + __attribute__((format (printf, 5, 6))); + +void debug_info(const char * file, unsigned int line, const char * function, + const char * format, ...) __attribute__((format (printf, 4, 5))); + +void debug_warn(const char * file, unsigned int line, const char * function, + const char * format, ...) __attribute__((format (printf, 4, 5))); + +void debug_error(const char * file, unsigned int line, const char * function, + const char * format, ...) __attribute__((format (printf, 4, 5))); + #define STRIPPATH(s) ( \ sizeof(s) > 2 && (s)[sizeof(s)- 3] == DIRECTORY_SEPARATOR ? (s) + sizeof(s) - 2 : \ sizeof(s) > 3 && (s)[sizeof(s)- 4] == DIRECTORY_SEPARATOR ? (s) + sizeof(s) - 3 : \ @@ -88,9 +104,8 @@ void printBacktrace(void); sizeof(s) > 21 && (s)[sizeof(s)-22] == DIRECTORY_SEPARATOR ? (s) + sizeof(s) - 21 : (s)) #define DEBUG_PRINT(level, fmt, ...) do { \ - fprintf(stderr, "%s%12" PRId64 "%20s:%-4u | %-30s | " fmt "%s\n", \ - debug_lookup[level], microtime(), STRIPPATH(__FILE__), \ - __LINE__, __FUNCTION__, ##__VA_ARGS__, debug_lookup[DEBUG_LEVEL_NONE]); \ + debug_level(level, STRIPPATH(__FILE__), __LINE__, __FUNCTION__, \ + fmt, ##__VA_ARGS__); \ } while (0) #define DEBUG_BREAK() DEBUG_PRINT(DEBUG_LEVEL_INFO, "================================================================================") @@ -124,13 +139,4 @@ void printBacktrace(void); #define DEBUG_PROTO(fmt, ...) do {} while(0) #endif -void debug_info(const char * file, unsigned int line, const char * function, - const char * format, ...) __attribute__((format (printf, 4, 5))); - -void debug_warn(const char * file, unsigned int line, const char * function, - const char * format, ...) __attribute__((format (printf, 4, 5))); - -void debug_error(const char * file, unsigned int line, const char * function, - const char * format, ...) __attribute__((format (printf, 4, 5))); - #endif diff --git a/common/src/debug.c b/common/src/debug.c index 2b6d3789..17575239 100644 --- a/common/src/debug.c +++ b/common/src/debug.c @@ -24,24 +24,56 @@ #include #include -inline static void debug_level(enum DebugLevel level, const char * file, +static uint64_t startTime; + +void debug_init() +{ + startTime = microtime(); + platform_debugInit(); +} + +inline static void debug_levelVA(enum DebugLevel level, const char * file, unsigned int line, const char * function, const char * format, va_list va) { - const char * f = strrchr(file, DIRECTORY_SEPARATOR) + 1; - fprintf(stderr, "%s%12" PRId64 "%20s:%-4u | %-30s | ", - debug_lookup[level], microtime(), f, + const char * f = strrchr(file, DIRECTORY_SEPARATOR); + if (!f) + f = file; + else + ++f; + + uint64_t elapsed = microtime() - startTime; + uint64_t sec = elapsed / 1000000UL; + uint64_t us = elapsed % 1000000UL; + + fprintf(stderr, "%02lu:%02lu:%02lu.%03lu %s %18s:%-4u | %-30s | ", + sec / 60 / 60, + sec / 60 % 60, + sec % 60, + us / 1000, + debug_lookup[level], + f, line, function); + vfprintf(stderr, format, va); fprintf(stderr, "%s\n", debug_lookup[DEBUG_LEVEL_NONE]); } +void debug_level(enum DebugLevel level, const char * file, unsigned int line, + const char * function, const char * format, ...) +{ + va_list va; + va_start(va, format); + debug_levelVA(level, file, line, function, format, va); + va_end(va); +} + void debug_info(const char * file, unsigned int line, const char * function, const char * format, ...) { va_list va; va_start(va, format); - debug_level(DEBUG_LEVEL_INFO, file, line, function, format, va); + debug_levelVA(DEBUG_LEVEL_INFO, file, line, function, format, va); va_end(va); } @@ -50,7 +82,7 @@ void debug_warn(const char * file, unsigned int line, const char * function, { va_list va; va_start(va, format); - debug_level(DEBUG_LEVEL_WARN, file, line, function, format, va); + debug_levelVA(DEBUG_LEVEL_WARN, file, line, function, format, va); va_end(va); } @@ -59,6 +91,6 @@ void debug_error(const char * file, unsigned int line, const char * function, { va_list va; va_start(va, format); - debug_level(DEBUG_LEVEL_ERROR, file, line, function, format, va); + debug_levelVA(DEBUG_LEVEL_ERROR, file, line, function, format, va); va_end(va); } diff --git a/common/src/platform/linux/debug.c b/common/src/platform/linux/debug.c index 551d865b..72d2d226 100644 --- a/common/src/platform/linux/debug.c +++ b/common/src/platform/linux/debug.c @@ -30,7 +30,7 @@ const char ** debug_lookup = NULL; -void debug_init(void) +void platform_debugInit(void) { static const char * colorLookup[] = { diff --git a/common/src/platform/windows/debug.c b/common/src/platform/windows/debug.c index 699a47f5..14bb2fe2 100644 --- a/common/src/platform/windows/debug.c +++ b/common/src/platform/windows/debug.c @@ -22,7 +22,7 @@ const char ** debug_lookup = NULL; -void debug_init(void) +void platform_debugInit(void) { static const char * plainLookup[] = {