From c535743af1c7282ee055a623026e056449922cb9 Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Wed, 6 Sep 2023 17:20:08 +0200 Subject: [PATCH] dbg: add dbg_printf_id --- include/re_dbg.h | 60 +++++++++++++++++++++++++++++++++++++++++------- src/dbg/dbg.c | 34 +++++++++++++++++++++++---- test/main.c | 4 +++- 3 files changed, 84 insertions(+), 14 deletions(-) diff --git a/include/re_dbg.h b/include/re_dbg.h index 8ab1e9370..859a54889 100644 --- a/include/re_dbg.h +++ b/include/re_dbg.h @@ -8,6 +8,8 @@ extern "C" { #endif +struct pl; + /** Debug levels */ enum { DBG_EMERG = 0, /**< System is unusable */ @@ -49,47 +51,87 @@ enum { * * Print warning message */ + +/** + * @def DEBUG_WARNING_ID(...) + * + * Print warning message with identifier + */ + #if (DEBUG_LEVEL >= 4) -#define DEBUG_WARNING(...) \ +#define DEBUG_WARNING(...) \ dbg_printf(DBG_WARNING, DEBUG_MODULE ": " __VA_ARGS__) +#define DEBUG_WARNING_ID(...) \ + dbg_printf_id(debug_id, DBG_WARNING, DEBUG_MODULE ": " __VA_ARGS__) #else #define DEBUG_WARNING(...) +#define DEBUG_WARNING_ID(...) #endif + /** * @def DEBUG_NOTICE(...) * * Print notice message */ + +/** + * @def DEBUG_NOTICE_ID(...) + * + * Print notice message with identifier + */ + #if (DEBUG_LEVEL >= 5) -#define DEBUG_NOTICE(...) \ - dbg_printf(DBG_NOTICE, DEBUG_MODULE ": " __VA_ARGS__) +#define DEBUG_NOTICE(...) dbg_printf(DBG_NOTICE, DEBUG_MODULE ": " __VA_ARGS__) +#define DEBUG_NOTICE_ID(...) \ + dbg_printf_id(debug_id, DBG_NOTICE, DEBUG_MODULE ": " __VA_ARGS__) #else #define DEBUG_NOTICE(...) +#define DEBUG_NOTICE_ID(...) #endif + /** * @def DEBUG_INFO(...) * * Print info message */ + +/** + * @def DEBUG_INFO_ID(...) + * + * Print info message with identifier + */ + #if (DEBUG_LEVEL >= 6) -#define DEBUG_INFO(...) \ - dbg_printf(DBG_INFO, DEBUG_MODULE ": " __VA_ARGS__) +#define DEBUG_INFO(...) dbg_printf(DBG_INFO, DEBUG_MODULE ": " __VA_ARGS__) +#define DEBUG_INFO_ID(...) \ + dbg_printf_id(debug_id, DBG_INFO, DEBUG_MODULE ": " __VA_ARGS__) #else #define DEBUG_INFO(...) +#define DEBUG_INFO_ID(...) #endif + /** * @def DEBUG_PRINTF(...) * * Print debug message */ + +/** + * @def DEBUG_PRINTF_ID(...) + * + * Print debug message with identifier + */ + #if (DEBUG_LEVEL >= 7) -#define DEBUG_PRINTF(...) \ - dbg_printf(DBG_DEBUG, DEBUG_MODULE ": " __VA_ARGS__) +#define DEBUG_PRINTF(...) dbg_printf(DBG_DEBUG, DEBUG_MODULE ": " __VA_ARGS__) +#define DEBUG_PRINTF_ID(...) \ + dbg_printf_id(debug_id, DBG_DEBUG, DEBUG_MODULE ": " __VA_ARGS__) #else #define DEBUG_PRINTF(...) +#define DEBUG_PRINTF_ID(...) #endif @@ -110,13 +152,15 @@ enum dbg_flags { * @param len String length * @param arg Handler argument */ -typedef void (dbg_print_h)(int level, const char *p, size_t len, void *arg); +typedef void(dbg_print_h)(struct pl *id, int level, const char *p, size_t len, + void *arg); void dbg_init(int level, enum dbg_flags flags); void dbg_close(void); int dbg_logfile_set(const char *name); void dbg_handler_set(dbg_print_h *ph, void *arg); void dbg_printf(int level, const char *fmt, ...); +void dbg_printf_id(struct pl *id, int level, const char *fmt, ...); const char *dbg_level_str(int level); #ifdef __cplusplus diff --git a/src/dbg/dbg.c b/src/dbg/dbg.c index e7a19ea84..16ff81683 100644 --- a/src/dbg/dbg.c +++ b/src/dbg/dbg.c @@ -130,7 +130,7 @@ void dbg_handler_set(dbg_print_h *ph, void *arg) /* NOTE: This function should not allocate memory */ -static void dbg_vprintf(int level, const char *fmt, va_list ap) +static void dbg_vprintf(struct pl *id, int level, const char *fmt, va_list ap) { dbg_lock(); @@ -171,6 +171,9 @@ static void dbg_vprintf(int level, const char *fmt, va_list ap) (void)re_fprintf(stderr, "[%09llu] ", ticks - dbg.tick); } + if (pl_isset(id)) + (void)re_fprintf(stderr, "{%r} ", id); + (void)re_vfprintf(stderr, fmt, ap); if (dbg.flags & DBG_ANSI && level < DBG_DEBUG) @@ -181,7 +184,8 @@ static void dbg_vprintf(int level, const char *fmt, va_list ap) /* Formatted output to print handler and/or logfile */ -static void dbg_fmt_vprintf(int level, const char *fmt, va_list ap) +static void dbg_fmt_vprintf(struct pl *id, int level, const char *fmt, + va_list ap) { char buf[256]; int len; @@ -201,7 +205,7 @@ static void dbg_fmt_vprintf(int level, const char *fmt, va_list ap) /* Print handler? */ if (dbg.ph) { - dbg.ph(level, buf, len, dbg.arg); + dbg.ph(id, level, buf, len, dbg.arg); } /* Output to file */ @@ -226,11 +230,31 @@ void dbg_printf(int level, const char *fmt, ...) va_list ap; va_start(ap, fmt); - dbg_vprintf(level, fmt, ap); + dbg_vprintf(NULL, level, fmt, ap); + va_end(ap); + + va_start(ap, fmt); + dbg_fmt_vprintf(NULL, level, fmt, ap); + va_end(ap); +} + + +/** + * Print a formatted debug message + * + * @param level Debug level + * @param fmt Formatted string + */ +void dbg_printf_id(struct pl *id, int level, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + dbg_vprintf(id, level, fmt, ap); va_end(ap); va_start(ap, fmt); - dbg_fmt_vprintf(level, fmt, ap); + dbg_fmt_vprintf(id, level, fmt, ap); va_end(ap); } diff --git a/test/main.c b/test/main.c index 62ff47fa4..da9d7e6e8 100644 --- a/test/main.c +++ b/test/main.c @@ -53,10 +53,12 @@ static void usage(void) #endif -static void dbg_handler(int level, const char *p, size_t len, void *arg) +static void dbg_handler(struct pl *id, int level, const char *p, size_t len, + void *arg) { (void)level; (void)arg; + (void)id; printf("%.*s", (int)len, p); }