mirror of
https://github.com/apache/nuttx.git
synced 2026-05-31 23:40:19 +08:00
syslog: fix crash when print localtime by syslog
Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
+44
-50
@@ -67,53 +67,22 @@ static FAR const char * 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;
|
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;
|
struct tcb_s *tcb;
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef CONFIG_SYSLOG_TIMESTAMP
|
||||||
|
struct timespec ts =
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
|
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
|
||||||
time_t time;
|
struct tm tm =
|
||||||
struct tm tm;
|
{
|
||||||
|
};
|
||||||
|
|
||||||
char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER];
|
char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_SYSLOG_TIMESTAMP
|
|
||||||
struct timespec ts;
|
|
||||||
|
|
||||||
/* Get the current time. Since debug output may be generated very early
|
|
||||||
* in the start-up sequence, hardware timer support may not yet be
|
|
||||||
* available.
|
|
||||||
*/
|
|
||||||
|
|
||||||
ret = -EAGAIN;
|
|
||||||
if (OSINIT_HW_READY())
|
|
||||||
{
|
|
||||||
#if defined(CONFIG_SYSLOG_TIMESTAMP_REALTIME)
|
|
||||||
/* Use CLOCK_REALTIME if so configured */
|
|
||||||
|
|
||||||
ret = clock_gettime(CLOCK_REALTIME, &ts);
|
|
||||||
|
|
||||||
#elif defined(CONFIG_CLOCK_MONOTONIC)
|
|
||||||
/* Prefer monotonic when enabled, as it can be synchronized to
|
|
||||||
* RTC with clock_resynchronize.
|
|
||||||
*/
|
|
||||||
|
|
||||||
ret = clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
||||||
|
|
||||||
#else
|
|
||||||
/* Otherwise, fall back to the system timer */
|
|
||||||
|
|
||||||
ret = clock_systime_timespec(&ts);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret < 0)
|
|
||||||
{
|
|
||||||
/* Timer hardware is not available, or clock function failed */
|
|
||||||
|
|
||||||
ts.tv_sec = 0;
|
|
||||||
ts.tv_nsec = 0;
|
|
||||||
}
|
|
||||||
#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
|
||||||
@@ -122,17 +91,44 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
|
|||||||
|
|
||||||
syslogstream_create(&stream);
|
syslogstream_create(&stream);
|
||||||
|
|
||||||
#if defined(CONFIG_SYSLOG_TIMESTAMP)
|
#ifdef CONFIG_SYSLOG_TIMESTAMP
|
||||||
/* Prepend the message with the current time, if available */
|
/* Get the current time. Since debug output may be generated very early
|
||||||
|
* in the start-up sequence, hardware timer support may not yet be
|
||||||
|
* available.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (OSINIT_HW_READY())
|
||||||
|
{
|
||||||
|
#if defined(CONFIG_SYSLOG_TIMESTAMP_REALTIME)
|
||||||
|
/* Use CLOCK_REALTIME if so configured */
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_REALTIME, &ts);
|
||||||
|
|
||||||
|
#elif defined(CONFIG_CLOCK_MONOTONIC)
|
||||||
|
/* Prefer monotonic when enabled, as it can be synchronized to
|
||||||
|
* RTC with clock_resynchronize.
|
||||||
|
*/
|
||||||
|
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||||
|
|
||||||
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
|
|
||||||
time = ts.tv_sec;
|
|
||||||
#if defined(CONFIG_SYSLOG_TIMESTAMP_LOCALTIME)
|
|
||||||
localtime_r(&time, &tm);
|
|
||||||
#else
|
#else
|
||||||
gmtime_r(&time, &tm);
|
/* Otherwise, fall back to the system timer */
|
||||||
|
|
||||||
|
clock_systime_timespec(&ts);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Prepend the message with the current time, if available */
|
||||||
|
|
||||||
|
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
|
||||||
|
#if defined(CONFIG_SYSLOG_TIMESTAMP_LOCALTIME)
|
||||||
|
localtime_r(&ts.tv_sec, &tm);
|
||||||
|
#else
|
||||||
|
gmtime_r(&ts.tv_sec, &tm);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
|
||||||
ret = strftime(date_buf, CONFIG_SYSLOG_TIMESTAMP_BUFFER,
|
ret = strftime(date_buf, CONFIG_SYSLOG_TIMESTAMP_BUFFER,
|
||||||
CONFIG_SYSLOG_TIMESTAMP_FORMAT, &tm);
|
CONFIG_SYSLOG_TIMESTAMP_FORMAT, &tm);
|
||||||
|
|
||||||
@@ -144,8 +140,6 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
|
|||||||
ret = lib_sprintf(&stream.public, "[%5jd.%06ld] ",
|
ret = lib_sprintf(&stream.public, "[%5jd.%06ld] ",
|
||||||
(uintmax_t)ts.tv_sec, ts.tv_nsec / 1000);
|
(uintmax_t)ts.tv_sec, ts.tv_nsec / 1000);
|
||||||
#endif
|
#endif
|
||||||
#else
|
|
||||||
ret = 0;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(CONFIG_SMP)
|
#if defined(CONFIG_SMP)
|
||||||
|
|||||||
Reference in New Issue
Block a user