drivers/syslog: add millisecond option for syslog timestamp formatting

This commit introduces a new Kconfig option SYSLOG_TIMESTAMP_MS to allowconfiguring
syslog timestamps to use milliseconds (ms) instead of the default microseconds (µs).

Key changes:
1. Add CONFIG_SYSLOG_TIMESTAMP_MS boolean Kconfig option (depends on SYSLOG_TIMESTAMP),
   default disabled. This option lets users choose between millisecond and microsecond
   precision in syslog timestamps.

2. Modify the timestamp format string in nx_vsyslog():
  a. When CONFIG_SYSLOG_TIMESTAMP_MS is enabled: Use [%5ju.%03ld] (3 digits for ms).
  b. When disabled (default): Retain original [%5ju.%06ld] (6 digits for µs).

3. Adjust the timestamp value calculation:
  a. For ms: Divide nanoseconds by NSEC_PER_MSEC (1,000,000) to get millisecond value.
  b. For µs (default): Retain division by NSEC_PER_USEC (1,000) for microsecond value.

This enhancement provides flexibility in syslog timestamp precision:

1. Millisecond precision reduces log line length and is sufficient for many use cases.
2. Maintains backward compatibility (microseconds remain the default).
3. The Kconfig dependency ensures the option is only visible when timestamps are enabled.

The change is fully conditional and has no impact on existing behavior unless the new option is explicitly enabled.

Signed-off-by: liwangzhu <liwangzhu@bytedance.com>
Signed-off-by: chao an <anchao.archer@bytedance.com>
This commit is contained in:
liwangzhu
2025-09-12 10:47:25 +08:00
committed by Alan C. Assis
parent 635f64ebb8
commit 19fa9c1c67
2 changed files with 17 additions and 0 deletions
+8
View File
@@ -174,6 +174,14 @@ if !SYSLOG_RFC5424
comment "Regular syslog formatting options"
config SYSLOG_TIMESTAMP_MS
bool "Use milliseconds for syslog timestamp"
default n
depends on SYSLOG_TIMESTAMP
---help---
Use milliseconds for timestamp. By default,
microsecond will be used.
config SYSLOG_TIMESTAMP_REALTIME
bool "Use wall-clock for syslog timestamp"
default n
+9
View File
@@ -167,7 +167,11 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
"[%s] "
# endif
# else
# if defined(CONFIG_SYSLOG_TIMESTAMP_MS)
"[%5ju.%03ld] "
# else
"[%5ju.%06ld] "
# endif
# endif
#endif
@@ -211,8 +215,13 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
, date_buf
# endif
# else
# if defined(CONFIG_SYSLOG_TIMESTAMP_MS)
, (uintmax_t)ts.tv_sec
, ts.tv_nsec / NSEC_PER_MSEC
# else
, (uintmax_t)ts.tv_sec
, ts.tv_nsec / NSEC_PER_USEC
# endif
# endif
#endif