Skip to content

Commit

Permalink
解决当LOG_RAW异步输出多条文本的时候会被截断,原因是rt_vsnprintf会在字符串最后添加\0,ulog.c中的do_outpu…
Browse files Browse the repository at this point in the history
…t()将\0也压入到ulog.async_rb,因此LOG_RAW没有及时输出,那么rb中的字符串被\0截断了,导致没法正确输出LOG_RAW信息
  • Loading branch information
ComerLater committed Apr 19, 2024
1 parent cd77f98 commit 640a58a
Showing 1 changed file with 30 additions and 26 deletions.
56 changes: 30 additions & 26 deletions components/utilities/ulog/ulog.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@

/* 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
/**
* 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 */

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 640a58a

Please sign in to comment.