[common] debug: output to stdout and reset the the color at the end

This commit is contained in:
Geoffrey McRae 2021-07-08 07:17:01 +10:00
parent e96a80dd20
commit 0acce0737f
3 changed files with 7 additions and 3 deletions

View File

@ -32,6 +32,7 @@
enum DebugLevel
{
DEBUG_LEVEL_NONE,
DEBUG_LEVEL_INFO,
DEBUG_LEVEL_WARN,
DEBUG_LEVEL_ERROR,
@ -79,9 +80,9 @@ void printBacktrace(void);
sizeof(s) > 21 && (s)[sizeof(s)-22] == DIRECTORY_SEPARATOR ? (s) + sizeof(s) - 21 : (s))
#define DEBUG_PRINT(level, fmt, ...) do { \
fprintf(stdout, "%s%12" PRId64 "%20s:%-4u | %-30s | " fmt "\n", \
fprintf(stderr, "%s%12" PRId64 "%20s:%-4u | %-30s | " fmt "%s\n", \
debug_lookup[level], microtime(), STRIPPATH(__FILE__), \
__LINE__, __FUNCTION__, ##__VA_ARGS__); \
__LINE__, __FUNCTION__, ##__VA_ARGS__, debug_lookup[DEBUG_LEVEL_NONE]); \
} while (0)
#define DEBUG_BREAK() DEBUG_PRINT(DEBUG_LEVEL_INFO, "================================================================================")

View File

@ -34,6 +34,7 @@ void debug_init(void)
{
static const char * colorLookup[] =
{
COLOR_RESET , // DEBUG_LEVEL_NONE
COLOR_RESET "[I] ", // DEBUG_LEVEL_INFO
COLOR_YELLOW "[W] ", // DEBUG_LEVEL_WARN
COLOR_RED "[E] ", // DEBUG_LEVEL_ERROR
@ -43,6 +44,7 @@ void debug_init(void)
static const char * plainLookup[] =
{
"" , // DEBUG_LEVEL_NONE
"[I] ", // DEBUG_LEVEL_INFO
"[W] ", // DEBUG_LEVEL_WARN
"[E] ", // DEBUG_LEVEL_ERROR
@ -50,5 +52,5 @@ void debug_init(void)
"[!] " // DEBUG_LEVEL_FATAL
};
debug_lookup = (isatty(0) == 1) ? colorLookup : plainLookup;
debug_lookup = (isatty(STDERR_FILENO) == 1) ? colorLookup : plainLookup;
}

View File

@ -26,6 +26,7 @@ void debug_init(void)
{
static const char * plainLookup[] =
{
"" , // DEBUG_LEVEL_NONE
"[I] ", // DEBUG_LEVEL_INFO
"[W] ", // DEBUG_LEVEL_WARN
"[E] ", // DEBUG_LEVEL_ERROR