Added formatted timestamps in syslog.

This commit is contained in:
Fotis Panagiotopoulos 2020-12-29 16:23:27 +02:00 committed by Xiang Xiao
parent 4e6adefc2b
commit c11413a258
2 changed files with 53 additions and 0 deletions

View File

@ -111,6 +111,37 @@ config SYSLOG_TIMESTAMP_REALTIME
CLOCK_MONOTONIC, if enabled, will be used or the system timer
is not.
config SYSLOG_TIMESTAMP_FORMATTED
bool "Formatted syslog time"
default n
depends on SYSLOG_TIMESTAMP_REALTIME
---help---
Syslog timestamp will be formatted according to the
SYSLOG_TIMESTAMP_FORMAT format string.
config SYSLOG_TIMESTAMP_LOCALTIME
bool "Use local-time timestamp"
default n
depends on SYSLOG_TIMESTAMP_FORMATTED
---help---
If selected local time will be used for the timestamps.
Else, timestamps will be in UTC.
config SYSLOG_TIMESTAMP_FORMAT
string "Time format"
default "%e/%m/%y %H:%M:%S"
depends on SYSLOG_TIMESTAMP_FORMATTED
---help---
Formatter string for syslog timestamp printing.
Uses the standard "strftime" format specifiers.
config SYSLOG_TIMESTAMP_BUFFER
int "Formatted timestamp buffer size"
default 64
depends on SYSLOG_TIMESTAMP_FORMATTED
---help---
Buffer size to store syslog formatted timestamps.
config SYSLOG_PREFIX
bool "Prepend prefix to syslog message"
default n

View File

@ -71,6 +71,11 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
{
struct lib_syslogstream_s stream;
int ret;
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
time_t time;
struct tm tm;
char date_buf[CONFIG_SYSLOG_TIMESTAMP_BUFFER];
#endif
#ifdef CONFIG_SYSLOG_TIMESTAMP
struct timespec ts;
@ -132,8 +137,25 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
#if defined(CONFIG_SYSLOG_TIMESTAMP)
/* Pre-pend the message with the current time, if available */
#if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
time = ts.tv_sec;
#if defined(CONFIG_SYSLOG_TIMESTAMP_LOCALTIME)
localtime_r(&time, &tm);
#else
gmtime_r(&time, &tm);
#endif
ret = strftime(date_buf, CONFIG_SYSLOG_TIMESTAMP_BUFFER,
CONFIG_SYSLOG_TIMESTAMP_FORMAT, &tm);
if (ret > 0)
{
ret = lib_sprintf(&stream.public, "[%s] ", date_buf);
}
#else
ret = lib_sprintf(&stream.public, "[%5jd.%06ld] ",
(uintmax_t)ts.tv_sec, ts.tv_nsec / 1000);
#endif
#else
ret = 0;
#endif