Skip to content
Open
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
17 changes: 9 additions & 8 deletions src/gurt/dlog.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* (C) Copyright 2016-2024 Intel Corporation.
* (C) Copyright 2025 Hewlett Packard Enterprise Development LP
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*/
Expand Down Expand Up @@ -370,6 +371,7 @@ log_exceed_threshold(void)

/* exceeds the size threshold, rename the current log file
* as backup, create a new log file.
* clog_lock() shall be called outside this function;
*/
static int
log_rotate(void)
Expand Down Expand Up @@ -431,7 +433,6 @@ log_rotate(void)
return rc;
}


/**
* This function can do a few things:
* - copy log message @msg to log buffer
Expand All @@ -440,6 +441,7 @@ log_rotate(void)
* original_name.old, and create a new log file.
*
* If @flush is true, it writes log buffer to log file immediately.
* clog_lock() shall be called outside this function;
*/
static int
d_log_write(char *msg, int len, bool flush)
Expand Down Expand Up @@ -544,11 +546,10 @@ d_log_disable_logging(void)
}

/**
* d_vlog: core log function, front-ended by d_log
* we vsnprintf the message into a holding buffer to format it. then we
* send it to all target output logs. the holding buffer is set to
* DLOG_TBSIZ, if the message is too long it will be silently truncated.
* caller should not hold clogmux, d_vlog will grab it as needed.
* d_vlog: core log function, front-ended by d_log we vsnprintf the message into a holding buffer to
* format it. Then we send it to all target output logs. The holding buffer is set to DLOG_TBSIZ,
* The message will be silently truncated if it is too long. caller should not hold clogmux,
* d_vlog will grab it as needed.
*
* @param flags returned by d_log_check
* @param fmt the printf(3) format to use
Expand Down Expand Up @@ -584,7 +585,7 @@ void d_vlog(int flags, const char *fmt, va_list ap)
lvl = flags & DLOG_PRIMASK;
pri = flags & DLOG_PRINDMASK;

/* Check the facility so we don't crash. We will just log the message
/* Check the facility so we don't crash. We will just log the message
* in this case but it really is indicative of a usage error as user
* didn't pass sanitized flags to this routine
*/
Expand Down Expand Up @@ -668,11 +669,11 @@ void d_vlog(int flags, const char *fmt, va_list ap)
* check for it anyway.
*/
if (hlen + 1 >= sizeof(b)) {
clog_unlock(); /* drop lock, this is the only early exit */
dlog_print_err(E2BIG,
"header overflowed %zd byte buffer (%d)\n",
sizeof(b), hlen + 1);
errno = save_errno;
clog_unlock();
return;
}
/*
Expand Down
94 changes: 46 additions & 48 deletions src/include/gurt/debug.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/*
* (C) Copyright 2017-2023 Intel Corporation.
* (C) Copyright 2025 Google LLC
* (C) Copyright 2025 Hewlett Packard Enterprise Development LP
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*/
Expand Down Expand Up @@ -79,103 +81,99 @@ extern void (*d_alt_assert)(const int, const char*, const char*, const int);
__func__, ptr, ##__VA_ARGS__)

/** Internal macro for saving the log, checking it, and printing, if enabled */
#define _D_LOG_CHECK(func, saved_mask, mask, ...) \
#define _D_LOG_CHECK(func, saved_flags, flags, ...) \
do { \
(saved_mask) = d_log_check(mask); \
if (saved_mask) { \
func(saved_mask, ##__VA_ARGS__); \
(saved_flags) = d_log_check(flags); \
if (saved_flags) { \
func(saved_flags, ##__VA_ARGS__); \
} \
} while (0)

/* The _D_LOG internal macro checks the specified mask and, if enabled, it logs the message,
* prependng the file, line, and function name. This function can be used directly by users or by
/* The _D_DEBUG_* internal macro checks the specified mask and, if enabled, it logs the message,
* prependng the file, line, and function name. This function can be used directly by users or by
* user defined macros if the provided log level macros are not flexible enough.
* saved_flags - L-value ....
*/
#define _D_LOG(func, mask, ...) \
#define _D_DEBUG_W_SAVED_FLAGS(func, saved_flags, level, ...) \
do { \
int __tmp_mask; \
_D_LOG_CHECK(func, __tmp_mask, mask, ##__VA_ARGS__); \
if (__builtin_expect(saved_flags, 0)) { \
if ((saved_flags) == (int)DLOG_UNINIT) { \
_D_LOG_CHECK(func, saved_flags, (level) | D_LOGFAC, \
##__VA_ARGS__); \
break; \
} \
func(saved_flags, ##__VA_ARGS__); \
} \
} while (0)

#define _D_DEBUG(func, flag, ...) \
do { \
if (__builtin_expect(DD_FLAG(flag, D_LOGFAC), 0)) { \
if (DD_FLAG(flag, D_LOGFAC) == (int)DLOG_UNINIT) { \
_D_LOG_CHECK(func, \
DD_FLAG(flag, D_LOGFAC), \
(flag) | D_LOGFAC, \
##__VA_ARGS__); \
break; \
} \
func(DD_FLAG(flag, D_LOGFAC), ##__VA_ARGS__); \
} \
} while (0)
#define _D_DEBUG(func, level, ...) \
_D_DEBUG_W_SAVED_FLAGS(func, DD_FLAG(level, D_LOGFAC), level, ##__VA_ARGS__)

#define D_LOG_ENABLED(flag) \
#define D_LOG_ENABLED(level) \
({ \
_D_DEBUG(D_NOOP, flag); \
__builtin_expect(DD_FLAG(flag, D_LOGFAC), 0); \
_D_DEBUG(D_NOOP, level); \
__builtin_expect(DD_FLAG(level, D_LOGFAC), 0); \
})

/* Log a message conditionally upon resolving the mask
/* Log a message conditionally upon resolving the level
*
* The mask is combined with D_LOGFAC which the user should define before
* The level is combined with D_LOGFAC which the user should define before
* including debug headers
*
* \param mask The debug bits or priority mask
* \param level The priority and/or debug flags
* \param fmt The format string to print
*
* User should define D_LOGFAC for the file
*/
#define D_DEBUG(flag, fmt, ...) \
_D_DEBUG(_D_LOG_NOCHECK, flag, fmt, ##__VA_ARGS__)
#define D_DEBUG(level, fmt, ...) \
_D_DEBUG(_D_LOG_NOCHECK, level, fmt, ##__VA_ARGS__)

/* Log a pointer value and message conditionally upon resolving the mask
/* Log a pointer value and message conditionally upon resolving the level
*
* The mask is combined with D_LOGFAC which the user should define before
* The level is combined with D_LOGFAC which the user should define before
* including debug headers
*
* \param mask The debug bits or priority mask
* \param level The priority and/or debug flags
* \param ptr A pointer value that is put into the message
* \param fmt The format string to print
*
* User should define D_LOGFAC for the file
*/
#define D_TRACE_DEBUG(flag, ptr, fmt, ...) \
_D_DEBUG(_D_TRACE_NOCHECK, flag, ptr, fmt, ##__VA_ARGS__)
#define D_TRACE_DEBUG(level, ptr, fmt, ...) \
_D_DEBUG(_D_TRACE_NOCHECK, level, ptr, fmt, ##__VA_ARGS__)

/** Special conditional debug so we can pass different flags to base routine
/** Special conditional debug so we can pass different level to base routine
* based on a condition. With V2, things like cond ? flag1 : flag2 don't
* work natively.
*/
#define D_CDEBUG(cond, flag_true, flag_false, ...) \
#define D_CDEBUG(cond, level_true, level_false, ...) \
do { \
if (cond) \
D_DEBUG(flag_true, __VA_ARGS__); \
D_DEBUG(level_true, __VA_ARGS__); \
else \
D_DEBUG(flag_false, __VA_ARGS__); \
D_DEBUG(level_false, __VA_ARGS__); \
} while (0)

#define DL_CDEBUG(cond, flag_true, flag_false, _rc, _fmt, ...) \
#define DL_CDEBUG(cond, level_true, level_false, _rc, _fmt, ...) \
do { \
if (cond) \
D_DEBUG(flag_true, _fmt ": " DF_RC " \n", ##__VA_ARGS__, DP_RC(_rc)); \
D_DEBUG(level_true, _fmt ": " DF_RC " \n", ##__VA_ARGS__, DP_RC(_rc)); \
else \
D_DEBUG(flag_false, _fmt ": " DF_RC "\n", ##__VA_ARGS__, DP_RC(_rc)); \
D_DEBUG(level_false, _fmt ": " DF_RC "\n", ##__VA_ARGS__, DP_RC(_rc)); \
} while (0)

/* Register a descriptor with a parent and a type */
#define D_TRACE_UP(flag, ptr, parent, type) \
D_TRACE_DEBUG(flag, ptr, "Registered new '%s' from %p\n", \
#define D_TRACE_UP(level, ptr, parent, type) \
D_TRACE_DEBUG(level, ptr, "Registered new '%s' from %p\n", \
type, parent)

/* De-register a descriptor, including all aliases */
#define D_TRACE_DOWN(flag, ptr) \
D_TRACE_DEBUG(flag, ptr, "Deregistered\n")
#define D_TRACE_DOWN(level, ptr) \
D_TRACE_DEBUG(level, ptr, "Deregistered\n")

/** Register a root with type */
#define D_TRACE_ROOT(flag, ptr, type) \
D_TRACE_DEBUG(flag, ptr, "Registered new '%s' as root\n", type)
#define D_TRACE_ROOT(level, ptr, type) \
D_TRACE_DEBUG(level, ptr, "Registered new '%s' as root\n", type)

/** Helper macros to conditionally output logs conditionally based on
* the message priority and the current log level. See D_DEBUG and
Expand Down
26 changes: 8 additions & 18 deletions src/include/gurt/dlog.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* (C) Copyright 2016-2024 Intel Corporation.
* (C) Copyright 2025 Hewlett Packard Enterprise Development LP
*
* SPDX-License-Identifier: BSD-2-Clause-Patent
*/
Expand Down Expand Up @@ -43,7 +44,6 @@ typedef uint64_t d_dbug_t;
#define DLOG_STDERR 0x20000000 /**< always log to stderr */
#define DLOG_STDOUT 0x10000000 /**< always log to stdout */

#define DLOG_PRIMASK 0x0fffff00 /**< priority mask */
#define D_FOREACH_PRIO_MASK(ACTION, arg) \
ACTION(DLOG_EMIT, emit, emit, 0x08000000, arg) /**< emit */ \
ACTION(DLOG_EMERG, fatal, fatal, 0x07000000, arg) /**< emergency */ \
Expand Down Expand Up @@ -72,6 +72,7 @@ enum d_log_flag_bits {

#define DLOG_PRISHIFT 24 /**< to get non-debug level */
#define DLOG_DPRISHIFT 8 /**< to get debug level */
#define DLOG_PRIMASK 0x0fffff00 /**< priority mask */
#define DLOG_PRINDMASK 0x0f000000 /**< mask for non-debug level bits */
#define DLOG_FACMASK 0x000000ff /**< facility mask */
#define DLOG_UNINIT 0x80000000 /**< Reserve one bit mask cache */
Expand Down Expand Up @@ -263,29 +264,18 @@ static inline int d_log_check(int flags)
return lvl | fac;
}

/**
* log a message using stdarg list without checking flags
*
* A log line cannot be larger than DLOG_TBSZ (4096), if it is larger it will be
* (silently) truncated].
*
* \param[in] flags flags returned from d_log_check
* \param[in] fmt printf-style format string
* \param[in] ap stdarg list
*/
void d_vlog(int flags, const char *fmt, va_list ap);

/**
* log a message if type specified by flags is enabled
*
* A log line cannot be larger than DLOG_TBSZ (4096), if it is larger it will be
* (silently) truncated].
* A log line must not be longer than 1023, including `\n' at the end, otherwise it will be silent
* truncated.
*
* \param[in] flags returned by d_log_check(facility+level+misc
* flags), 0 indicates no log.
* \param[in] flags returned by d_log_check (facility+level+debug flags),
* 0 indicates no log.
* \param[in] fmt printf-style format string
* \param[in] ap stdarg list
*/
void d_vlog(int flags, const char *fmt, va_list ap);
static inline void d_log(int flags, const char *fmt, ...)
__attribute__ ((__format__(__printf__, 2, 3)));
static inline void d_log(int flags, const char *fmt, ...)
Expand Down Expand Up @@ -313,7 +303,7 @@ int d_log_allocfacility(const char *aname, const char *lname);
/**
* Ensure default cart log is initialized. This routine calls
* d_log_open the first time based on D_LOG_MASK and D_LOG_FILE
* environment variables. It keeps a reference count so d_log_fini
* environment variables. It keeps a reference count so d_log_fini
* must be called by all callers to release the call d_log_close()
*
* Without this mechanism, it is difficult to use the cart logging
Expand Down