syslog: merge multiple lib_sprintf into one

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai
2023-01-04 13:58:52 +08:00
committed by Xiang Xiao
parent 74b8776872
commit e9ed994fec
+62 -16
View File
@@ -81,7 +81,7 @@ static FAR const char * const g_priority_str[] =
int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap) int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
{ {
struct lib_syslogstream_s stream; struct lib_syslogstream_s stream;
int ret = 0; int ret;
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME) #if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME)
FAR struct tcb_s *tcb = nxsched_get_tcb(gettid()); FAR struct tcb_s *tcb = nxsched_get_tcb(gettid());
#endif #endif
@@ -92,6 +92,9 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER]; char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER];
# endif # endif
#endif #endif
struct va_format vaf;
vaf.fmt = fmt;
vaf.va = ap;
/* Wrap the low-level output in a stream object and let lib_vsprintf /* Wrap the low-level output in a stream object and let lib_vsprintf
* do the work. * do the work.
@@ -118,7 +121,6 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
/* Use CLOCK_REALTIME if so configured */ /* Use CLOCK_REALTIME if so configured */
clock_gettime(CLOCK_REALTIME, &ts); clock_gettime(CLOCK_REALTIME, &ts);
# else # else
/* Prefer monotonic when enabled, as it can be synchronized to /* Prefer monotonic when enabled, as it can be synchronized to
* RTC with clock_resynchronize. * RTC with clock_resynchronize.
@@ -145,68 +147,112 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
# endif # endif
#endif #endif
ret = lib_sprintf(&stream.public,
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT) #if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Reset the terminal style. */ /* Reset the terminal style. */
ret = lib_sprintf(&stream.public, "\e[0m"); "\e[0m"
#endif #endif
#ifdef CONFIG_SYSLOG_TIMESTAMP #ifdef CONFIG_SYSLOG_TIMESTAMP
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED) # if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND) # if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND)
ret += lib_sprintf(&stream.public, "[%s.%06ld] ", "[%s.%06ld] "
date_buf, ts.tv_nsec / NSEC_PER_USEC);
# else # else
ret += lib_sprintf(&stream.public, "[%s] ", date_buf); "[%s] "
# endif # endif
# else # else
ret += lib_sprintf(&stream.public, "[%5jd.%06ld] ", "[%5jd.%06ld] "
(uintmax_t)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
# endif # endif
#endif #endif
#if defined(CONFIG_SMP) #if defined(CONFIG_SMP)
ret += lib_sprintf(&stream.public, "[CPU%d] ", up_cpu_index()); "[CPU%d] "
#endif #endif
#if defined(CONFIG_SYSLOG_PROCESSID) #if defined(CONFIG_SYSLOG_PROCESSID)
/* Prepend the Thread ID */ /* Prepend the Thread ID */
ret += lib_sprintf(&stream.public, "[%2d] ", gettid()); "[%2d] "
#endif #endif
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT) #if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Set the terminal style according to message priority. */ /* Set the terminal style according to message priority. */
ret += lib_sprintf(&stream.public, "%s", g_priority_color[priority]); "%s"
#endif #endif
#if defined(CONFIG_SYSLOG_PRIORITY) #if defined(CONFIG_SYSLOG_PRIORITY)
/* Prepend the message priority. */ /* Prepend the message priority. */
ret += lib_sprintf(&stream.public, "[%6s] ", g_priority_str[priority]); "[%6s] "
#endif #endif
#if defined(CONFIG_SYSLOG_PREFIX) #if defined(CONFIG_SYSLOG_PREFIX)
/* Prepend the prefix, if available */ /* Prepend the prefix, if available */
ret += lib_sprintf(&stream.public, "[%s] ", CONFIG_SYSLOG_PREFIX_STRING); "[%s] "
#endif
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME)
/* Prepend the thread name */
"%s: "
#endif
"%pV"
#ifdef CONFIG_SYSLOG_TIMESTAMP
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND)
, date_buf, ts.tv_nsec / NSEC_PER_USEC
# else
, date_buf
# endif
# else
, (uintmax_t)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC
# endif
#endif
#if defined(CONFIG_SMP)
, up_cpu_index()
#endif
#if defined(CONFIG_SYSLOG_PROCESSID)
/* Prepend the Thread ID */
, (int)gettid()
#endif
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Set the terminal style according to message priority. */
, g_priority_color[priority]
#endif
#if defined(CONFIG_SYSLOG_PRIORITY)
/* Prepend the message priority. */
, g_priority_str[priority]
#endif
#if defined(CONFIG_SYSLOG_PREFIX)
/* Prepend the prefix, if available */
, CONFIG_SYSLOG_PREFIX_STRING
#endif #endif
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME) #if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME)
/* Prepend the thread name */ /* Prepend the thread name */
ret += lib_sprintf(&stream.public, "%s: ", , tcb != NULL ? tcb->name : "(null)"
tcb != NULL ? tcb->name : "(null)");
#endif #endif
/* Generate the output */ /* Generate the output */
ret += lib_vsprintf(&stream.public, fmt, *ap); , &vaf);
if (stream.last_ch != '\n') if (stream.last_ch != '\n')
{ {
lib_stream_putc(&stream.public, '\n'); lib_stream_putc(&stream.public, '\n');
ret++;
} }
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT) #if defined(CONFIG_SYSLOG_COLOR_OUTPUT)