From 6f7684ed8ba9f6693c7a1d2365e6dc042f4fbabc Mon Sep 17 00:00:00 2001 From: Abtin Keshavarzian Date: Fri, 27 Aug 2021 16:07:25 -0700 Subject: [PATCH] [logging] add `CONFIG_LOG_PREPEND_UPTIME` to prepend uptime to the log (#6968) This commit adds `OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME` config to prepend the current uptime (as "[x days] hh::mm::ss.mmmm") to the OpenThread log messages. --- src/core/common/logging.cpp | 15 ++++++++++++++- src/core/config/logging.h | 10 ++++++++++ tests/toranj/openthread-core-toranj-config.h | 12 ++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/core/common/logging.cpp b/src/core/common/logging.cpp index 1c40407e0..8037e5f1f 100644 --- a/src/core/common/logging.cpp +++ b/src/core/common/logging.cpp @@ -43,7 +43,15 @@ * It is reasonable to only enable the debug uart and not enable logs to the DEBUG uart. */ #if (OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_DEBUG_UART) && (!OPENTHREAD_CONFIG_ENABLE_DEBUG_UART) -#error OPENTHREAD_CONFIG_ENABLE_DEBUG_UART_LOG requires OPENTHREAD_CONFIG_ENABLE_DEBUG_UART +#error "OPENTHREAD_CONFIG_ENABLE_DEBUG_UART_LOG requires OPENTHREAD_CONFIG_ENABLE_DEBUG_UART" +#endif + +#if OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME && !OPENTHREAD_CONFIG_UPTIME_ENABLE +#error "OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME requires OPENTHREAD_CONFIG_UPTIME_ENABLE" +#endif + +#if OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME && OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE +#error "OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME is not supported under OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE" #endif #ifdef __cplusplus @@ -56,6 +64,11 @@ static void Log(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aForma { ot::String logString; +#if OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME + ot::Uptime::UptimeToString(ot::Instance::Get().Get().GetUptime(), logString); + logString.Append(" "); +#endif + #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE VerifyOrExit(otLoggingGetLevel() >= aLogLevel); #endif diff --git a/src/core/config/logging.h b/src/core/config/logging.h index 3af655332..509bb73ec 100644 --- a/src/core/config/logging.h +++ b/src/core/config/logging.h @@ -339,6 +339,16 @@ #define OPENTHREAD_CONFIG_LOG_DNS 1 #endif +/** + * @def OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME + * + * Define as 1 to prepend the current uptime to all log messages. + * + */ +#ifndef OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME +#define OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME 0 +#endif + /** * @def OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL * diff --git a/tests/toranj/openthread-core-toranj-config.h b/tests/toranj/openthread-core-toranj-config.h index 1c15a6c8f..959b0aadd 100644 --- a/tests/toranj/openthread-core-toranj-config.h +++ b/tests/toranj/openthread-core-toranj-config.h @@ -288,13 +288,21 @@ */ #define OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE 1 +/** + * @def OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME + * + * Define as 1 to prepend the current uptime to all log messages. + * + */ +#define OPENTHREAD_CONFIG_LOG_PREPEND_UPTIME 1 + /** * @def OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL * * Define to prepend the log level to all log messages * */ -#define OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL 0 +#define OPENTHREAD_CONFIG_LOG_PREPEND_LEVEL 1 /** * @def OPENTHREAD_CONFIG_LOG_PREPEND_REGION @@ -302,7 +310,7 @@ * Define to prepend the log region to all log messages * */ -#define OPENTHREAD_CONFIG_LOG_PREPEND_REGION 0 +#define OPENTHREAD_CONFIG_LOG_PREPEND_REGION 1 /** * @def OPENTHREAD_CONFIG_LOG_SUFFIX