From 4a6477aaa87ed35388d27ffbedb8f3d19a032461 Mon Sep 17 00:00:00 2001 From: SG Date: Wed, 3 Aug 2022 22:01:38 +1000 Subject: [PATCH] Core, logs: removed tag concatenation (#1524) * Core, logs: removed tag concatenation * Logs: remove unused fn * Logs: remove allocation --- furi/core/log.c | 44 ++++++++++++++++++++++++++++++++++++++++---- furi/core/log.h | 30 ++++++++++++++---------------- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/furi/core/log.c b/furi/core/log.c index 8cf90485..8a36a930 100644 --- a/furi/core/log.c +++ b/furi/core/log.c @@ -22,24 +22,60 @@ void furi_log_init() { furi_log.mutex = furi_mutex_alloc(FuriMutexTypeNormal); } -void furi_log_print(FuriLogLevel level, const char* format, ...) { +void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...) { if(level <= furi_log.log_level && furi_mutex_acquire(furi_log.mutex, FuriWaitForever) == FuriStatusOk) { string_t string; + string_init(string); + + const char* color = FURI_LOG_CLR_RESET; + const char* log_letter = " "; + switch(level) { + case FuriLogLevelError: + color = FURI_LOG_CLR_E; + log_letter = "E"; + break; + case FuriLogLevelWarn: + color = FURI_LOG_CLR_W; + log_letter = "W"; + break; + case FuriLogLevelInfo: + color = FURI_LOG_CLR_I; + log_letter = "I"; + break; + case FuriLogLevelDebug: + color = FURI_LOG_CLR_D; + log_letter = "D"; + break; + case FuriLogLevelTrace: + color = FURI_LOG_CLR_T; + log_letter = "T"; + break; + default: + break; + } // Timestamp - string_init_printf(string, "%lu ", furi_log.timetamp()); + string_printf( + string, + "%lu %s[%s][%s] " FURI_LOG_CLR_RESET, + furi_log.timetamp(), + color, + log_letter, + tag); furi_log.puts(string_get_cstr(string)); - string_clear(string); + string_reset(string); va_list args; va_start(args, format); - string_init_vprintf(string, format, args); + string_vprintf(string, format, args); va_end(args); furi_log.puts(string_get_cstr(string)); string_clear(string); + furi_log.puts("\r\n"); + furi_mutex_release(furi_log.mutex); } } diff --git a/furi/core/log.h b/furi/core/log.h index 6d23a9c2..30026fc4 100644 --- a/furi/core/log.h +++ b/furi/core/log.h @@ -44,13 +44,14 @@ typedef uint32_t (*FuriLogTimestamp)(void); /** Initialize logging */ void furi_log_init(); -/** Log record - * - * @param[in] level The level - * @param[in] format The format - * @param[in] VA args +/** Print log record + * + * @param level + * @param tag + * @param format + * @param ... */ -void furi_log_print(FuriLogLevel level, const char* format, ...); +void furi_log_print_format(FuriLogLevel level, const char* tag, const char* format, ...); /** Set log level * @@ -76,11 +77,6 @@ void furi_log_set_puts(FuriLogPuts puts); */ void furi_log_set_timestamp(FuriLogTimestamp timestamp); -#define FURI_LOG_FORMAT(log_letter, tag, format) \ - FURI_LOG_CLR_##log_letter "[" #log_letter "][" tag "]: " FURI_LOG_CLR_RESET format "\r\n" -#define FURI_LOG_SHOW(tag, format, log_level, log_letter, ...) \ - furi_log_print(log_level, FURI_LOG_FORMAT(log_letter, tag, format), ##__VA_ARGS__) - /** Log methods * * @param tag The application tag @@ -88,13 +84,15 @@ void furi_log_set_timestamp(FuriLogTimestamp timestamp); * @param ... VA Args */ #define FURI_LOG_E(tag, format, ...) \ - FURI_LOG_SHOW(tag, format, FuriLogLevelError, E, ##__VA_ARGS__) -#define FURI_LOG_W(tag, format, ...) FURI_LOG_SHOW(tag, format, FuriLogLevelWarn, W, ##__VA_ARGS__) -#define FURI_LOG_I(tag, format, ...) FURI_LOG_SHOW(tag, format, FuriLogLevelInfo, I, ##__VA_ARGS__) + furi_log_print_format(FuriLogLevelError, tag, format, ##__VA_ARGS__) +#define FURI_LOG_W(tag, format, ...) \ + furi_log_print_format(FuriLogLevelWarn, tag, format, ##__VA_ARGS__) +#define FURI_LOG_I(tag, format, ...) \ + furi_log_print_format(FuriLogLevelInfo, tag, format, ##__VA_ARGS__) #define FURI_LOG_D(tag, format, ...) \ - FURI_LOG_SHOW(tag, format, FuriLogLevelDebug, D, ##__VA_ARGS__) + furi_log_print_format(FuriLogLevelDebug, tag, format, ##__VA_ARGS__) #define FURI_LOG_T(tag, format, ...) \ - FURI_LOG_SHOW(tag, format, FuriLogLevelTrace, T, ##__VA_ARGS__) + furi_log_print_format(FuriLogLevelTrace, tag, format, ##__VA_ARGS__) #ifdef __cplusplus }