diff --git a/components/utilities/Kconfig b/components/utilities/Kconfig index c32cd692bde..f5deaaafa5a 100644 --- a/components/utilities/Kconfig +++ b/components/utilities/Kconfig @@ -95,6 +95,30 @@ menuconfig RT_USING_ULOG bool "Enable assert check." default y + 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." default 128 diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index 1d7cd4851e8..06894697c13 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; @@ -817,6 +820,306 @@ 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; +} + +/** + * format the calling context prefix + * + * 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; 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 + * @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 length written, 0 means nothing was written + */ +static rt_size_t ulog_ctx_formater(char *buf, rt_size_t size, const char *file, const char *func, rt_int32_t line) +{ + rt_size_t pos = 0; + rt_size_t reserved = 0; + rt_size_t room; + int n; + + if (size < 8) + { + return 0; + } + + /* 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) + { + reserved += 12; /* ':' plus up to 10 digits */ + } + if (func != RT_NULL) + { + reserved += rt_strlen(func) + 4; /* " (" and ")" */ + } + + buf[pos++] = '['; + + /* 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); + 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); + } + } + + /* line number, emitted only when the complete number fits, so that a + * truncated number is never printed */ + if ((line > 0) && (size - pos > 4)) + { + 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)) + { + rt_memcpy(buf + pos, line_buf, (rt_size_t)n); + pos += (rt_size_t)n; + } + } + + /* function name, emitted only when the complete name fits */ + if ((func != RT_NULL) && (size - pos > 4)) + { + room = size - pos - 3; + + if (room >= rt_strlen(func) + 5) /* " " + func + "()" + "\0" */ + { + n = rt_snprintf(buf + pos, room, " %s()", func); + if (n > 0) + { + pos += (rt_size_t)n; + } + } + } + + buf[pos++] = ']'; + buf[pos++] = ' '; + + return pos; +} + +/** + * voutput the log with calling context, reusing the thread or ISR log buffer + * + * 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 format output format + * @param args args + */ +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) +{ + char *log_buf = RT_NULL; + static rt_size_t log_len = 0; + rt_size_t ctx_size = 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; + } + +#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(); + + /* lock output */ + output_lock(); + + if (ulog_voutput_recursion == RT_TRUE) + { + /* a synchronous backend is logging again: format into a static buffer + * as wide as the console buffer and print it directly, so that the + * 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); + + if (newline == RT_TRUE) + { + rt_kprintf(ULOG_NEWLINE_SIGN); + } + + output_unlock(); + return; + } + + 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()] ". 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 + * with an embedded NUL still reaches the backend in full */ + if ((fmt_result > -1) && (log_len + fmt_result <= ULOG_LINE_BUF_SIZE)) + { + log_len += fmt_result; + } + else + { + 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)) + { + 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); + +#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 */ + ulog_voutput(level, tag, newline, RT_NULL, 0, 0, 0, format, args); + } + else + { + ulog_voutput_ctx(level, tag, newline, file, func, line, format, args); + } +#endif /* ULOG_USING_SYSLOG */ + + va_end(args); +} + /** * output RAW string format log * diff --git a/components/utilities/ulog/ulog.h b/components/utilities/ulog/ulog.h index 1e0353d1217..4e2af9db040 100644 --- a/components/utilities/ulog/ulog.h +++ b/components/utilities/ulog/ulog.h @@ -40,10 +40,55 @@ 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 __func__ +#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 + +/* + * 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. + * + * 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)) && !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__) +#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) @@ -95,6 +140,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 diff --git a/components/utilities/ulog/ulog_def.h b/components/utilities/ulog/ulog_def.h index e84b19846cf..8742e4a0736 100644 --- a/components/utilities/ulog/ulog_def.h +++ b/components/utilities/ulog/ulog_def.h @@ -71,27 +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(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(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(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(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(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(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(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(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)