syslog: put variable initialization in the front

Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
This commit is contained in:
yinshengkai 2023-01-04 14:36:47 +08:00 committed by Xiang Xiao
parent 72b19200b5
commit 74b8776872

View File

@ -83,15 +83,14 @@ 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 = 0;
#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME) #if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME)
struct tcb_s *tcb; FAR struct tcb_s *tcb = nxsched_get_tcb(gettid());
#endif #endif
#ifdef CONFIG_SYSLOG_TIMESTAMP #ifdef CONFIG_SYSLOG_TIMESTAMP
struct timespec ts; struct timespec ts;
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED) # if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
int d_ret;
struct tm tm; struct tm tm;
char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER]; char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER];
#endif # endif
#endif #endif
/* 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
@ -104,9 +103,9 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
ts.tv_sec = 0; ts.tv_sec = 0;
ts.tv_nsec = 0; ts.tv_nsec = 0;
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED) # if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
memset(&tm, 0, sizeof(tm)); memset(&tm, 0, sizeof(tm));
#endif # endif
/* Get the current time. Since debug output may be generated very early /* Get the current time. Since debug output may be generated very early
* in the start-up sequence, hardware timer support may not yet be * in the start-up sequence, hardware timer support may not yet be
@ -115,53 +114,55 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
if (OSINIT_HW_READY()) if (OSINIT_HW_READY())
{ {
#if defined(CONFIG_SYSLOG_TIMESTAMP_REALTIME) # if defined(CONFIG_SYSLOG_TIMESTAMP_REALTIME)
/* 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.
*/ */
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
#endif # endif
/* Prepend the message with the current time, if available */ /* Prepend the message with the current time, if available */
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED) # if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
#if defined(CONFIG_SYSLOG_TIMESTAMP_LOCALTIME) # if defined(CONFIG_SYSLOG_TIMESTAMP_LOCALTIME)
localtime_r(&ts.tv_sec, &tm); localtime_r(&ts.tv_sec, &tm);
#else # else
gmtime_r(&ts.tv_sec, &tm); gmtime_r(&ts.tv_sec, &tm);
#endif # endif
#endif # endif
} }
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
date_buf[0] = '\0';
strftime(date_buf, CONFIG_SYSLOG_TIMESTAMP_BUFFER,
CONFIG_SYSLOG_TIMESTAMP_FORMAT, &tm);
# endif
#endif
#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"); ret = lib_sprintf(&stream.public, "\e[0m");
#endif #endif
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED) #ifdef CONFIG_SYSLOG_TIMESTAMP
d_ret = strftime(date_buf, CONFIG_SYSLOG_TIMESTAMP_BUFFER, # if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
CONFIG_SYSLOG_TIMESTAMP_FORMAT, &tm); # if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND)
ret += lib_sprintf(&stream.public, "[%s.%06ld] ",
if (d_ret > 0) date_buf, ts.tv_nsec / NSEC_PER_USEC);
{ # else
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND) ret += lib_sprintf(&stream.public, "[%s] ", date_buf);
ret += lib_sprintf(&stream.public, "[%s.%06ld] ", # endif
date_buf, ts.tv_nsec / NSEC_PER_USEC); # else
#else
ret += lib_sprintf(&stream.public, "[%s] ", date_buf);
#endif
}
#else
ret += lib_sprintf(&stream.public, "[%5jd.%06ld] ", ret += lib_sprintf(&stream.public, "[%5jd.%06ld] ",
(uintmax_t)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC); (uintmax_t)ts.tv_sec, ts.tv_nsec / NSEC_PER_USEC);
#endif # endif
#endif #endif
#if defined(CONFIG_SMP) #if defined(CONFIG_SMP)
@ -195,7 +196,6 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
#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 */
tcb = nxsched_get_tcb(gettid());
ret += lib_sprintf(&stream.public, "%s: ", ret += lib_sprintf(&stream.public, "%s: ",
tcb != NULL ? tcb->name : "(null)"); tcb != NULL ? tcb->name : "(null)");
#endif #endif