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

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)
{
struct lib_syslogstream_s stream;
int ret = 0;
int ret;
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME)
FAR struct tcb_s *tcb = nxsched_get_tcb(gettid());
#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];
# 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
* 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 */
clock_gettime(CLOCK_REALTIME, &ts);
# else
/* Prefer monotonic when enabled, as it can be synchronized to
* RTC with clock_resynchronize.
@ -145,68 +147,112 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
# endif
#endif
ret = lib_sprintf(&stream.public,
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Reset the terminal style. */
ret = lib_sprintf(&stream.public, "\e[0m");
"\e[0m"
#endif
#ifdef CONFIG_SYSLOG_TIMESTAMP
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND)
ret += lib_sprintf(&stream.public, "[%s.%06ld] ",
date_buf, ts.tv_nsec / NSEC_PER_USEC);
"[%s.%06ld] "
# else
ret += lib_sprintf(&stream.public, "[%s] ", date_buf);
"[%s] "
# endif
# else
ret += lib_sprintf(&stream.public, "[%5jd.%06ld] ",
(uintmax_t)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
"[%5jd.%06ld] "
# endif
#endif
#if defined(CONFIG_SMP)
ret += lib_sprintf(&stream.public, "[CPU%d] ", up_cpu_index());
"[CPU%d] "
#endif
#if defined(CONFIG_SYSLOG_PROCESSID)
/* Prepend the Thread ID */
ret += lib_sprintf(&stream.public, "[%2d] ", gettid());
"[%2d] "
#endif
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
/* Set the terminal style according to message priority. */
ret += lib_sprintf(&stream.public, "%s", g_priority_color[priority]);
"%s"
#endif
#if defined(CONFIG_SYSLOG_PRIORITY)
/* Prepend the message priority. */
ret += lib_sprintf(&stream.public, "[%6s] ", g_priority_str[priority]);
"[%6s] "
#endif
#if defined(CONFIG_SYSLOG_PREFIX)
/* 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
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME)
/* Prepend the thread name */
ret += lib_sprintf(&stream.public, "%s: ",
tcb != NULL ? tcb->name : "(null)");
, tcb != NULL ? tcb->name : "(null)"
#endif
/* Generate the output */
ret += lib_vsprintf(&stream.public, fmt, *ap);
, &vaf);
if (stream.last_ch != '\n')
{
lib_stream_putc(&stream.public, '\n');
ret++;
}
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)