logging: support using real time as timestamp

Adds a option to use real time from posix instant
of the system uptime as the logging timestamp.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
This commit is contained in:
Fin Maaß 2024-02-23 11:26:59 +01:00 committed by Fabio Baltieri
parent c00bd4a8d5
commit abdfc4b1fe
2 changed files with 26 additions and 0 deletions

View file

@ -158,6 +158,13 @@ config LOG_TRACE_SHORT_TIMESTAMP
config LOG_TIMESTAMP_64BIT
bool "Use 64 bit timestamp"
config LOG_TIMESTAMP_USE_REALTIME
bool "Use real time clock for timestamp"
select LOG_TIMESTAMP_64BIT
depends on POSIX_CLOCK
help
When enabled, real time clock is used for timestamping.
config LOG_SPEED
bool "Prefer performance over size"
depends on LOG_MODE_DEFERRED

View file

@ -23,6 +23,10 @@
#include <zephyr/logging/log_output_custom.h>
#include <zephyr/linker/utils.h>
#ifdef CONFIG_LOG_TIMESTAMP_USE_REALTIME
#include <zephyr/posix/time.h>
#endif
LOG_MODULE_REGISTER(log);
#ifndef CONFIG_LOG_PROCESS_THREAD_SLEEP_MS
@ -221,6 +225,7 @@ void z_log_vprintk(const char *fmt, va_list ap)
fmt, ap);
}
#ifndef CONFIG_LOG_TIMESTAMP_USE_REALTIME
static log_timestamp_t default_get_timestamp(void)
{
return IS_ENABLED(CONFIG_LOG_TIMESTAMP_64BIT) ?
@ -232,6 +237,16 @@ static log_timestamp_t default_lf_get_timestamp(void)
return IS_ENABLED(CONFIG_LOG_TIMESTAMP_64BIT) ?
k_uptime_get() : k_uptime_get_32();
}
#else
static log_timestamp_t default_rt_get_timestamp(void)
{
struct timespec tspec;
clock_gettime(CLOCK_REALTIME, &tspec);
return ((uint64_t)tspec.tv_sec * MSEC_PER_SEC) + (tspec.tv_nsec / NSEC_PER_MSEC);
}
#endif /* CONFIG_LOG_TIMESTAMP_USE_REALTIME */
void log_core_init(void)
{
@ -252,6 +267,9 @@ void log_core_init(void)
}
/* Set default timestamp. */
#ifdef CONFIG_LOG_TIMESTAMP_USE_REALTIME
log_set_timestamp_func(default_rt_get_timestamp, 1000U);
#else
if (sys_clock_hw_cycles_per_sec() > 1000000) {
log_set_timestamp_func(default_lf_get_timestamp, 1000U);
} else {
@ -259,6 +277,7 @@ void log_core_init(void)
CONFIG_SYS_CLOCK_TICKS_PER_SEC : sys_clock_hw_cycles_per_sec();
log_set_timestamp_func(default_get_timestamp, freq);
}
#endif /* CONFIG_LOG_TIMESTAMP_USE_REALTIME */
if (IS_ENABLED(CONFIG_LOG_MODE_DEFERRED)) {
z_log_msg_init();