logging: add formated timestamp for realtime

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ß <f.maass@vogl-electronic.com>
This commit is contained in:
Fin Maaß 2024-09-09 16:13:34 +02:00 committed by Mahesh Mahadevan
parent 683ae11510
commit 20a6ad7918
2 changed files with 40 additions and 1 deletions

View File

@ -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

View File

@ -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] ",