log: Avoid including function names by default

Unless function names are requested, the logging system should not
compile these into the code. Adjust the macros to handle this.

This means that turning on function names at runtime won't work unless
CONFIG_LOGF_FUNC is enabled. We could perhaps split this into a
separate option if that is a problem.

Enable CONFIG_LOGF_FUNC logging for sandbox since the tests expect the
function names to be included. Fix up the pinmux test which checks a
logging statement.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Quentin Schulz <quentin.schulz@cherry.de>
This commit is contained in:
Simon Glass 2024-08-22 07:54:56 -06:00 committed by Tom Rini
parent 99b23d45cb
commit dfc0acd0cc
6 changed files with 26 additions and 11 deletions

View file

@ -38,10 +38,10 @@ static int log_console_emit(struct log_device *ldev, struct log_rec *rec)
printf("%d-", rec->line);
if (fmt & BIT(LOGF_FUNC)) {
if (CONFIG_IS_ENABLED(USE_TINY_PRINTF)) {
printf("%s()", rec->func);
printf("%s()", rec->func ?: "?");
} else {
printf("%*s()", CONFIG_LOGF_FUNC_PAD,
rec->func);
rec->func ?: "?");
}
}
}

View file

@ -88,7 +88,7 @@ static int log_syslog_emit(struct log_device *ldev, struct log_rec *rec)
if (fmt & BIT(LOGF_LINE))
append(&ptr, msg_end, "%d-", rec->line);
if (fmt & BIT(LOGF_FUNC))
append(&ptr, msg_end, "%s()", rec->func);
append(&ptr, msg_end, "%s()", rec->func ?: "?");
if (fmt & BIT(LOGF_MSG))
append(&ptr, msg_end, "%s%s",
fmt != BIT(LOGF_MSG) ? " " : "", rec->msg);

View file

@ -40,6 +40,7 @@ CONFIG_PRE_CONSOLE_BUFFER=y
CONFIG_LOG=y
CONFIG_LOG_MAX_LEVEL=9
CONFIG_LOG_DEFAULT_LEVEL=6
CONFIG_LOGF_FUNC=y
CONFIG_DISPLAY_BOARDINFO_LATE=y
CONFIG_STACKPROTECTOR=y
CONFIG_CMD_CPU=y

View file

@ -125,7 +125,7 @@ static inline int log_uc_cat(enum uclass_id id)
* @level: Level of log record (indicating its severity)
* @file: File name of file where log record was generated
* @line: Line number in file where log record was generated
* @func: Function where log record was generated
* @func: Function where log record was generated, NULL if not known
* @fmt: printf() format string for log record
* @...: Optional parameters, according to the format string @fmt
* Return: 0 if log record was emitted, -ve on error
@ -141,7 +141,7 @@ int _log(enum log_category_t cat, enum log_level_t level, const char *file,
* @level: Level of log record (indicating its severity)
* @file: File name of file where log record was generated
* @line: Line number in file where log record was generated
* @func: Function where log record was generated
* @func: Function where log record was generated, NULL if not known
* @addr: Starting address to display at start of line
* @data: pointer to data buffer
* @width: data value width. May be 1, 2, or 4.
@ -193,6 +193,12 @@ int _log_buffer(enum log_category_t cat, enum log_level_t level,
#define _LOG_DEBUG 0
#endif
#ifdef CONFIG_LOGF_FUNC
#define _log_func __func__
#else
#define _log_func NULL
#endif
#if CONFIG_IS_ENABLED(LOG)
/* Emit a log record if the level is less that the maximum */
@ -201,7 +207,7 @@ int _log_buffer(enum log_category_t cat, enum log_level_t level,
if (_LOG_DEBUG != 0 || _l <= _LOG_MAX_LEVEL) \
_log((enum log_category_t)(_cat), \
(enum log_level_t)(_l | _LOG_DEBUG), __FILE__, \
__LINE__, __func__, \
__LINE__, _log_func, \
pr_fmt(_fmt), ##_args); \
})
@ -211,7 +217,7 @@ int _log_buffer(enum log_category_t cat, enum log_level_t level,
if (_LOG_DEBUG != 0 || _l <= _LOG_MAX_LEVEL) \
_log_buffer((enum log_category_t)(_cat), \
(enum log_level_t)(_l | _LOG_DEBUG), __FILE__, \
__LINE__, __func__, _addr, _data, \
__LINE__, _log_func, _addr, _data, \
_width, _count, _linelen); \
})
#else
@ -314,7 +320,7 @@ void __assert_fail(const char *assertion, const char *file, unsigned int line,
#define assert_noisy(x) \
({ bool _val = (x); \
if (!_val) \
__assert_fail(#x, "?", __LINE__, __func__); \
__assert_fail(#x, "?", __LINE__, _log_func); \
_val; \
})

View file

@ -30,7 +30,13 @@ static int dm_test_cmd_pinmux_status_pinname(struct unit_test_state *uts)
console_record_reset();
run_command("pinmux status P9", 0);
ut_assert_nextlinen("single-pinctrl pinctrl-single-no-width: missing register width");
if (IS_ENABLED(CONFIG_LOGF_FUNC)) {
ut_assert_nextlinen(
" single_of_to_plat() single-pinctrl pinctrl-single-no-width: missing register width");
} else {
ut_assert_nextlinen(
"single-pinctrl pinctrl-single-no-width: missing register width");
}
ut_assert_nextlinen("P9 not found");
ut_assert_console_end();

View file

@ -452,8 +452,10 @@ int log_test_buffer(struct unit_test_state *uts)
/* This one should product no output due to the debug level */
log_buffer(LOGC_BOOT, LOGL_DEBUG, 0, buf, 1, 0x12, 0);
ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
ut_assert_nextline("00000010: 10 00 ..");
ut_assert_nextline(
" log_test_buffer() 00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
ut_assert_nextline(
" log_test_buffer() 00000010: 10 00 ..");
ut_assert_console_end();
free(buf);