From 20a6ad7918aa5e2ace692ea1c5abdb1897455b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fin=20Maa=C3=9F?= Date: Mon, 9 Sep 2024 16:13:34 +0200 Subject: [PATCH] logging: add formated timestamp for realtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds a timestamp format mode, that includes the date, which is usefull, when using realtime for the logging timestamp. Signed-off-by: Fin Maaß --- subsys/logging/Kconfig.formatting | 19 ++++++++++++++++++- subsys/logging/log_output.c | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/subsys/logging/Kconfig.formatting b/subsys/logging/Kconfig.formatting index 93dfb113079..0a8f1d307f7 100644 --- a/subsys/logging/Kconfig.formatting +++ b/subsys/logging/Kconfig.formatting @@ -175,7 +175,22 @@ config LOG_BACKEND_FORMAT_TIMESTAMP depends on LOG_BACKEND_SUPPORTS_FORMAT_TIMESTAMP default y help - When enabled timestamp is formatted to hh:mm:ss:ms,us. + When enabled timestamp is formatted. + +choice LOG_BACKEND_FORMAT_TIMESTAMP_MODE + prompt "Timestamp format mode" + default LOG_OUTPUT_FORMAT_DATE_TIMESTAMP if LOG_TIMESTAMP_USE_REALTIME + default LOG_OUTPUT_FORMAT_TIME_TIMESTAMP + +config LOG_OUTPUT_FORMAT_TIME_TIMESTAMP + bool "Format timestamp in time format" + help + When enabled timestamp is formatted to hh:mm:ss.ms,us. + +config LOG_OUTPUT_FORMAT_DATE_TIMESTAMP + bool "Format timestamp in date format" + help + When enabled timestamp is formatted to YYYY-MM-DD hh:mm:ss.ms,us. config LOG_OUTPUT_FORMAT_LINUX_TIMESTAMP bool "Format timestamp in Linux format" @@ -191,4 +206,6 @@ config LOG_OUTPUT_FORMAT_CUSTOM_TIMESTAMP Enable support for custom formatter for the timestamp. It will be applied to all backends except the syslog net backend. +endchoice + endmenu diff --git a/subsys/logging/log_output.c b/subsys/logging/log_output.c index a2d6557d062..221fb116740 100644 --- a/subsys/logging/log_output.c +++ b/subsys/logging/log_output.c @@ -286,6 +286,28 @@ static int timestamp_print(const struct log_output *output, "[%5lu.%06d] ", #endif total_seconds, ms * 1000U + us); + } else if (IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_DATE_TIMESTAMP)) { +#if defined(CONFIG_REQUIRES_FULL_LIBC) + char time_str[sizeof("1970-01-01 00:00:00")]; + struct tm *tm_timestamp; + time_t time_seconds = total_seconds; + + tm_timestamp = gmtime(&time_seconds); + + strftime(time_str, sizeof(time_str), "%F %T", tm_timestamp); + + length = print_formatted(output, "[%s.%03u,%03u] ", time_str, ms, + us); +#else + struct YMD_date date; + + get_YMD_from_seconds(total_seconds, &date); + hours = hours % 24; + length = print_formatted( + output, "[%04u-%02u-%02u %02u:%02u:%02u.%03u,%03u] ", + date.year, date.month, date.day, hours, mins, seconds, ms, + us); +#endif } else { length = print_formatted(output, "[%02u:%02u:%02u.%03u,%03u] ",