From 3a82d6e056e447bc69b16db1694387b7931b30bd Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sat, 1 Nov 2014 09:17:34 -0600 Subject: Add optional timestamp to syslog output. From pn_bouteville@yahoo.fr --- nuttx/arch/arm/src/armv7-m/etm.h | 2 +- nuttx/fs/Kconfig | 6 ++++ nuttx/libc/syslog/lib_syslog.c | 65 +++++++++++++++++++++++++++++++++------- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/nuttx/arch/arm/src/armv7-m/etm.h b/nuttx/arch/arm/src/armv7-m/etm.h index 5af02d45d..eef1ba85d 100644 --- a/nuttx/arch/arm/src/armv7-m/etm.h +++ b/nuttx/arch/arm/src/armv7-m/etm.h @@ -70,7 +70,7 @@ *******************************************************************************************************************************/ /* ETM Register Base Address ***************************************************************************************************/ -#define ETM_BASE +#define ETM_BASE (0xe0041000ul) /* ETM Register Offsets ********************************************************************************************************/ diff --git a/nuttx/fs/Kconfig b/nuttx/fs/Kconfig index 0dba7f80a..78d2fc89c 100644 --- a/nuttx/fs/Kconfig +++ b/nuttx/fs/Kconfig @@ -75,6 +75,12 @@ config SYSLOG if SYSLOG +config SYSLOG_TIMESTAMP + bool "Prepend timestamp to syslog message" + default y + ---help--- + Prepend timestamp to syslog message. + config SYSLOG_CHAR bool "System log character device support" default y diff --git a/nuttx/libc/syslog/lib_syslog.c b/nuttx/libc/syslog/lib_syslog.c index 63441fd9e..70e8e13de 100644 --- a/nuttx/libc/syslog/lib_syslog.c +++ b/nuttx/libc/syslog/lib_syslog.c @@ -42,6 +42,7 @@ #include #include +#include #include #include "syslog/syslog.h" @@ -94,41 +95,85 @@ static inline int vsyslog_internal(FAR const char *fmt, va_list ap) { #if defined(CONFIG_SYSLOG) - struct lib_outstream_s stream; +#elif CONFIG_NFILE_DESCRIPTORS > 0 + struct lib_rawoutstream_s stream; +#elif defined(CONFIG_ARCH_LOWPUTC) + struct lib_outstream_s stream; +#endif +#if defined(CONFIG_SYSLOG_TIMESTAMP) + struct timespec ts; + int ret; + + /* Get the current time */ + + ret = clock_systimespec(&ts); +#endif + +#if defined(CONFIG_SYSLOG) /* Wrap the low-level output in a stream object and let lib_vsprintf * do the work. */ lib_syslogstream((FAR struct lib_outstream_s *)&stream); - return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, ap); -#elif CONFIG_NFILE_DESCRIPTORS > 0 +#if defined(CONFIG_SYSLOG_TIMESTAMP) + /* Pre-pend the message with the current time */ - struct lib_rawoutstream_s rawoutstream; + if (ret == OK) + { + (void)lib_sprintf((FAR struct lib_outstream_s *)&stream, + "[%6d.%06d]", + ts.tv_sec, ts.tv_nsec/1000); + } +#endif + return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, ap); + +#elif CONFIG_NFILE_DESCRIPTORS > 0 /* Wrap the stdout in a stream object and let lib_vsprintf * do the work. */ - lib_rawoutstream(&rawoutstream, 1); - return lib_vsprintf(&rawoutstream.public, fmt, ap); + lib_rawoutstream(&stream, 1); -#elif defined(CONFIG_ARCH_LOWPUTC) +#if defined(CONFIG_SYSLOG_TIMESTAMP) + /* Pre-pend the message with the current time */ - struct lib_outstream_s stream; + if (ret == OK) + { + (void)lib_sprintf((FAR struct lib_outstream_s *)&stream, + "[%6d.%06d]", + ts.tv_sec, ts.tv_nsec/1000); + } +#endif + + return lib_vsprintf(&stream.public, fmt, ap); +#elif defined(CONFIG_ARCH_LOWPUTC) /* Wrap the low-level output in a stream object and let lib_vsprintf * do the work. */ lib_lowoutstream((FAR struct lib_outstream_s *)&stream); + +#if defined(CONFIG_SYSLOG_TIMESTAMP) + /* Pre-pend the message with the current time */ + + if (ret == OK) + { + (void)lib_sprintf((FAR struct lib_outstream_s *)&stream, + "[%6d.%06d]", + ts.tv_sec, ts.tv_nsec/1000); + } +#endif + return lib_vsprintf((FAR struct lib_outstream_s *)&stream, fmt, ap); -#else +#else /* CONFIG_SYSLOG */ return 0; -#endif +#endif /* CONFIG_SYSLOG */ } /**************************************************************************** -- cgit v1.2.3