Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dbg: add dbg_printf_id #946

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 52 additions & 8 deletions include/re_dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
extern "C" {
#endif

struct pl;

/** Debug levels */
enum {
DBG_EMERG = 0, /**< System is unusable */
Expand Down Expand Up @@ -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__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it better to pass "debug_id" via the macro parameters, instead of assume the name ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It follows the same technique used for DEBUG_LEVEL and DEBUG_MODULE.
The compiler errors if debug_id is not defined. I think it's much easier for rewriting existing DEBUG code since you have only define debug_id and add _ID. And this needs less reformatting new lines, that was the idea behind it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we consider uppercase: DEBUG_ID ?

#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 +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, ...);
sreimers marked this conversation as resolved.
Show resolved Hide resolved
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(struct pl *id, int level, const char *fmt, va_list ap)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const for id ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a custom log handler should able to call mem_ref(id), this discards the const qualifier.

{
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 (pl_isset(id))
(void)re_fprintf(stderr, "{%r} ", 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(struct pl *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(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);
}

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(struct pl *id, int level, const char *p, size_t len,
void *arg)
{
(void)level;
(void)arg;
(void)id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the id (aka "tag") be part of the log line ?


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