From 640a58a83775d1c7ec54b2f6b7cfba758ef90a8a Mon Sep 17 00:00:00 2001 From: latercomer Date: Fri, 19 Apr 2024 09:51:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BD=93LOG=5FRAW=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E8=BE=93=E5=87=BA=E5=A4=9A=E6=9D=A1=E6=96=87=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=E4=BC=9A=E8=A2=AB=E6=88=AA=E6=96=AD?= =?UTF-8?q?=EF=BC=8C=E5=8E=9F=E5=9B=A0=E6=98=AFrt=5Fvsnprintf=E4=BC=9A?= =?UTF-8?q?=E5=9C=A8=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=9C=80=E5=90=8E=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0\0=EF=BC=8Culog.c=E4=B8=AD=E7=9A=84do=5Foutput()?= =?UTF-8?q?=E5=B0=86\0=E4=B9=9F=E5=8E=8B=E5=85=A5=E5=88=B0ulog.async=5Frb?= =?UTF-8?q?=EF=BC=8C=E5=9B=A0=E6=AD=A4LOG=5FRAW=E6=B2=A1=E6=9C=89=E5=8F=8A?= =?UTF-8?q?=E6=97=B6=E8=BE=93=E5=87=BA=EF=BC=8C=E9=82=A3=E4=B9=88rb?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=A2=AB\0?= =?UTF-8?q?=E6=88=AA=E6=96=AD=E4=BA=86=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=B2=A1?= =?UTF-8?q?=E6=B3=95=E6=AD=A3=E7=A1=AE=E8=BE=93=E5=87=BALOG=5FRAW=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/utilities/ulog/ulog.c | 56 +++++++++++++++++--------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/components/utilities/ulog/ulog.c b/components/utilities/ulog/ulog.c index cb0790b9de66..85bbbf42d53b 100644 --- a/components/utilities/ulog/ulog.c +++ b/components/utilities/ulog/ulog.c @@ -28,7 +28,7 @@ /* the number which is max stored line logs */ #ifndef ULOG_ASYNC_OUTPUT_STORE_LINES -#define ULOG_ASYNC_OUTPUT_STORE_LINES (ULOG_ASYNC_OUTPUT_BUF_SIZE * 3 / 2 / 80) +#define ULOG_ASYNC_OUTPUT_STORE_LINES (ULOG_ASYNC_OUTPUT_BUF_SIZE * 3 / 2 / 80) #endif #ifdef ULOG_USING_COLOR @@ -36,33 +36,33 @@ * CSI(Control Sequence Introducer/Initiator) sign * more information on https://en.wikipedia.org/wiki/ANSI_escape_code */ -#define CSI_START "\033[" -#define CSI_END "\033[0m" +#define CSI_START "\033[" +#define CSI_END "\033[0m" /* output log front color */ -#define F_BLACK "30m" -#define F_RED "31m" -#define F_GREEN "32m" -#define F_YELLOW "33m" -#define F_BLUE "34m" -#define F_MAGENTA "35m" -#define F_CYAN "36m" -#define F_WHITE "37m" +#define F_BLACK "30m" +#define F_RED "31m" +#define F_GREEN "32m" +#define F_YELLOW "33m" +#define F_BLUE "34m" +#define F_MAGENTA "35m" +#define F_CYAN "36m" +#define F_WHITE "37m" /* output log default color definition */ #ifndef ULOG_COLOR_DEBUG -#define ULOG_COLOR_DEBUG RT_NULL +#define ULOG_COLOR_DEBUG RT_NULL #endif #ifndef ULOG_COLOR_INFO -#define ULOG_COLOR_INFO (F_GREEN) +#define ULOG_COLOR_INFO (F_GREEN) #endif #ifndef ULOG_COLOR_WARN -#define ULOG_COLOR_WARN (F_YELLOW) +#define ULOG_COLOR_WARN (F_YELLOW) #endif #ifndef ULOG_COLOR_ERROR -#define ULOG_COLOR_ERROR (F_RED) +#define ULOG_COLOR_ERROR (F_RED) #endif #ifndef ULOG_COLOR_ASSERT -#define ULOG_COLOR_ASSERT (F_MAGENTA) +#define ULOG_COLOR_ASSERT (F_MAGENTA) #endif #endif /* ULOG_USING_COLOR */ @@ -285,7 +285,7 @@ rt_weak rt_size_t ulog_head_formater(char *log_buf, rt_uint32_t level, const cha tm = localtime_r(&t, &tm_tmp); /* show the time format MM-DD HH:MM:SS */ rt_snprintf(log_buf + log_len, ULOG_LINE_BUF_SIZE - log_len, "%02d-%02d %02d:%02d:%02d", tm->tm_mon + 1, - tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); + tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); /* check the microseconds support when kernel is startup */ if (t > 0 && !check_usec_support && rt_thread_self() != RT_NULL) { @@ -420,7 +420,7 @@ rt_weak rt_size_t ulog_tail_formater(char *log_buf, rt_size_t log_len, rt_bool_t } rt_weak rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *tag, rt_bool_t newline, - const char *format, va_list args) + const char *format, va_list args) { /* the caller has locker, so it can use static variable for reduce stack usage */ static rt_size_t log_len; @@ -449,7 +449,7 @@ rt_weak rt_size_t ulog_formater(char *log_buf, rt_uint32_t level, const char *ta rt_weak rt_size_t ulog_hex_formater(char *log_buf, const char *tag, const rt_uint8_t *buf, rt_size_t size, rt_size_t width, rt_base_t addr) { -#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ') +#define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ') /* the caller has locker, so it can use static variable for reduce stack usage */ static rt_size_t log_len, j; static int fmt_result; @@ -592,14 +592,15 @@ static void do_output(rt_uint32_t level, const char *tag, rt_bool_t is_raw, cons if (already_output == RT_FALSE) { rt_kprintf("Warning: There is no enough buffer for saving async log," - " please increase the ULOG_ASYNC_OUTPUT_BUF_SIZE option.\n"); + " please increase the ULOG_ASYNC_OUTPUT_BUF_SIZE option.\n"); already_output = RT_TRUE; } } } else if (ulog.async_rb) { - rt_ringbuffer_put(ulog.async_rb, (const rt_uint8_t *)log_buf, (rt_uint16_t)log_buf_size); + /* log_buf_size contain the tail \0, which will lead discard follow char, so only put log_buf_size -1 */ + rt_ringbuffer_put(ulog.async_rb, (const rt_uint8_t *)log_buf, (rt_uint16_t)log_buf_size - 1); /* send a notice */ rt_sem_release(&ulog.async_notice); } @@ -639,7 +640,7 @@ static void do_output(rt_uint32_t level, const char *tag, rt_bool_t is_raw, cons * @param args variable argument list */ 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) + 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; @@ -793,8 +794,11 @@ void ulog_raw(const char *format, ...) fmt_result = rt_vsnprintf(log_buf, ULOG_LINE_BUF_SIZE, format, args); va_end(args); - /* calculate log length */ - if ((fmt_result > -1) && (fmt_result <= ULOG_LINE_BUF_SIZE)) + /* calculate log length + * rt_vsnprintf would add \0 to the end, push \0 to ulog.async_rb will discard the follow char + * if fmt_result = ULOG_LINE_BUF_SIZE, then the last char must be \0 + */ + if ((fmt_result > -1) && (fmt_result < ULOG_LINE_BUF_SIZE)) { log_len = fmt_result; } @@ -1358,7 +1362,7 @@ void ulog_async_output(void) { /* output to all backends */ ulog_output_to_all_backend(log_frame->level, log_frame->tag, log_frame->is_raw, log_frame->log, - log_frame->log_len); + log_frame->log_len); } rt_rbb_blk_free(ulog.async_rbb, log_blk); } @@ -1498,7 +1502,7 @@ int ulog_async_init(void) { /* async output thread */ ulog.async_th = rt_thread_create("ulog_async", async_output_thread_entry, &ulog, ULOG_ASYNC_OUTPUT_THREAD_STACK, - ULOG_ASYNC_OUTPUT_THREAD_PRIORITY, 20); + ULOG_ASYNC_OUTPUT_THREAD_PRIORITY, 20); if (ulog.async_th == RT_NULL) { rt_kprintf("Error: ulog init failed! No memory for async output thread.\n");