diff --git a/subsys/logging/Kconfig.formatting b/subsys/logging/Kconfig.formatting index 609b3c41786..671de0e9fba 100644 --- a/subsys/logging/Kconfig.formatting +++ b/subsys/logging/Kconfig.formatting @@ -178,7 +178,7 @@ config LOG_BACKEND_FORMAT_TIMESTAMP 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_DATE_TIMESTAMP if LOG_TIMESTAMP_USE_REALTIME && POSIX_C_LANG_SUPPORT_R default LOG_OUTPUT_FORMAT_TIME_TIMESTAMP config LOG_OUTPUT_FORMAT_TIME_TIMESTAMP @@ -188,11 +188,13 @@ config LOG_OUTPUT_FORMAT_TIME_TIMESTAMP config LOG_OUTPUT_FORMAT_DATE_TIMESTAMP bool "Format timestamp in date format" + depends on POSIX_C_LANG_SUPPORT_R help When enabled timestamp is formatted to YYYY-MM-DD hh:mm:ss.ms,us. config LOG_OUTPUT_FORMAT_ISO8601_TIMESTAMP bool "Format timestamp in ISO 8601 format" + depends on POSIX_C_LANG_SUPPORT_R help When enabled timestamp is formatted to YYYY-MM-DDThh:mm:ss,ffffffZ. diff --git a/subsys/logging/backends/Kconfig.net b/subsys/logging/backends/Kconfig.net index 67491dafa92..23ee19fb448 100644 --- a/subsys/logging/backends/Kconfig.net +++ b/subsys/logging/backends/Kconfig.net @@ -6,6 +6,7 @@ config LOG_BACKEND_NET bool "Networking backend" depends on NETWORKING && (NET_UDP || NET_TCP) && NET_SOCKETS && !LOG_MODE_IMMEDIATE + depends on POSIX_C_LANG_SUPPORT_R help Send syslog messages to network server. See RFC 5424 (syslog protocol) and RFC 5426 (syslog over UDP) and diff --git a/subsys/logging/log_output.c b/subsys/logging/log_output.c index 7b5e02fd75a..c5c512bb1e9 100644 --- a/subsys/logging/log_output.c +++ b/subsys/logging/log_output.c @@ -50,17 +50,6 @@ static const char *const colors[] = { static uint32_t freq; static log_timestamp_t timestamp_div; -#define SECONDS_IN_DAY 86400U - -static uint32_t days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, - 31, 30, 31, 30, 31}; - -struct YMD_date { - uint32_t year; - uint32_t month; - uint32_t day; -}; - /* The RFC 5424 allows very flexible mapping and suggest the value 0 being the * highest severity and 7 to be the lowest (debugging level) severity. * @@ -150,46 +139,6 @@ static int print_formatted(const struct log_output *output, return length; } -static inline bool is_leap_year(uint32_t year) -{ - return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)); -} - -static void __attribute__((unused)) get_YMD_from_seconds(uint64_t seconds, - struct YMD_date *output_date) -{ - uint64_t tmp; - int i; - - output_date->year = 1970; - output_date->month = 1; - output_date->day = 1; - - /* compute the proper year */ - while (1) { - tmp = (is_leap_year(output_date->year)) ? - 366*SECONDS_IN_DAY : 365*SECONDS_IN_DAY; - if (tmp > seconds) { - break; - } - seconds -= tmp; - output_date->year++; - } - /* compute the proper month */ - for (i = 0; i < ARRAY_SIZE(days_in_month); i++) { - tmp = ((i == 1) && is_leap_year(output_date->year)) ? - ((uint64_t)days_in_month[i] + 1) * SECONDS_IN_DAY : - (uint64_t)days_in_month[i] * SECONDS_IN_DAY; - if (tmp > seconds) { - output_date->month += i; - break; - } - seconds -= tmp; - } - - output_date->day += seconds / SECONDS_IN_DAY; -} - static int timestamp_print(const struct log_output *output, uint32_t flags, log_timestamp_t timestamp) { @@ -233,27 +182,27 @@ static int timestamp_print(const struct log_output *output, us = (1000 * (remainder * 1000U - (ms * freq))) / freq; if (IS_ENABLED(CONFIG_LOG_BACKEND_NET) && flags & LOG_OUTPUT_FLAG_FORMAT_SYSLOG) { -#if defined(CONFIG_REQUIRES_FULL_LIBC) - char time_str[sizeof("1970-01-01T00:00:00")]; +#if defined(CONFIG_POSIX_C_LANG_SUPPORT_R) struct tm tm_timestamp = {0}; time_t time_seconds = total_seconds; gmtime_r(&time_seconds, &tm_timestamp); +#if defined(CONFIG_REQUIRES_FULL_LIBC) + char time_str[sizeof("1970-01-01T00:00:00")]; strftime(time_str, sizeof(time_str), "%FT%T", &tm_timestamp); length = print_formatted(output, "%s.%06uZ ", time_str, ms * 1000U + us); -#else - struct YMD_date date; - - get_YMD_from_seconds(total_seconds, &date); - hours = hours % 24; +#else /* CONFIG_REQUIRES_FULL_LIBC */ length = print_formatted(output, "%04u-%02u-%02uT%02u:%02u:%02u.%06uZ ", - date.year, date.month, date.day, - hours, mins, seconds, ms * 1000U + us); -#endif + tm_timestamp.tm_year + 1900, tm_timestamp.tm_mon + 1, + tm_timestamp.tm_mday, tm_timestamp.tm_hour, + tm_timestamp.tm_min, tm_timestamp.tm_sec, + ms * 1000U + us); +#endif /* CONFIG_REQUIRES_FULL_LIBC */ +#endif /* CONFIG_POSIX_C_LANG_SUPPORT_R */ } else if (IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_CUSTOM_TIMESTAMP)) { length = log_custom_timestamp_print(output, timestamp, print_formatted); } else { @@ -265,50 +214,49 @@ static int timestamp_print(const struct log_output *output, "[%5lu.%06d] ", #endif total_seconds, ms * 1000U + us); +#if defined(CONFIG_POSIX_C_LANG_SUPPORT_R) } 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 = {0}; time_t time_seconds = total_seconds; gmtime_r(&time_seconds, &tm_timestamp); +#if defined(CONFIG_REQUIRES_FULL_LIBC) + char time_str[sizeof("1970-01-01 00:00:00")]; 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; +#else /* CONFIG_REQUIRES_FULL_LIBC */ length = print_formatted( output, "[%04u-%02u-%02u %02u:%02u:%02u.%03u,%03u] ", - date.year, date.month, date.day, hours, mins, seconds, ms, - us); -#endif + tm_timestamp.tm_year + 1900, tm_timestamp.tm_mon + 1, + tm_timestamp.tm_mday, tm_timestamp.tm_hour, + tm_timestamp.tm_min, tm_timestamp.tm_sec, + ms, us); +#endif /* CONFIG_REQUIRES_FULL_LIBC */ } else if (IS_ENABLED(CONFIG_LOG_OUTPUT_FORMAT_ISO8601_TIMESTAMP)) { -#if defined(CONFIG_REQUIRES_FULL_LIBC) - char time_str[sizeof("1970-01-01T00:00:00")]; struct tm tm_timestamp = {0}; time_t time_seconds = total_seconds; gmtime_r(&time_seconds, &tm_timestamp); +#if defined(CONFIG_REQUIRES_FULL_LIBC) + char time_str[sizeof("1970-01-01T00:00:00")]; strftime(time_str, sizeof(time_str), "%FT%T", &tm_timestamp); length = print_formatted(output, "[%s,%06uZ] ", time_str, ms * 1000U + us); -#else - struct YMD_date date; - - get_YMD_from_seconds(total_seconds, &date); - hours = hours % 24; +#else /* CONFIG_REQUIRES_FULL_LIBC */ length = print_formatted(output, "[%04u-%02u-%02uT%02u:%02u:%02u,%06uZ] ", - date.year, date.month, date.day, hours, - mins, seconds, ms * 1000U + us); -#endif + tm_timestamp.tm_year + 1900, + tm_timestamp.tm_mon + 1, + tm_timestamp.tm_mday, tm_timestamp.tm_hour, + tm_timestamp.tm_min, tm_timestamp.tm_sec, + ms * 1000U + us); +#endif /* CONFIG_REQUIRES_FULL_LIBC */ +#endif /* CONFIG_POSIX_C_LANG_SUPPORT_R */ } else { length = print_formatted(output, "[%02u:%02u:%02u.%03u,%03u] ", diff --git a/tests/subsys/logging/log_output/testcase.yaml b/tests/subsys/logging/log_output/testcase.yaml index ff83e13cb3e..b7603f845c3 100644 --- a/tests/subsys/logging/log_output/testcase.yaml +++ b/tests/subsys/logging/log_output/testcase.yaml @@ -22,6 +22,7 @@ tests: extra_configs: - CONFIG_LOG_TIMESTAMP_64BIT=y - CONFIG_LOG_OUTPUT_FORMAT_DATE_TIMESTAMP=y + - CONFIG_POSIX_C_LANG_SUPPORT_R=y logging.output.ts64.date.fulllibc: tags: - log_output @@ -29,6 +30,7 @@ tests: extra_configs: - CONFIG_LOG_TIMESTAMP_64BIT=y - CONFIG_LOG_OUTPUT_FORMAT_DATE_TIMESTAMP=y + - CONFIG_POSIX_C_LANG_SUPPORT_R=y - CONFIG_REQUIRES_FULL_LIBC=y filter: CONFIG_FULL_LIBC_SUPPORTED logging.output.ts64.iso8601: @@ -38,6 +40,7 @@ tests: extra_configs: - CONFIG_LOG_TIMESTAMP_64BIT=y - CONFIG_LOG_OUTPUT_FORMAT_ISO8601_TIMESTAMP=y + - CONFIG_POSIX_C_LANG_SUPPORT_R=y logging.output.ts64.iso8601.fulllibc: tags: - log_output @@ -45,6 +48,7 @@ tests: extra_configs: - CONFIG_LOG_TIMESTAMP_64BIT=y - CONFIG_LOG_OUTPUT_FORMAT_ISO8601_TIMESTAMP=y + - CONFIG_POSIX_C_LANG_SUPPORT_R=y - CONFIG_REQUIRES_FULL_LIBC=y filter: CONFIG_FULL_LIBC_SUPPORTED logging.output.thread_id: diff --git a/tests/subsys/logging/log_output_net/prj.conf b/tests/subsys/logging/log_output_net/prj.conf index a24e85268d1..a9c3393e8b9 100644 --- a/tests/subsys/logging/log_output_net/prj.conf +++ b/tests/subsys/logging/log_output_net/prj.conf @@ -13,3 +13,4 @@ CONFIG_LOG_MODE_DEFERRED=y CONFIG_NET_SOCKETS=y CONFIG_ENTROPY_GENERATOR=y CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_POSIX_C_LANG_SUPPORT_R=y