From a7568e4a6a15a47477bf69bf2456f6b993ac9448 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Fri, 14 Dec 2018 10:47:13 +0100 Subject: [PATCH] logging: Handle dropped messages in log_output Extended log_output interface to handle dropped messages. Log_output is printing a message containing number of dropped messages. Signed-off-by: Krzysztof Chruscinski --- include/logging/log_output.h | 9 ++++++ subsys/logging/log_output.c | 54 ++++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/include/logging/log_output.h b/include/logging/log_output.h index 70eebb89444..963e8b51d07 100644 --- a/include/logging/log_output.h +++ b/include/logging/log_output.h @@ -98,6 +98,15 @@ void log_output_msg_process(const struct log_output *log_output, struct log_msg *msg, u32_t flags); +/** @brief Process dropped messages indication. + * + * Function prints error message indicating lost log messages. + * + * @param log_output Pointer to the log output instance. + * @param cnt Number of dropped messages. + */ +void log_output_dropped_process(const struct log_output *log_output, u32_t cnt); + /** @brief Flush output buffer. * * @param log_output Pointer to the log output instance. diff --git a/subsys/logging/log_output.c b/subsys/logging/log_output.c index 96f218f4b8d..929cf3f349f 100644 --- a/subsys/logging/log_output.c +++ b/subsys/logging/log_output.c @@ -10,18 +10,19 @@ #include #include #include +#include + +#define LOG_COLOR_CODE_DEFAULT "\x1B[0m" +#define LOG_COLOR_CODE_RED "\x1B[1;31m" +#define LOG_COLOR_CODE_YELLOW "\x1B[1;33m" #define HEXDUMP_BYTES_IN_LINE 8 -#define LOG_COLOR_CODE_DEFAULT "\x1B[0m" -#define LOG_COLOR_CODE_BLACK "\x1B[1;30m" -#define LOG_COLOR_CODE_RED "\x1B[1;31m" -#define LOG_COLOR_CODE_GREEN "\x1B[1;32m" -#define LOG_COLOR_CODE_YELLOW "\x1B[1;33m" -#define LOG_COLOR_CODE_BLUE "\x1B[1;34m" -#define LOG_COLOR_CODE_MAGENTA "\x1B[1;35m" -#define LOG_COLOR_CODE_CYAN "\x1B[1;36m" -#define LOG_COLOR_CODE_WHITE "\x1B[1;37m" +#define DROPPED_COLOR_PREFIX \ + _LOG_EVAL(CONFIG_LOG_BACKEND_SHOW_COLOR, (LOG_COLOR_CODE_RED), ()) + +#define DROPPED_COLOR_POSTFIX \ + _LOG_EVAL(CONFIG_LOG_BACKEND_SHOW_COLOR, (LOG_COLOR_CODE_DEFAULT), ()) static const char *const severity[] = { NULL, @@ -114,18 +115,23 @@ static int print_formatted(const struct log_output *log_output, return length; } -void log_output_flush(const struct log_output *log_output) +static void buffer_write(log_output_func_t outf, u8_t *buf, size_t len, + void *ctx) { - int offset = 0; - int len = log_output->control_block->offset; int processed; do { - processed = log_output->func(&log_output->buf[offset], len, - log_output->control_block->ctx); + processed = outf(buf, len, ctx); len -= processed; - offset += processed; + buf += processed; } while (len); +} + +void log_output_flush(const struct log_output *log_output) +{ + buffer_write(log_output->func, log_output->buf, + log_output->control_block->offset, + log_output->control_block->ctx); log_output->control_block->offset = 0; } @@ -524,6 +530,24 @@ void log_output_msg_process(const struct log_output *log_output, log_output_flush(log_output); } +void log_output_dropped_process(const struct log_output *log_output, u32_t cnt) +{ + char buf[5]; + int len; + static const char prefix[] = DROPPED_COLOR_PREFIX "--- "; + static const char postfix[] = + " messages dropped ---\r\n" DROPPED_COLOR_POSTFIX; + log_output_func_t outf = log_output->func; + struct device *dev = (struct device *)log_output->control_block->ctx; + + cnt = min(cnt, 9999); + len = snprintf(buf, sizeof(buf), "%d", cnt); + + buffer_write(outf, (u8_t *)prefix, sizeof(prefix) - 1, dev); + buffer_write(outf, buf, len, dev); + buffer_write(outf, (u8_t *)postfix, sizeof(postfix) - 1, dev); +} + void log_output_timestamp_freq_set(u32_t frequency) { timestamp_div = 1U;