feat(log): Refactoring color macros

This commit is contained in:
Konstantin Kondrashov 2024-05-02 15:09:52 +03:00
parent ec594e2c6f
commit 149a9e0d7f
6 changed files with 120 additions and 26 deletions

View File

@ -1,5 +1,13 @@
menu "Format"
config BOOTLOADER_LOG_COLORS
bool "Color"
default y
help
Use ANSI terminal colors in log output
Enable ANSI terminal color codes.
In order to view these, your terminal program must support ANSI color codes.
choice BOOTLOADER_LOG_TIMESTAMP_SOURCE
prompt "Timestamp"
default BOOTLOADER_LOG_TIMESTAMP_SOURCE_CPU_TICKS

View File

@ -9,10 +9,10 @@
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include "esp_log_level.h"
#include "sdkconfig.h"
#include "esp_rom_sys.h"
#include "esp_log_level.h"
#include "esp_log_color.h"
#include "esp_log_buffer.h"
#include "esp_log_timestamp.h"
@ -59,31 +59,6 @@ void esp_log_writev(esp_log_level_t level, const char* tag, const char* format,
/** @cond */
#if CONFIG_LOG_COLORS
#define LOG_COLOR_BLACK "30"
#define LOG_COLOR_RED "31"
#define LOG_COLOR_GREEN "32"
#define LOG_COLOR_BROWN "33"
#define LOG_COLOR_BLUE "34"
#define LOG_COLOR_PURPLE "35"
#define LOG_COLOR_CYAN "36"
#define LOG_COLOR(COLOR) "\033[0;" COLOR "m"
#define LOG_BOLD(COLOR) "\033[1;" COLOR "m"
#define LOG_RESET_COLOR "\033[0m"
#define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED)
#define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN)
#define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN)
#define LOG_COLOR_D
#define LOG_COLOR_V
#else //CONFIG_LOG_COLORS
#define LOG_COLOR_E
#define LOG_COLOR_W
#define LOG_COLOR_I
#define LOG_COLOR_D
#define LOG_COLOR_V
#define LOG_RESET_COLOR
#endif //CONFIG_LOG_COLORS
#define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%" PRIu32 ") %s: " format LOG_RESET_COLOR "\n"
#define LOG_SYSTEM_TIME_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%s) %s: " format LOG_RESET_COLOR "\n"

View File

@ -0,0 +1,108 @@
/*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C" {
#endif
/** @cond */
// ANSI Color Codes:
// Macros for defining foreground colors (text).
#define LOG_ANSI_COLOR_BLACK "30"
#define LOG_ANSI_COLOR_RED "31"
#define LOG_ANSI_COLOR_GREEN "32"
#define LOG_ANSI_COLOR_YELLOW "33"
#define LOG_ANSI_COLOR_BLUE "34"
#define LOG_ANSI_COLOR_MAGENTA "35"
#define LOG_ANSI_COLOR_CYAN "36"
#define LOG_ANSI_COLOR_WHITE "37"
#define LOG_ANSI_COLOR_DEFAULT "39"
// Macros for defining background colors.
#define LOG_ANSI_COLOR_BG_BLACK "40"
#define LOG_ANSI_COLOR_BG_RED "41"
#define LOG_ANSI_COLOR_BG_GREEN "42"
#define LOG_ANSI_COLOR_BG_YELLOW "43"
#define LOG_ANSI_COLOR_BG_BLUE "44"
#define LOG_ANSI_COLOR_BG_MAGENTA "45"
#define LOG_ANSI_COLOR_BG_CYAN "46"
#define LOG_ANSI_COLOR_BG_WHITE "47"
#define LOG_ANSI_COLOR_BG_DEFAULT "49"
// Macros for defining text styles like bold, italic, and underline.
#define LOG_ANSI_COLOR_STYLE_RESET "0"
#define LOG_ANSI_COLOR_STYLE_BOLD "1"
#define LOG_ANSI_COLOR_STYLE_ITALIC "3"
#define LOG_ANSI_COLOR_STYLE_UNDERLINE "4"
// Macros that form the starting sequence for setting the text color, background color, and reset all.
#define LOG_ANSI_COLOR(TEXT_COLOR) "\033[" TEXT_COLOR "m"
#define LOG_ANSI_COLOR_BG(BG_COLOR) "\033[" BG_COLOR "m"
#define LOG_ANSI_COLOR_RESET "\033[" LOG_ANSI_COLOR_STYLE_RESET "m"
// Macros that form the starting sequence for text color + style + background colors
#define LOG_ANSI_COLOR_REGULAR(COLOR) LOG_ANSI_COLOR(LOG_ANSI_COLOR_STYLE_RESET ";" COLOR)
#define LOG_ANSI_COLOR_BOLD(COLOR) LOG_ANSI_COLOR(LOG_ANSI_COLOR_STYLE_BOLD ";" COLOR)
#define LOG_ANSI_COLOR_ITALIC(COLOR) LOG_ANSI_COLOR(LOG_ANSI_COLOR_STYLE_ITALIC ";" COLOR)
#define LOG_ANSI_COLOR_UNDERLINE(COLOR) LOG_ANSI_COLOR(LOG_ANSI_COLOR_STYLE_UNDERLINE ";" COLOR)
#define LOG_ANSI_COLOR_BACKGROUND(TEXT_COLOR, BG_COLOR) LOG_ANSI_COLOR(TEXT_COLOR ";" BG_COLOR)
#define LOG_ANSI_COLOR_REGULAR_BACKGROUND(TEXT_COLOR, BG_COLOR) LOG_ANSI_COLOR_REGULAR(TEXT_COLOR ";" BG_COLOR)
#define LOG_ANSI_COLOR_BOLD_BACKGROUND(TEXT_COLOR, BG_COLOR) LOG_ANSI_COLOR_BOLD(TEXT_COLOR ";" BG_COLOR)
#define LOG_ANSI_COLOR_ITALIC_BACKGROUND(TEXT_COLOR, BG_COLOR) LOG_ANSI_COLOR_ITALIC(TEXT_COLOR ";" BG_COLOR)
#define LOG_ANSI_COLOR_UNDERLINE_BACKGROUND(TEXT_COLOR, BG_COLOR) LOG_ANSI_COLOR_UNDERLINE(TEXT_COLOR ";" BG_COLOR)
#define LOG_ANSI_COLOR_FORMAT(TEXT_STYLE, TEXT_COLOR, BG_COLOR) LOG_ANSI_COLOR(TEXT_STYLE ";" TEXT_COLOR ";" BG_COLOR)
/**
* Usage example of ANSI color for logs:
*
* The text is yellow and no style.
* printf(LOG_ANSI_COLOR(LOG_ANSI_COLOR_YELLOW) "%s" LOG_ANSI_COLOR_RESET "\n", text_str);
*
* The text is red and no style on a green background.
* printf(LOG_ANSI_COLOR_REGULAR_BACKGROUND(LOG_ANSI_COLOR_RED, LOG_ANSI_COLOR_BG_GREEN) "%s" LOG_ANSI_COLOR_RESET "\n", text_str);
*
* Note that if LOG_ANSI_COLOR_RESET is not sent, the settings are retained until the following setting.
*
* The LOG_ANSI_COLOR_FORMAT macro is more flexible than others. It does not use the reset command, which means that
* this macro can combine the style with the previous settings.
* The text is white and bold on a blue background.
* printf(LOG_ANSI_COLOR_FORMAT(LOG_ANSI_COLOR_STYLE_BOLD, LOG_ANSI_COLOR_WHITE, LOG_ANSI_COLOR_BG_BLUE) "%s" LOG_ANSI_COLOR_RESET "\n", text_str);
*/
#if (!BOOTLOADER_BUILD && CONFIG_LOG_COLORS) || (BOOTLOADER_BUILD && CONFIG_BOOTLOADER_LOG_COLORS)
#define LOG_COLOR_BLACK LOG_ANSI_COLOR_BLACK
#define LOG_COLOR_RED LOG_ANSI_COLOR_RED
#define LOG_COLOR_GREEN LOG_ANSI_COLOR_GREEN
#define LOG_COLOR_BROWN LOG_ANSI_COLOR_YELLOW
#define LOG_COLOR_YELLOW LOG_ANSI_COLOR_YELLOW
#define LOG_COLOR_BLUE LOG_ANSI_COLOR_BLUE
#define LOG_COLOR_PURPLE LOG_ANSI_COLOR_MAGENTA
#define LOG_COLOR_MAGENTA LOG_ANSI_COLOR_MAGENTA
#define LOG_COLOR_CYAN LOG_ANSI_COLOR_CYAN
#define LOG_COLOR_WHITE LOG_ANSI_COLOR_WHITE
#define LOG_COLOR(COLOR) LOG_ANSI_COLOR_REGULAR(COLOR)
#define LOG_BOLD(COLOR) LOG_ANSI_COLOR_BOLD(COLOR)
#define LOG_ITALIC(COLOR) LOG_ANSI_COLOR_ITALIC(COLOR)
#define LOG_UNDERLINE(COLOR) LOG_ANSI_COLOR_UNDERLINE(COLOR)
#define LOG_RESET_COLOR LOG_ANSI_COLOR_RESET
#define LOG_COLOR_E LOG_ANSI_COLOR_REGULAR(LOG_COLOR_RED)
#define LOG_COLOR_W LOG_ANSI_COLOR_REGULAR(LOG_COLOR_YELLOW)
#define LOG_COLOR_I LOG_ANSI_COLOR_REGULAR(LOG_COLOR_GREEN)
#define LOG_COLOR_D ""
#define LOG_COLOR_V ""
#else
#define LOG_RESET_COLOR ""
#define LOG_COLOR_E ""
#define LOG_COLOR_W ""
#define LOG_COLOR_I ""
#define LOG_COLOR_D ""
#define LOG_COLOR_V ""
#endif
/** @endcond */
#ifdef __cplusplus
}
#endif

View File

@ -272,6 +272,7 @@ INPUT = \
$(PROJECT_PATH)/components/log/include/esp_log_level.h \
$(PROJECT_PATH)/components/log/include/esp_log_buffer.h \
$(PROJECT_PATH)/components/log/include/esp_log_timestamp.h \
$(PROJECT_PATH)/components/log/include/esp_log_color.h \
$(PROJECT_PATH)/components/lwip/include/apps/esp_sntp.h \
$(PROJECT_PATH)/components/lwip/include/apps/ping/ping_sock.h \
$(PROJECT_PATH)/components/mbedtls/esp_crt_bundle/include/esp_crt_bundle.h \

View File

@ -154,3 +154,4 @@ API Reference
.. include-build-file:: inc/esp_log_level.inc
.. include-build-file:: inc/esp_log_buffer.inc
.. include-build-file:: inc/esp_log_timestamp.inc
.. include-build-file:: inc/esp_log_color.inc

View File

@ -138,3 +138,4 @@ API 参考
.. include-build-file:: inc/esp_log_level.inc
.. include-build-file:: inc/esp_log_buffer.inc
.. include-build-file:: inc/esp_log_timestamp.inc
.. include-build-file:: inc/esp_log_color.inc