Skip to content

Commit

Permalink
dbg: add dbg_printf_id
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Sep 6, 2023
1 parent 9f69a23 commit 73e29fa
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
58 changes: 50 additions & 8 deletions include/re_dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,47 +49,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


Expand All @@ -110,13 +150,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)(uint32_t 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(uint32_t id, int level, const char *fmt, ...);
const char *dbg_level_str(int level);

#ifdef __cplusplus
Expand Down
34 changes: 29 additions & 5 deletions src/dbg/dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(uint32_t id, int level, const char *fmt, va_list ap)
{
dbg_lock();

Expand Down Expand Up @@ -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 (id)
(void)re_fprintf(stderr, "{%u} ", id);

(void)re_vfprintf(stderr, fmt, ap);

if (dbg.flags & DBG_ANSI && level < DBG_DEBUG)
Expand All @@ -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(uint32_t id, int level, const char *fmt,
va_list ap)
{
char buf[256];
int len;
Expand All @@ -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 */
Expand All @@ -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(0, level, fmt, ap);
va_end(ap);

va_start(ap, fmt);
dbg_fmt_vprintf(0, level, fmt, ap);
va_end(ap);
}


/**
* Print a formatted debug message
*
* @param level Debug level
* @param fmt Formatted string
*/
void dbg_printf_id(uint32_t 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);
}

Expand Down
4 changes: 3 additions & 1 deletion test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(uint32_t id, int level, const char *p, size_t len,
void *arg)
{
(void)level;
(void)arg;
(void)id;

printf("%.*s", (int)len, p);
}
Expand Down

0 comments on commit 73e29fa

Please sign in to comment.