diff --git a/components/bootloader/Kconfig.log.format b/components/bootloader/Kconfig.log.format index 672001886d..985fa5f43a 100644 --- a/components/bootloader/Kconfig.log.format +++ b/components/bootloader/Kconfig.log.format @@ -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 diff --git a/components/log/include/esp_log.h b/components/log/include/esp_log.h index 529d00a176..8d11d9ca94 100644 --- a/components/log/include/esp_log.h +++ b/components/log/include/esp_log.h @@ -9,10 +9,10 @@ #include #include #include -#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" diff --git a/components/log/include/esp_log_color.h b/components/log/include/esp_log_color.h new file mode 100644 index 0000000000..483d634ea6 --- /dev/null +++ b/components/log/include/esp_log_color.h @@ -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 diff --git a/docs/doxygen/Doxyfile b/docs/doxygen/Doxyfile index 9f0c2cd37a..d54d9deb76 100644 --- a/docs/doxygen/Doxyfile +++ b/docs/doxygen/Doxyfile @@ -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 \ diff --git a/docs/en/api-reference/system/log.rst b/docs/en/api-reference/system/log.rst index d472ddfaa0..452161ddb1 100644 --- a/docs/en/api-reference/system/log.rst +++ b/docs/en/api-reference/system/log.rst @@ -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 diff --git a/docs/zh_CN/api-reference/system/log.rst b/docs/zh_CN/api-reference/system/log.rst index a3147b6d1d..ccfa490c39 100644 --- a/docs/zh_CN/api-reference/system/log.rst +++ b/docs/zh_CN/api-reference/system/log.rst @@ -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