This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 19fa9c1c6733cb987c9268d8ef2025c7289b4ed0
Author: liwangzhu <[email protected]>
AuthorDate: Fri Sep 12 10:47:25 2025 +0800

    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 <[email protected]>
    Signed-off-by: chao an <[email protected]>
---
 drivers/syslog/Kconfig   | 8 ++++++++
 drivers/syslog/vsyslog.c | 9 +++++++++
 2 files changed, 17 insertions(+)

diff --git a/drivers/syslog/Kconfig b/drivers/syslog/Kconfig
index d6c6bbd9b27..30a87feddda 100644
--- a/drivers/syslog/Kconfig
+++ b/drivers/syslog/Kconfig
@@ -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
diff --git a/drivers/syslog/vsyslog.c b/drivers/syslog/vsyslog.c
index 21a1bfe77f5..b930245a95e 100644
--- a/drivers/syslog/vsyslog.c
+++ b/drivers/syslog/vsyslog.c
@@ -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
 

Reply via email to