From 8aa4a23eac3833c8846801218df75f1da69414ea Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Fri, 30 Oct 2026 20:32:38 +0800 Subject: [PATCH 1/9] feat(ulog): support output the calling file, function and line Add three independent switches (ULOG_USING_OUTPUT_FILE/FUNC/LINE) so that users can freely decide whether to prepend the calling file name, function name or line number to the log message when using the LOG_X API. These switches are disabled by default, keeping the output unchanged. Signed-off-by: ymj0440 --- components/utilities/Kconfig | 18 ++++++++++ components/utilities/ulog/ulog.c | 59 ++++++++++++++++++++++++++++++++ components/utilities/ulog/ulog.h | 36 ++++++++++++++++--- 3 files changed, 109 insertions(+), 4 deletions(-) diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig index c32cd692bde6..7a3824498adb 100644 --- a/components/utilities/Kconfig +++ b/components/utilities/Kconfig @@ -95,6 +95,24 @@ menuconfig RT_USING_ULOG bool "Enable assert check." default y + config ULOG_USING_OUTPUT_FILE + bool "Output the calling file name." + default n + help + The calling file name will be prepended to the log message when using LOG_X API. + + config ULOG_USING_OUTPUT_FUNC + bool "Output the calling function name." + default n + help + The calling function name will be prepended to the log message when using LOG_X API. + + config ULOG_USING_OUTPUT_LINE + bool "Output the calling line number." + default n + help + The calling line number will be prepended to the log message when using LOG_X API. + config ULOG_LINE_BUF_SIZE int "The log's max width." default 128 diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 1d7cd4851e8b..368083489fe3 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -817,6 +817,65 @@ void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const ch va_end(args); } +/** + * output the log with calling context (file name / function name / line number) + * + * @param level level + * @param tag tag + * @param file file name, can be RT_NULL + * @param func function name, can be RT_NULL + * @param line line number, 0 means not enabled + * @param newline has newline + * @param format output format + * @param ... args + */ +void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const char *func, rt_int32_t line, + rt_bool_t newline, const char *format, ...) +{ + va_list args; + char ctx_buf[ULOG_LINE_BUF_SIZE]; + char fmt[ULOG_LINE_BUF_SIZE]; + rt_size_t pos = 0; + int n; + + RT_ASSERT(tag); + RT_ASSERT(format); + + /* build the context prefix, e.g. "main.c:42 app_entry()" */ + if (file) + { + n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, "%s", file); + pos += (n > 0 && n < (int)(sizeof(ctx_buf) - pos)) ? n : (sizeof(ctx_buf) - pos - 1); + } + if (line > 0) + { + n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, ":%d", line); + pos += (n > 0 && n < (int)(sizeof(ctx_buf) - pos)) ? n : (sizeof(ctx_buf) - pos - 1); + } + if (func) + { + n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, " %s()", func); + pos += (n > 0 && n < (int)(sizeof(ctx_buf) - pos)) ? n : (sizeof(ctx_buf) - pos - 1); + } + + /* combine the context prefix with the user format */ + if (pos > 0) + { + rt_snprintf(fmt, sizeof(fmt), "[%s] %s", ctx_buf, format); + } + else + { + rt_snprintf(fmt, sizeof(fmt), "%s", format); + } + + /* args point to the first variable parameter */ + va_start(args, format); + + ulog_voutput(level, tag, newline, RT_NULL, 0, 0, 0, fmt, args); + + va_end(args); +} + /** * output RAW string format log * diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h index 1e0353d12170..b5dd4c5a4c26 100644 --- a/components/utilities/ulog/ulog.h +++ b/components/utilities/ulog/ulog.h @@ -40,10 +40,37 @@ void ulog_deinit(void); * LOG_D("this is a debug log!"); * LOG_E("this is a error log!"); */ -#define LOG_E(...) ulog_e(LOG_TAG, __VA_ARGS__) -#define LOG_W(...) ulog_w(LOG_TAG, __VA_ARGS__) -#define LOG_I(...) ulog_i(LOG_TAG, __VA_ARGS__) -#define LOG_D(...) ulog_d(LOG_TAG, __VA_ARGS__) +/* + * Whether to output the calling context (file name / function name / line number) + * in the LOG_X API. These three macros can be enabled independently, and the + * corresponding context information will be prepended to the log message, e.g. + * + * [main.c:42] app_entry(): this is a debug log! + * + * They are disabled by default to keep the output unchanged. + */ +#ifdef ULOG_USING_OUTPUT_FILE +#define ULOG_FILE_INFO __FILE__ +#else +#define ULOG_FILE_INFO RT_NULL +#endif + +#ifdef ULOG_USING_OUTPUT_FUNC +#define ULOG_FUNC_INFO __FUNCTION__ +#else +#define ULOG_FUNC_INFO RT_NULL +#endif + +#ifdef ULOG_USING_OUTPUT_LINE +#define ULOG_LINE_INFO __LINE__ +#else +#define ULOG_LINE_INFO 0 +#endif + +#define LOG_E(...) ulog_output_ctx(LOG_LVL_ERROR, LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, RT_TRUE, __VA_ARGS__) +#define LOG_W(...) ulog_output_ctx(LOG_LVL_WARNING, LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, RT_TRUE, __VA_ARGS__) +#define LOG_I(...) ulog_output_ctx(LOG_LVL_INFO, LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, RT_TRUE, __VA_ARGS__) +#define LOG_D(...) ulog_output_ctx(LOG_LVL_DBG, LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, RT_TRUE, __VA_ARGS__) #define LOG_RAW(...) ulog_raw(__VA_ARGS__) #define LOG_HEX(name, width, buf, size) ulog_hex(name, width, buf, size) @@ -95,6 +122,7 @@ void ulog_hexdump(const char *tag, rt_size_t width, const rt_uint8_t *buf, rt_si void ulog_voutput(rt_uint32_t level, const char *tag, rt_bool_t newline, const rt_uint8_t *hex_buf, rt_size_t hex_size, rt_size_t hex_width, rt_base_t hex_addr, const char *format, va_list args); void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *format, ...); +void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const char *func, rt_int32_t line, rt_bool_t newline, const char *format, ...); void ulog_raw(const char *format, ...); #ifdef __cplusplus From a2e8d01b193af4459797294ef9eb86e5f7c30d3e Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Fri, 6 Nov 2026 15:34:45 +0800 Subject: [PATCH 2/9] fix(ulog): address review comments on calling context output 1. Keep the compile-time log level filtering. LOG_X still expands to ulog_e/w/i/d when all three switches are disabled, and the new ulog_x_ctx macros share the same LOG_LVL/ULOG_OUTPUT_LVL guard, so logs filtered out at compile time do not evaluate their arguments. 2. Format the message before truncation. The original format string is always used in full; only the formatted result is limited by ULOG_LINE_BUF_SIZE, so a long template with a short output is no longer cut. 3. Assemble "[context] message" as plain text and pass it to the formatter through an "%s" format, so '%' characters inside the file name or the message are never parsed as format specifiers. Verified with gcc -fsyntax-only (all off / all on), macro expansion for the level filtering case, and runtime cases for a long format template and for '%' inside file names. Signed-off-by: ymj0440 --- components/utilities/ulog/ulog.c | 46 ++++++++++++++++++++++------ components/utilities/ulog/ulog.h | 21 ++++++++++--- components/utilities/ulog/ulog_def.h | 12 ++++++++ 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 368083489fe3..9147b0621e5e 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -834,14 +834,16 @@ void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const { va_list args; char ctx_buf[ULOG_LINE_BUF_SIZE]; - char fmt[ULOG_LINE_BUF_SIZE]; + char body_buf[ULOG_LINE_BUF_SIZE]; + char out_buf[2 * ULOG_LINE_BUF_SIZE + 8]; rt_size_t pos = 0; + rt_size_t body_len; int n; RT_ASSERT(tag); RT_ASSERT(format); - /* build the context prefix, e.g. "main.c:42 app_entry()" */ + /* build the context prefix as plain text, e.g. "main.c:42 app_entry()" */ if (file) { n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, "%s", file); @@ -858,20 +860,44 @@ void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const pos += (n > 0 && n < (int)(sizeof(ctx_buf) - pos)) ? n : (sizeof(ctx_buf) - pos - 1); } - /* combine the context prefix with the user format */ - if (pos > 0) + /* args point to the first variable parameter */ + va_start(args, format); + + if (pos == 0) { - rt_snprintf(fmt, sizeof(fmt), "[%s] %s", ctx_buf, format); + /* no context enabled, keep the original path with zero overhead */ + ulog_voutput(level, tag, newline, RT_NULL, 0, 0, 0, format, args); } else { - rt_snprintf(fmt, sizeof(fmt), "%s", format); - } + /* format the message first, so that the result (not the format + * string) is truncated when the output is longer than the buffer */ + n = rt_vsnprintf(body_buf, sizeof(body_buf), format, args); + if (n < 0) + { + body_len = 0; + } + else if (n >= (int)sizeof(body_buf)) + { + body_len = sizeof(body_buf) - 1; + } + else + { + body_len = (rt_size_t)n; + } - /* args point to the first variable parameter */ - va_start(args, format); + /* assemble "[context] message" as plain text, then hand it to the + * formatter as a "%s" argument, so that '%' characters inside the + * file name or the message are never parsed as format specifiers */ + out_buf[0] = '['; + rt_memcpy(out_buf + 1, ctx_buf, pos); + out_buf[pos + 1] = ']'; + out_buf[pos + 2] = ' '; + rt_memcpy(out_buf + pos + 3, body_buf, body_len); + out_buf[pos + 3 + body_len] = '\0'; - ulog_voutput(level, tag, newline, RT_NULL, 0, 0, 0, fmt, args); + ulog_output(level, tag, newline, "%s", out_buf); + } va_end(args); } diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h index b5dd4c5a4c26..a949c076aaf0 100644 --- a/components/utilities/ulog/ulog.h +++ b/components/utilities/ulog/ulog.h @@ -67,10 +67,23 @@ void ulog_deinit(void); #define ULOG_LINE_INFO 0 #endif -#define LOG_E(...) ulog_output_ctx(LOG_LVL_ERROR, LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, RT_TRUE, __VA_ARGS__) -#define LOG_W(...) ulog_output_ctx(LOG_LVL_WARNING, LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, RT_TRUE, __VA_ARGS__) -#define LOG_I(...) ulog_output_ctx(LOG_LVL_INFO, LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, RT_TRUE, __VA_ARGS__) -#define LOG_D(...) ulog_output_ctx(LOG_LVL_DBG, LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, RT_TRUE, __VA_ARGS__) +/* + * When all three switches are disabled, LOG_X keeps using the original + * ulog_e/w/i/d path, so that the compile-time log level filtering + * (LOG_LVL / ULOG_OUTPUT_LVL) still works and the arguments are not + * evaluated for logs that would be filtered out. + */ +#if defined(ULOG_USING_OUTPUT_FILE) || defined(ULOG_USING_OUTPUT_FUNC) || defined(ULOG_USING_OUTPUT_LINE) +#define LOG_E(...) ulog_e_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) +#define LOG_W(...) ulog_w_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) +#define LOG_I(...) ulog_i_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) +#define LOG_D(...) ulog_d_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) +#else +#define LOG_E(...) ulog_e(LOG_TAG, __VA_ARGS__) +#define LOG_W(...) ulog_w(LOG_TAG, __VA_ARGS__) +#define LOG_I(...) ulog_i(LOG_TAG, __VA_ARGS__) +#define LOG_D(...) ulog_d(LOG_TAG, __VA_ARGS__) +#endif /* ULOG_USING_OUTPUT_FILE || ULOG_USING_OUTPUT_FUNC || ULOG_USING_OUTPUT_LINE */ #define LOG_RAW(...) ulog_raw(__VA_ARGS__) #define LOG_HEX(name, width, buf, size) ulog_hex(name, width, buf, size) diff --git a/components/utilities/ulog/ulog_def.h b/components/utilities/ulog/ulog_def.h index e84b19846cf9..e43aa6084770 100644 --- a/components/utilities/ulog/ulog_def.h +++ b/components/utilities/ulog/ulog_def.h @@ -72,26 +72,38 @@ extern "C" { #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) #define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__) + #define ulog_d_ctx(TAG, FILE, FUNC, LINE, ...) \ + ulog_output_ctx(LOG_LVL_DBG, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) #else #define ulog_d(TAG, ...) + #define ulog_d_ctx(TAG, FILE, FUNC, LINE, ...) #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */ #if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) #define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__) + #define ulog_i_ctx(TAG, FILE, FUNC, LINE, ...) \ + ulog_output_ctx(LOG_LVL_INFO, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) #else #define ulog_i(TAG, ...) + #define ulog_i_ctx(TAG, FILE, FUNC, LINE, ...) #endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */ #if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) #define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__) + #define ulog_w_ctx(TAG, FILE, FUNC, LINE, ...) \ + ulog_output_ctx(LOG_LVL_WARNING, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) #else #define ulog_w(TAG, ...) + #define ulog_w_ctx(TAG, FILE, FUNC, LINE, ...) #endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */ #if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) #define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__) + #define ulog_e_ctx(TAG, FILE, FUNC, LINE, ...) \ + ulog_output_ctx(LOG_LVL_ERROR, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) #else #define ulog_e(TAG, ...) + #define ulog_e_ctx(TAG, FILE, FUNC, LINE, ...) #endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */ #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) From 8fa52fc2648730cfecc40949fd9e8147cfeaab4f Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Sat, 7 Nov 2026 23:30:05 +0800 Subject: [PATCH 3/9] style(ulog): apply clang-format 22.1.8 to the new macros Format the LOG_X and ulog_x_ctx macro definitions in ulog.h and ulog_def.h with the repo-pinned clang-format 22.1.8, fixing the 'Check File Format and License' CI job. Signed-off-by: ymj0440 --- components/utilities/ulog/ulog.h | 16 +++++------ components/utilities/ulog/ulog_def.h | 40 ++++++++++++++-------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h index a949c076aaf0..b9ca3099e87f 100644 --- a/components/utilities/ulog/ulog.h +++ b/components/utilities/ulog/ulog.h @@ -74,15 +74,15 @@ void ulog_deinit(void); * evaluated for logs that would be filtered out. */ #if defined(ULOG_USING_OUTPUT_FILE) || defined(ULOG_USING_OUTPUT_FUNC) || defined(ULOG_USING_OUTPUT_LINE) -#define LOG_E(...) ulog_e_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) -#define LOG_W(...) ulog_w_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) -#define LOG_I(...) ulog_i_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) -#define LOG_D(...) ulog_d_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) +#define LOG_E(...) ulog_e_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) +#define LOG_W(...) ulog_w_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) +#define LOG_I(...) ulog_i_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) +#define LOG_D(...) ulog_d_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) #else -#define LOG_E(...) ulog_e(LOG_TAG, __VA_ARGS__) -#define LOG_W(...) ulog_w(LOG_TAG, __VA_ARGS__) -#define LOG_I(...) ulog_i(LOG_TAG, __VA_ARGS__) -#define LOG_D(...) ulog_d(LOG_TAG, __VA_ARGS__) +#define LOG_E(...) ulog_e(LOG_TAG, __VA_ARGS__) +#define LOG_W(...) ulog_w(LOG_TAG, __VA_ARGS__) +#define LOG_I(...) ulog_i(LOG_TAG, __VA_ARGS__) +#define LOG_D(...) ulog_d(LOG_TAG, __VA_ARGS__) #endif /* ULOG_USING_OUTPUT_FILE || ULOG_USING_OUTPUT_FUNC || ULOG_USING_OUTPUT_LINE */ #define LOG_RAW(...) ulog_raw(__VA_ARGS__) #define LOG_HEX(name, width, buf, size) ulog_hex(name, width, buf, size) diff --git a/components/utilities/ulog/ulog_def.h b/components/utilities/ulog/ulog_def.h index e43aa6084770..8742e4a07368 100644 --- a/components/utilities/ulog/ulog_def.h +++ b/components/utilities/ulog/ulog_def.h @@ -71,39 +71,39 @@ extern "C" { #endif /* !defined(LOG_LVL) */ #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) - #define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__) - #define ulog_d_ctx(TAG, FILE, FUNC, LINE, ...) \ - ulog_output_ctx(LOG_LVL_DBG, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) +#define ulog_d(TAG, ...) ulog_output(LOG_LVL_DBG, TAG, RT_TRUE, __VA_ARGS__) +#define ulog_d_ctx(TAG, FILE, FUNC, LINE, ...) \ + ulog_output_ctx(LOG_LVL_DBG, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) #else - #define ulog_d(TAG, ...) - #define ulog_d_ctx(TAG, FILE, FUNC, LINE, ...) +#define ulog_d(TAG, ...) +#define ulog_d_ctx(TAG, FILE, FUNC, LINE, ...) #endif /* (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) */ #if (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) - #define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__) - #define ulog_i_ctx(TAG, FILE, FUNC, LINE, ...) \ - ulog_output_ctx(LOG_LVL_INFO, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) +#define ulog_i(TAG, ...) ulog_output(LOG_LVL_INFO, TAG, RT_TRUE, __VA_ARGS__) +#define ulog_i_ctx(TAG, FILE, FUNC, LINE, ...) \ + ulog_output_ctx(LOG_LVL_INFO, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) #else - #define ulog_i(TAG, ...) - #define ulog_i_ctx(TAG, FILE, FUNC, LINE, ...) +#define ulog_i(TAG, ...) +#define ulog_i_ctx(TAG, FILE, FUNC, LINE, ...) #endif /* (LOG_LVL >= LOG_LVL_INFO) && (ULOG_OUTPUT_LVL >= LOG_LVL_INFO) */ #if (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) - #define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__) - #define ulog_w_ctx(TAG, FILE, FUNC, LINE, ...) \ - ulog_output_ctx(LOG_LVL_WARNING, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) +#define ulog_w(TAG, ...) ulog_output(LOG_LVL_WARNING, TAG, RT_TRUE, __VA_ARGS__) +#define ulog_w_ctx(TAG, FILE, FUNC, LINE, ...) \ + ulog_output_ctx(LOG_LVL_WARNING, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) #else - #define ulog_w(TAG, ...) - #define ulog_w_ctx(TAG, FILE, FUNC, LINE, ...) +#define ulog_w(TAG, ...) +#define ulog_w_ctx(TAG, FILE, FUNC, LINE, ...) #endif /* (LOG_LVL >= LOG_LVL_WARNING) && (ULOG_OUTPUT_LVL >= LOG_LVL_WARNING) */ #if (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) - #define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__) - #define ulog_e_ctx(TAG, FILE, FUNC, LINE, ...) \ - ulog_output_ctx(LOG_LVL_ERROR, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) +#define ulog_e(TAG, ...) ulog_output(LOG_LVL_ERROR, TAG, RT_TRUE, __VA_ARGS__) +#define ulog_e_ctx(TAG, FILE, FUNC, LINE, ...) \ + ulog_output_ctx(LOG_LVL_ERROR, TAG, FILE, FUNC, LINE, RT_TRUE, __VA_ARGS__) #else - #define ulog_e(TAG, ...) - #define ulog_e_ctx(TAG, FILE, FUNC, LINE, ...) +#define ulog_e(TAG, ...) +#define ulog_e_ctx(TAG, FILE, FUNC, LINE, ...) #endif /* (LOG_LVL >= LOG_LVL_ERROR) && (ULOG_OUTPUT_LVL >= LOG_LVL_ERROR) */ #if (LOG_LVL >= LOG_LVL_DBG) && (ULOG_OUTPUT_LVL >= LOG_LVL_DBG) From 2820068a379d678eaf3afbe84759a3627c9bc2cf Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Sun, 8 Nov 2026 07:45:55 +0800 Subject: [PATCH 4/9] fix(ulog): avoid copying uninitialized stack data in context output Only advance `pos` when rt_snprintf() returns a positive value, so that an empty file name or function name can no longer push `pos` to the end of the buffer and make the following memcpy pick up uninitialized stack bytes. Initialize ctx_buf[0] as well. Also use the standard __func__ instead of the compiler extension __FUNCTION__ for ULOG_FUNC_INFO, as suggested. Verified: empty file -> pos stays 0; normal case -> 'main.c:42 app_entry()'; a 95-byte file name is truncated safely to the buffer. Signed-off-by: ymj0440 --- components/utilities/ulog/ulog.c | 17 ++++++++++++++--- components/utilities/ulog/ulog.h | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 9147b0621e5e..2d4f27d7ecee 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -843,21 +843,32 @@ void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const RT_ASSERT(tag); RT_ASSERT(format); + ctx_buf[0] = '\0'; + /* build the context prefix as plain text, e.g. "main.c:42 app_entry()" */ if (file) { n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, "%s", file); - pos += (n > 0 && n < (int)(sizeof(ctx_buf) - pos)) ? n : (sizeof(ctx_buf) - pos - 1); + if (n > 0) + { + pos += (n < (int)(sizeof(ctx_buf) - pos)) ? (rt_size_t)n : (sizeof(ctx_buf) - pos - 1); + } } if (line > 0) { n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, ":%d", line); - pos += (n > 0 && n < (int)(sizeof(ctx_buf) - pos)) ? n : (sizeof(ctx_buf) - pos - 1); + if (n > 0) + { + pos += (n < (int)(sizeof(ctx_buf) - pos)) ? (rt_size_t)n : (sizeof(ctx_buf) - pos - 1); + } } if (func) { n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, " %s()", func); - pos += (n > 0 && n < (int)(sizeof(ctx_buf) - pos)) ? n : (sizeof(ctx_buf) - pos - 1); + if (n > 0) + { + pos += (n < (int)(sizeof(ctx_buf) - pos)) ? (rt_size_t)n : (sizeof(ctx_buf) - pos - 1); + } } /* args point to the first variable parameter */ diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h index b9ca3099e87f..bf872ec3db06 100644 --- a/components/utilities/ulog/ulog.h +++ b/components/utilities/ulog/ulog.h @@ -56,7 +56,7 @@ void ulog_deinit(void); #endif #ifdef ULOG_USING_OUTPUT_FUNC -#define ULOG_FUNC_INFO __FUNCTION__ +#define ULOG_FUNC_INFO __func__ #else #define ULOG_FUNC_INFO RT_NULL #endif From 476090454525c796294f1705d5e80f973d386e3b Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Tue, 10 Nov 2026 18:19:39 +0800 Subject: [PATCH 5/9] [components][ulog] reduce the stack footprint and filter before formatting - keep a single ULOG_LINE_BUF_SIZE + 8 buffer on the caller stack instead of three line buffers (520 bytes), so the function stays usable on a 512 bytes soft timer thread stack (measured 480 bytes on a 64-bit host, which includes the va_list register save area); - apply the runtime filters (init state, global/tag level and tag filter) before formatting, so a log that would be dropped no longer scans the format string or converts any argument; - use only the file name part of __FILE__ and bound the context prefix to ULOG_LINE_BUF_SIZE / 2, so a long build path cannot hide the message. Signed-off-by: ymj0440 --- components/utilities/ulog/ulog.c | 156 ++++++++++++++++++++++--------- 1 file changed, 113 insertions(+), 43 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 2d4f27d7ecee..1d9c2da6778f 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -817,9 +817,83 @@ void ulog_output(rt_uint32_t level, const char *tag, rt_bool_t newline, const ch va_end(args); } +/** + * get the file name part of a path + * + * Only the last component is kept, so that a long build path cannot eat the + * whole context prefix. + * + * @param path path + * + * @return pointer to the file name part + */ +static const char *ulog_basename(const char *path) +{ + const char *base = path; + + while (*path != '\0') + { + if ((*path == '/') || (*path == '\\')) + { + base = path + 1; + } + path++; + } + + return base; +} + +/** + * check whether a log with the given level and tag would be dropped by the + * runtime filters + * + * Mirrors the checks done in ulog_voutput(), so that ulog_output_ctx() can + * skip formatting entirely for a log that is going to be dropped. + * + * @param level level + * @param tag tag + * + * @return RT_TRUE if the log should be dropped + */ +static rt_bool_t ulog_output_ctx_filtered(rt_uint32_t level, const char *tag) +{ + if (!ulog.init_ok) + { + return RT_TRUE; + } + +#ifdef ULOG_USING_FILTER +#ifndef ULOG_USING_SYSLOG + /* level filter */ + if ((level > ulog.filter.level) || (level > ulog_tag_lvl_filter_get(tag))) + { + return RT_TRUE; + } +#else + if (((LOG_MASK(LOG_PRI(level)) & ulog.filter.level) == 0) || + ((LOG_MASK(LOG_PRI(level)) & ulog_tag_lvl_filter_get(tag)) == 0)) + { + return RT_TRUE; + } +#endif /* ULOG_USING_SYSLOG */ + + /* tag filter */ + if (!rt_strstr(tag, ulog.filter.tag)) + { + return RT_TRUE; + } +#endif /* ULOG_USING_FILTER */ + + return RT_FALSE; +} + /** * output the log with calling context (file name / function name / line number) * + * Only one buffer is used on the caller's stack: the context prefix and the + * formatted message are both built into it, so that this function keeps a + * small stack footprint and stays usable from threads with a 512 bytes stack. + * * @param level level * @param tag tag * @param file file name, can be RT_NULL @@ -833,84 +907,80 @@ void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const rt_bool_t newline, const char *format, ...) { va_list args; - char ctx_buf[ULOG_LINE_BUF_SIZE]; - char body_buf[ULOG_LINE_BUF_SIZE]; - char out_buf[2 * ULOG_LINE_BUF_SIZE + 8]; - rt_size_t pos = 0; - rt_size_t body_len; + char out_buf[ULOG_LINE_BUF_SIZE + 8]; + rt_size_t pos = 1; int n; RT_ASSERT(tag); RT_ASSERT(format); - ctx_buf[0] = '\0'; + /* apply the runtime filters before formatting, so that a dropped log does + * not scan the format string or convert any argument */ + if (ulog_output_ctx_filtered(level, tag)) + { + return; + } + + /* reserve the first byte for '[' */ + out_buf[0] = '['; - /* build the context prefix as plain text, e.g. "main.c:42 app_entry()" */ + /* build the context prefix as plain text, e.g. "[main.c:42 app_entry()]". + * The prefix is bounded by ULOG_LINE_BUF_SIZE so that the message keeps + * enough room even when __FILE__ carries a long build path */ if (file) { - n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, "%s", file); + file = ulog_basename(file); + n = rt_snprintf(out_buf + pos, ULOG_LINE_BUF_SIZE / 2 - pos, "%s", file); if (n > 0) { - pos += (n < (int)(sizeof(ctx_buf) - pos)) ? (rt_size_t)n : (sizeof(ctx_buf) - pos - 1); + pos += (n < (int)(ULOG_LINE_BUF_SIZE / 2 - pos)) ? (rt_size_t)n : (ULOG_LINE_BUF_SIZE / 2 - pos - 1); } } if (line > 0) { - n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, ":%d", line); + n = rt_snprintf(out_buf + pos, ULOG_LINE_BUF_SIZE / 2 - pos, ":%d", line); if (n > 0) { - pos += (n < (int)(sizeof(ctx_buf) - pos)) ? (rt_size_t)n : (sizeof(ctx_buf) - pos - 1); + pos += (n < (int)(ULOG_LINE_BUF_SIZE / 2 - pos)) ? (rt_size_t)n : (ULOG_LINE_BUF_SIZE / 2 - pos - 1); } } if (func) { - n = rt_snprintf(ctx_buf + pos, sizeof(ctx_buf) - pos, " %s()", func); + n = rt_snprintf(out_buf + pos, ULOG_LINE_BUF_SIZE / 2 - pos, " %s()", func); if (n > 0) { - pos += (n < (int)(sizeof(ctx_buf) - pos)) ? (rt_size_t)n : (sizeof(ctx_buf) - pos - 1); + pos += (n < (int)(ULOG_LINE_BUF_SIZE / 2 - pos)) ? (rt_size_t)n : (ULOG_LINE_BUF_SIZE / 2 - pos - 1); } } - /* args point to the first variable parameter */ va_start(args, format); - if (pos == 0) + if (pos == 1) { - /* no context enabled, keep the original path with zero overhead */ + /* no context available, keep the original path with zero overhead */ ulog_voutput(level, tag, newline, RT_NULL, 0, 0, 0, format, args); + va_end(args); + return; } - else - { - /* format the message first, so that the result (not the format - * string) is truncated when the output is longer than the buffer */ - n = rt_vsnprintf(body_buf, sizeof(body_buf), format, args); - if (n < 0) - { - body_len = 0; - } - else if (n >= (int)sizeof(body_buf)) - { - body_len = sizeof(body_buf) - 1; - } - else - { - body_len = (rt_size_t)n; - } - /* assemble "[context] message" as plain text, then hand it to the - * formatter as a "%s" argument, so that '%' characters inside the - * file name or the message are never parsed as format specifiers */ - out_buf[0] = '['; - rt_memcpy(out_buf + 1, ctx_buf, pos); - out_buf[pos + 1] = ']'; - out_buf[pos + 2] = ' '; - rt_memcpy(out_buf + pos + 3, body_buf, body_len); - out_buf[pos + 3 + body_len] = '\0'; + out_buf[pos++] = ']'; + out_buf[pos++] = ' '; - ulog_output(level, tag, newline, "%s", out_buf); + /* format the message directly into the remaining space, so that only the + * result is truncated for over-long messages and the format string itself + * is always used in full */ + n = rt_vsnprintf(out_buf + pos, sizeof(out_buf) - pos, format, args); + if (n < 0) + { + out_buf[pos] = '\0'; } va_end(args); + + /* hand the assembled "[context] message" to the formatter as a "%s" + * argument, so that '%' characters inside the file name or the message + * are never parsed as format specifiers */ + ulog_output(level, tag, newline, "%s", out_buf); } /** From e31da27c23f03cf8a25da35c7960edf54e2b2407 Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Wed, 11 Nov 2026 23:11:53 +0800 Subject: [PATCH 6/9] [components][ulog] build the context in the log buffer and keep the byte length - reuse the thread/ISR log buffer instead of adding a local array, so ulog_output_ctx() no longer needs 520 bytes of caller stack (measured 304 bytes at -O0 on a 64-bit host, which already includes the va_list register save area, and 32-bit targets are smaller); - reserve space for the line number and the function name before writing the file name, so a long basename can no longer truncate them; - keep the complete byte count returned by rt_vsnprintf and hand it to do_output(), so a message containing an embedded NUL reaches the backend in full; - in the recursion branch format into a small local buffer and print it with "%s", so the va_list is never passed to rt_kprintf() as an ordinary argument. Signed-off-by: ymj0440 --- components/utilities/ulog/ulog.c | 266 ++++++++++++++++++++++--------- 1 file changed, 187 insertions(+), 79 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 1d9c2da6778f..12552952168e 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -696,10 +696,13 @@ static void do_output(rt_uint32_t level, const char *tag, rt_bool_t is_raw, cons * @param format output format * @param args variable argument list */ +/* guard against recursive logging from a synchronous backend, shared by all + * the output paths so that the context path behaves the same way */ +static rt_bool_t ulog_voutput_recursion = RT_FALSE; + void ulog_voutput(rt_uint32_t level, const char *tag, rt_bool_t newline, const rt_uint8_t *hex_buf, rt_size_t hex_size, rt_size_t hex_width, rt_base_t hex_addr, const char *format, va_list args) { - static rt_bool_t ulog_voutput_recursion = RT_FALSE; char *log_buf = RT_NULL; static rt_size_t log_len = 0; @@ -844,143 +847,248 @@ static const char *ulog_basename(const char *path) } /** - * check whether a log with the given level and tag would be dropped by the - * runtime filters + * format the calling context prefix * - * Mirrors the checks done in ulog_voutput(), so that ulog_output_ctx() can - * skip formatting entirely for a log that is going to be dropped. + * writes "[file:line func()] " into the given buffer. The line number and the + * function name are reserved first, so that a long file name can never + * truncate them. * - * @param level level - * @param tag tag + * @param buf buffer to write to + * @param size size of the buffer + * @param file file name, can be RT_NULL + * @param func function name, can be RT_NULL + * @param line line number, 0 means not enabled * - * @return RT_TRUE if the log should be dropped + * @return length written, 0 means nothing was written */ -static rt_bool_t ulog_output_ctx_filtered(rt_uint32_t level, const char *tag) +static rt_size_t ulog_ctx_formater(char *buf, rt_size_t size, const char *file, const char *func, rt_int32_t line) { - if (!ulog.init_ok) + rt_size_t pos = 0; + rt_size_t reserved = 0; + rt_size_t room; + int n; + + if (size < 8) { - return RT_TRUE; + return 0; } -#ifdef ULOG_USING_FILTER -#ifndef ULOG_USING_SYSLOG - /* level filter */ - if ((level > ulog.filter.level) || (level > ulog_tag_lvl_filter_get(tag))) + /* reserve the space needed by the line number and the function name + * first, so that they are never truncated by a long file name */ + if (line > 0) { - return RT_TRUE; + reserved += 12; /* ':' plus up to 10 digits */ } -#else - if (((LOG_MASK(LOG_PRI(level)) & ulog.filter.level) == 0) || - ((LOG_MASK(LOG_PRI(level)) & ulog_tag_lvl_filter_get(tag)) == 0)) + if (func != RT_NULL) { - return RT_TRUE; + reserved += rt_strlen(func) + 4; /* " (" and ")" */ } -#endif /* ULOG_USING_SYSLOG */ + if (reserved + 6 > size) + { + reserved = size / 3; + } + + buf[pos++] = '['; - /* tag filter */ - if (!rt_strstr(tag, ulog.filter.tag)) + /* file name, bounded so that the line number and the function name fit */ + if ((file != RT_NULL) && (size - pos > reserved + 3)) { - return RT_TRUE; + file = ulog_basename(file); + room = size - pos - reserved - 3; /* keep room for "] " and "\0" */ + n = rt_snprintf(buf + pos, room, "%s", file); + if (n > 0) + { + pos += ((rt_size_t)n < room) ? (rt_size_t)n : (room - 1); + } + } + + if ((line > 0) && (size - pos > 3)) + { + room = size - pos - 3; + n = rt_snprintf(buf + pos, room, ":%d", line); + if (n > 0) + { + pos += ((rt_size_t)n < room) ? (rt_size_t)n : (room - 1); + } + } + + if ((func != RT_NULL) && (size - pos > 3)) + { + room = size - pos - 3; + n = rt_snprintf(buf + pos, room, " %s()", func); + if (n > 0) + { + pos += ((rt_size_t)n < room) ? (rt_size_t)n : (room - 1); + } } -#endif /* ULOG_USING_FILTER */ - return RT_FALSE; + buf[pos++] = ']'; + buf[pos++] = ' '; + + return pos; } /** - * output the log with calling context (file name / function name / line number) + * voutput the log with calling context, reusing the thread or ISR log buffer * - * Only one buffer is used on the caller's stack: the context prefix and the - * formatted message are both built into it, so that this function keeps a - * small stack footprint and stays usable from threads with a 512 bytes stack. + * The context prefix and the message are written into the existing log buffer + * while the output is locked, so this path needs no extra buffer on the + * caller stack. * * @param level level * @param tag tag + * @param newline has newline * @param file file name, can be RT_NULL * @param func function name, can be RT_NULL * @param line line number, 0 means not enabled - * @param newline has newline * @param format output format - * @param ... args + * @param args args */ -void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const char *func, rt_int32_t line, - rt_bool_t newline, const char *format, ...) +static void ulog_voutput_ctx(rt_uint32_t level, const char *tag, rt_bool_t newline, const char *file, + const char *func, rt_int32_t line, const char *format, va_list args) { - va_list args; - char out_buf[ULOG_LINE_BUF_SIZE + 8]; - rt_size_t pos = 1; - int n; + char *log_buf = RT_NULL; + static rt_size_t log_len = 0; + int fmt_result; RT_ASSERT(tag); RT_ASSERT(format); +#ifndef ULOG_USING_SYSLOG + RT_ASSERT(level <= LOG_LVL_DBG); +#else + RT_ASSERT(LOG_PRI(level) <= LOG_DEBUG); +#endif /* ULOG_USING_SYSLOG */ + + if (!ulog.init_ok) + { + return; + } - /* apply the runtime filters before formatting, so that a dropped log does - * not scan the format string or convert any argument */ - if (ulog_output_ctx_filtered(level, tag)) +#ifdef ULOG_USING_FILTER + /* the runtime filters run before any formatting, so a dropped log does not + * scan the format string or convert any argument */ +#ifndef ULOG_USING_SYSLOG + if ((level > ulog.filter.level) || (level > ulog_tag_lvl_filter_get(tag))) { return; } +#else + if (((LOG_MASK(LOG_PRI(level)) & ulog.filter.level) == 0) || + ((LOG_MASK(LOG_PRI(level)) & ulog_tag_lvl_filter_get(tag)) == 0)) + { + return; + } +#endif /* ULOG_USING_SYSLOG */ + else if (!rt_strstr(tag, ulog.filter.tag)) + { + return; + } +#endif /* ULOG_USING_FILTER */ + + /* get log buffer */ + log_buf = get_log_buf(); - /* reserve the first byte for '[' */ - out_buf[0] = '['; + /* lock output */ + output_lock(); - /* build the context prefix as plain text, e.g. "[main.c:42 app_entry()]". - * The prefix is bounded by ULOG_LINE_BUF_SIZE so that the message keeps - * enough room even when __FILE__ carries a long build path */ - if (file) + if (ulog_voutput_recursion == RT_TRUE) { - file = ulog_basename(file); - n = rt_snprintf(out_buf + pos, ULOG_LINE_BUF_SIZE / 2 - pos, "%s", file); - if (n > 0) + /* a synchronous backend is logging again: format into a small local + * buffer and print it directly, so that the va_list is never handed to + * rt_kprintf() as an ordinary argument */ + char tmp_buf[64]; + + rt_vsnprintf(tmp_buf, sizeof(tmp_buf), format, args); + rt_kprintf("%s", tmp_buf); + + if (newline == RT_TRUE) { - pos += (n < (int)(ULOG_LINE_BUF_SIZE / 2 - pos)) ? (rt_size_t)n : (ULOG_LINE_BUF_SIZE / 2 - pos - 1); + rt_kprintf(ULOG_NEWLINE_SIGN); } + + output_unlock(); + return; } - if (line > 0) + + ulog_voutput_recursion = RT_TRUE; + + /* log head */ + log_len = ulog_head_formater(log_buf, level, tag); + /* calling context prefix, e.g. "[ulog.c:42 app_entry()] " */ + log_len += ulog_ctx_formater(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, file, func, line); + /* log content */ + fmt_result = rt_vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args); + /* calculate log length; the complete byte count is kept so that a message + * with an embedded NUL still reaches the backend in full */ + if ((fmt_result > -1) && (log_len + fmt_result <= ULOG_LINE_BUF_SIZE)) { - n = rt_snprintf(out_buf + pos, ULOG_LINE_BUF_SIZE / 2 - pos, ":%d", line); - if (n > 0) - { - pos += (n < (int)(ULOG_LINE_BUF_SIZE / 2 - pos)) ? (rt_size_t)n : (ULOG_LINE_BUF_SIZE / 2 - pos - 1); - } + log_len += fmt_result; } - if (func) + else { - n = rt_snprintf(out_buf + pos, ULOG_LINE_BUF_SIZE / 2 - pos, " %s()", func); - if (n > 0) + log_len = ULOG_LINE_BUF_SIZE; + ulog_no_enough_buffer_printf(); + } + /* log tail */ + log_len = ulog_tail_formater(log_buf, log_len, newline, level); + +#ifdef ULOG_USING_FILTER + /* keyword filter */ + if (ulog.filter.keyword[0] != '\0') + { + log_buf[log_len] = '\0'; + + if (!rt_strstr(log_buf, ulog.filter.keyword)) { - pos += (n < (int)(ULOG_LINE_BUF_SIZE / 2 - pos)) ? (rt_size_t)n : (ULOG_LINE_BUF_SIZE / 2 - pos - 1); + ulog_voutput_recursion = RT_FALSE; + output_unlock(); + return; } } +#endif /* ULOG_USING_FILTER */ + + /* do log output */ + do_output(level, tag, RT_FALSE, log_buf, log_len); + + ulog_voutput_recursion = RT_FALSE; + + /* unlock output */ + output_unlock(); +} + +/** + * output the log with calling context (file name / function name / line number) + * + * @param level level + * @param tag tag + * @param file file name, can be RT_NULL + * @param func function name, can be RT_NULL + * @param line line number, 0 means not enabled + * @param newline has newline + * @param format output format + * @param ... args + */ +void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const char *func, rt_int32_t line, + rt_bool_t newline, const char *format, ...) +{ + va_list args; + + RT_ASSERT(tag); + RT_ASSERT(format); va_start(args, format); - if (pos == 1) + if ((file == RT_NULL) && (func == RT_NULL) && (line <= 0)) { /* no context available, keep the original path with zero overhead */ ulog_voutput(level, tag, newline, RT_NULL, 0, 0, 0, format, args); - va_end(args); - return; } - - out_buf[pos++] = ']'; - out_buf[pos++] = ' '; - - /* format the message directly into the remaining space, so that only the - * result is truncated for over-long messages and the format string itself - * is always used in full */ - n = rt_vsnprintf(out_buf + pos, sizeof(out_buf) - pos, format, args); - if (n < 0) + else { - out_buf[pos] = '\0'; + ulog_voutput_ctx(level, tag, newline, file, func, line, format, args); } va_end(args); - - /* hand the assembled "[context] message" to the formatter as a "%s" - * argument, so that '%' characters inside the file name or the message - * are never parsed as format specifiers */ - ulog_output(level, tag, newline, "%s", out_buf); } /** From 6cc989a210c650740326a7da46b2d499059c495c Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Thu, 12 Nov 2026 07:16:21 +0800 Subject: [PATCH 7/9] [components][ulog] disable the context switches in syslog mode syslog_formater() owns the format of every syslog record, so a context prefix cannot be inserted without breaking the syslog format. Hide the three switches in Kconfig when ULOG_USING_SYSLOG is enabled, and add the same guard to the LOG_X macro, so that a hand-written configuration falls back to the original path instead of emitting records without the syslog head. Signed-off-by: ymj0440 --- components/utilities/Kconfig | 6 ++++++ components/utilities/ulog/ulog.h | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig index 7a3824498adb..f5deaaafa5a4 100644 --- a/components/utilities/Kconfig +++ b/components/utilities/Kconfig @@ -98,20 +98,26 @@ menuconfig RT_USING_ULOG config ULOG_USING_OUTPUT_FILE bool "Output the calling file name." default n + depends on !ULOG_USING_SYSLOG help The calling file name will be prepended to the log message when using LOG_X API. + Not available in syslog mode, where the format of every record is owned by syslog_formater(). config ULOG_USING_OUTPUT_FUNC bool "Output the calling function name." default n + depends on !ULOG_USING_SYSLOG help The calling function name will be prepended to the log message when using LOG_X API. + Not available in syslog mode, where the format of every record is owned by syslog_formater(). config ULOG_USING_OUTPUT_LINE bool "Output the calling line number." default n + depends on !ULOG_USING_SYSLOG help The calling line number will be prepended to the log message when using LOG_X API. + Not available in syslog mode, where the format of every record is owned by syslog_formater(). config ULOG_LINE_BUF_SIZE int "The log's max width." diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h index bf872ec3db06..4e2af9db040c 100644 --- a/components/utilities/ulog/ulog.h +++ b/components/utilities/ulog/ulog.h @@ -72,8 +72,13 @@ void ulog_deinit(void); * ulog_e/w/i/d path, so that the compile-time log level filtering * (LOG_LVL / ULOG_OUTPUT_LVL) still works and the arguments are not * evaluated for logs that would be filtered out. + * + * The context switches are not available in syslog mode: every record is + * formatted by syslog_formater() there, so the prefix cannot be inserted + * without changing the syslog format. Kconfig already hides the switches in + * that case; the check below also covers hand-written configurations. */ -#if defined(ULOG_USING_OUTPUT_FILE) || defined(ULOG_USING_OUTPUT_FUNC) || defined(ULOG_USING_OUTPUT_LINE) +#if (defined(ULOG_USING_OUTPUT_FILE) || defined(ULOG_USING_OUTPUT_FUNC) || defined(ULOG_USING_OUTPUT_LINE)) && !defined(ULOG_USING_SYSLOG) #define LOG_E(...) ulog_e_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) #define LOG_W(...) ulog_w_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) #define LOG_I(...) ulog_i_ctx(LOG_TAG, ULOG_FILE_INFO, ULOG_FUNC_INFO, ULOG_LINE_INFO, __VA_ARGS__) From 9d6b54cfe194b59c9bb8f9b1ce2cac47372ef0df Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Fri, 13 Nov 2026 10:05:12 +0800 Subject: [PATCH 8/9] [components][ulog] keep the message room, the line number and recursive logs - give the context prefix at most half of the space left after the log head, so a long file name can no longer hide the message; when the space is really tight the prefix still gets 12 bytes for the line number - emit the line number and the function name only when they fit completely, a truncated number is never printed - format recursive backend logs into a static buffer as wide as the console buffer instead of a 64-byte local one, and keep it out of the caller stack - route the public ulog_output_ctx() through the plain path in syslog mode, so a direct call never emits a record without the syslog head Signed-off-by: ymj0440 --- components/utilities/ulog/ulog.c | 67 +++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 12552952168e..ec32636d1a11 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -851,7 +851,8 @@ static const char *ulog_basename(const char *path) * * writes "[file:line func()] " into the given buffer. The line number and the * function name are reserved first, so that a long file name can never - * truncate them. + * truncate them; when the buffer is too small they are omitted instead of + * being printed truncated. * * @param buf buffer to write to * @param size size of the buffer @@ -883,14 +884,11 @@ static rt_size_t ulog_ctx_formater(char *buf, rt_size_t size, const char *file, { reserved += rt_strlen(func) + 4; /* " (" and ")" */ } - if (reserved + 6 > size) - { - reserved = size / 3; - } buf[pos++] = '['; - /* file name, bounded so that the line number and the function name fit */ + /* file name, written only when the line number and the function name + * still fit after it */ if ((file != RT_NULL) && (size - pos > reserved + 3)) { file = ulog_basename(file); @@ -902,23 +900,32 @@ static rt_size_t ulog_ctx_formater(char *buf, rt_size_t size, const char *file, } } - if ((line > 0) && (size - pos > 3)) + /* line number, emitted only when the complete number fits, so that a + * truncated number is never printed */ + if ((line > 0) && (size - pos > 4)) { - room = size - pos - 3; - n = rt_snprintf(buf + pos, room, ":%d", line); - if (n > 0) + char line_buf[16]; + + n = rt_snprintf(line_buf, sizeof(line_buf), ":%d", line); + if ((n > 0) && (n < (int)sizeof(line_buf)) && (size - pos - 3 >= (rt_size_t)n)) { - pos += ((rt_size_t)n < room) ? (rt_size_t)n : (room - 1); + rt_memcpy(buf + pos, line_buf, (rt_size_t)n); + pos += (rt_size_t)n; } } - if ((func != RT_NULL) && (size - pos > 3)) + /* function name, emitted only when the complete name fits */ + if ((func != RT_NULL) && (size - pos > 4)) { room = size - pos - 3; - n = rt_snprintf(buf + pos, room, " %s()", func); - if (n > 0) + + if (room >= rt_strlen(func) + 5) /* " " + func + "()" + "\0" */ { - pos += ((rt_size_t)n < room) ? (rt_size_t)n : (room - 1); + n = rt_snprintf(buf + pos, room, " %s()", func); + if (n > 0) + { + pos += (rt_size_t)n; + } } } @@ -949,6 +956,7 @@ static void ulog_voutput_ctx(rt_uint32_t level, const char *tag, rt_bool_t newli { char *log_buf = RT_NULL; static rt_size_t log_len = 0; + rt_size_t ctx_size = 0; int fmt_result; RT_ASSERT(tag); @@ -993,13 +1001,14 @@ static void ulog_voutput_ctx(rt_uint32_t level, const char *tag, rt_bool_t newli if (ulog_voutput_recursion == RT_TRUE) { - /* a synchronous backend is logging again: format into a small local - * buffer and print it directly, so that the va_list is never handed to + /* a synchronous backend is logging again: format into a static buffer + * as wide as the console buffer and print it directly, so that the + * message is not truncated and the va_list is never handed to * rt_kprintf() as an ordinary argument */ - char tmp_buf[64]; + static char recursion_buf[ULOG_LINE_BUF_SIZE]; - rt_vsnprintf(tmp_buf, sizeof(tmp_buf), format, args); - rt_kprintf("%s", tmp_buf); + rt_vsnprintf(recursion_buf, sizeof(recursion_buf), format, args); + rt_kprintf("%s", recursion_buf); if (newline == RT_TRUE) { @@ -1014,8 +1023,16 @@ static void ulog_voutput_ctx(rt_uint32_t level, const char *tag, rt_bool_t newli /* log head */ log_len = ulog_head_formater(log_buf, level, tag); - /* calling context prefix, e.g. "[ulog.c:42 app_entry()] " */ - log_len += ulog_ctx_formater(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, file, func, line); + /* calling context prefix, e.g. "[ulog.c:42 app_entry()] ". At most half of + * the remaining space is given to the prefix, so that the message always + * keeps the same amount of room and a long file name cannot hide it; when + * the space is really tight the line number still gets its 12 bytes */ + ctx_size = (ULOG_LINE_BUF_SIZE - log_len) / 2; + if ((ctx_size < 12) && ((ULOG_LINE_BUF_SIZE - log_len) >= 12)) + { + ctx_size = 12; + } + log_len += ulog_ctx_formater(log_buf + log_len, ctx_size, file, func, line); /* log content */ fmt_result = rt_vsnprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, format, args); /* calculate log length; the complete byte count is kept so that a message @@ -1078,6 +1095,11 @@ void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const va_start(args, format); +#ifdef ULOG_USING_SYSLOG + /* syslog mode owns the record format, so the public entry keeps using the + * plain path and never emits a record without the syslog head */ + ulog_voutput(level, tag, newline, RT_NULL, 0, 0, 0, format, args); +#else if ((file == RT_NULL) && (func == RT_NULL) && (line <= 0)) { /* no context available, keep the original path with zero overhead */ @@ -1087,6 +1109,7 @@ void ulog_output_ctx(rt_uint32_t level, const char *tag, const char *file, const { ulog_voutput_ctx(level, tag, newline, file, func, line, format, args); } +#endif /* ULOG_USING_SYSLOG */ va_end(args); } From e3db5efa3c20a2cdfec3620e5a7147cdb78ddab1 Mon Sep 17 00:00:00 2001 From: ymj0440 Date: Wed, 25 Nov 2026 12:09:28 +0800 Subject: [PATCH 9/9] [components][ulog] follow the console buffer size for recursive logs The recursion buffer was sized by ULOG_LINE_BUF_SIZE (128 by default), while rt_kprintf() prints through RT_CONSOLEBUF_SIZE (256 in the UTest configuration). A recursive message longer than the ulog line buffer was therefore cut before rt_kprintf() could print it. Size the buffer by RT_CONSOLEBUF_SIZE so that the console capacity decides how much is printed, and keep ULOG_LINE_BUF_SIZE only as the fallback when the console buffer size is not defined. Signed-off-by: ymj0440 --- components/utilities/ulog/ulog.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index ec32636d1a11..06894697c134 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -1003,9 +1003,15 @@ static void ulog_voutput_ctx(rt_uint32_t level, const char *tag, rt_bool_t newli { /* a synchronous backend is logging again: format into a static buffer * as wide as the console buffer and print it directly, so that the - * message is not truncated and the va_list is never handed to - * rt_kprintf() as an ordinary argument */ + * va_list is never handed to rt_kprintf() as an ordinary argument. + * The buffer follows RT_CONSOLEBUF_SIZE, not ULOG_LINE_BUF_SIZE: the + * message is going to the console, and it must not be cut by the ulog + * line buffer before rt_kprintf() gets a chance to print it. */ +#ifdef RT_CONSOLEBUF_SIZE + static char recursion_buf[RT_CONSOLEBUF_SIZE]; +#else static char recursion_buf[ULOG_LINE_BUF_SIZE]; +#endif rt_vsnprintf(recursion_buf, sizeof(recursion_buf), format, args); rt_kprintf("%s", recursion_buf);