From 8647eaa7a4a195b2ef7ecb812e9d083f0684d68e Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Sat, 21 Apr 2018 22:56:48 +0200 Subject: [PATCH] Coloured, file, function and line referenced debug output. --- CMakeLists.txt | 1 + src/util.c | 76 +++++++++++++++++--------------------------------- src/util.h | 34 ++++++++++++++++------ 3 files changed, 52 insertions(+), 59 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 508930c..df0ffc0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,6 +85,7 @@ if (NOT WIN32) endif (NOT WIN32) set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -D__FNAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'") # PROGRAMS: add_executable(breakhack diff --git a/src/util.c b/src/util.c index 958640d..3aa6369 100644 --- a/src/util.c +++ b/src/util.c @@ -84,64 +84,40 @@ m_vsprintf(char *dest, size_t sz, const char *fmt, va_list args) } void -debug(const char *fmt, ...) +log_print(FILE *out, + const char *prefix, + const char *file, + int line, + const char *function, + const char * fmt, + ...) { -#ifdef DEBUG va_list args; char tstamp[10]; timestamp(tstamp, 10); - printf("[%s][--] ", tstamp); +#ifndef _WIN32 + if (out == stdout || out == stderr) { + fprintf(out, "\033[34m[%s]", tstamp); + if (strcmp(prefix, "DEBUG") == 0) + fprintf(out, "\033[33m"); + else if (strcmp(prefix, "FATAL") == 0 || strcmp(prefix, "ERROR") == 0) + fprintf(out, "\033[31m"); + else + fprintf(out, "\033[32m"); + fprintf(out, "[%5s]", prefix); + fprintf(out, "\033[36m[%20s:%-3d]\033[37m[%20s()]\033[0m ", + file, line, function); + } else { + fprintf(out, "[%s][%5s][%20s:%-3d][%20s()] ", tstamp, prefix, file, line, function); + } +#else + fprintf(out, "[%s][%5s][%20s:%-3d][%20s()] ", tstamp, prefix, file, line, function); +#endif va_start(args, fmt); - vprintf(fmt, args); + vfprintf(out, fmt, args); va_end(args); printf("\n"); -#else // DEBUG - UNUSED (fmt); -#endif // DEBUG -} - -void -info(const char * fmt, ...) -{ - va_list args; - char tstamp[10]; - - timestamp(tstamp, 10); - printf("[%s][**] ", tstamp); - va_start(args, fmt); - vprintf(fmt, args); - va_end(args); - printf("\n"); -} - -void -error(const char *fmt, ...) -{ - va_list args; - char tstamp[10]; - - timestamp(tstamp, 10); - fprintf(stderr, "[%s][!*] ", tstamp); - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - fprintf(stderr, "\n"); -} - -void -fatal(const char *fmt, ...) -{ - va_list args; - char tstamp[10]; - - timestamp(tstamp, 10); - fprintf(stderr, "[%s][!!] ", tstamp); - va_start(args, fmt); - vfprintf(stderr, fmt, args); - va_end(args); - fprintf(stderr, "\n"); - exit(-1); } void diff --git a/src/util.h b/src/util.h index 092f2e5..fdaf1e1 100644 --- a/src/util.h +++ b/src/util.h @@ -19,20 +19,36 @@ #ifndef UTIL_H_ #define UTIL_H_ +#include #include #include -void -fatal(const char *fmt, ...); +#ifndef __FNAME__ +#define __FNAME__ __FILE__ +#endif // __FNAME__ + +#ifdef DEBUG +#define debug(...) log_print(stdout, "DEBUG", __FNAME__, __LINE__, __func__, __VA_ARGS__) +#define info(...) log_print(stdout, "INFO", __FNAME__, __LINE__, __func__, __VA_ARGS__) +#else // DEBUG +#define debug(...) do {} while(0) +#define info(...) do {} while(0) +#endif // DEBUG +#define error(...) log_print(stderr, "ERROR", __FNAME__, __LINE__, __func__, __VA_ARGS__) +#define fatal(...) \ +{ \ + log_print(stderr, "FATAL", __FNAME__, __LINE__, __func__, __VA_ARGS__); \ + exit(-1); \ +} void -error(const char *fmt, ...); - -void -debug(const char *fmt, ...); - -void -info(const char *fmt, ...); +log_print(FILE *out, + const char *prefix, + const char *file, + int line, + const char *func, + const char *fmt, + ...); void * ec_malloc(unsigned long size);