From fa5229052c5bd4928d1b939c6db6a574e5de2ba2 Mon Sep 17 00:00:00 2001 From: "Liu, Xinwu" Date: Fri, 15 Jun 2018 17:19:55 +0800 Subject: [PATCH] tools: acrn-crashlog: Improve the efficiency of do_log Get the header size by receiving return value of snprintf instead of initialize log buffer and strlen. Signed-off-by: Liu, Xinwu Reviewed-by: Eddie Dong Acked-by: Chen Gang --- tools/acrn-crashlog/common/log_sys.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tools/acrn-crashlog/common/log_sys.c b/tools/acrn-crashlog/common/log_sys.c index 6dfa30170..35110971b 100644 --- a/tools/acrn-crashlog/common/log_sys.c +++ b/tools/acrn-crashlog/common/log_sys.c @@ -8,17 +8,18 @@ #include #include "log_sys.h" -void do_log(int level, +void do_log(const int level, #ifdef DEBUG_ACRN_CRASHLOG - const char *func, int line, + const char *func, const int line, #endif ...) { va_list args; char *fmt; - char log[MAX_LOG_LEN] = {0}; + char log[MAX_LOG_LEN]; + int n = 0; #ifdef DEBUG_ACRN_CRASHLOG - char header_fmt[] = "<%-20s%d>: "; + const char header_fmt[] = "<%-20s%d>: "; #endif if (level > LOG_LEVEL) @@ -35,10 +36,13 @@ void do_log(int level, #ifdef DEBUG_ACRN_CRASHLOG /* header */ - snprintf(log, sizeof(log) - 1, header_fmt, func, line); + n = snprintf(log, sizeof(log), header_fmt, func, line); + if (n < 0 || n >= sizeof(log)) + n = 0; #endif /* msg */ - vsnprintf(log + strlen(log), sizeof(log) - strlen(log) - 1, fmt, args); + vsnprintf(log + n, sizeof(log) - n, fmt, args); + log[sizeof(log) - 1] = 0; va_end(args); sd_journal_print(level, log);