feat(log): Refactoring buffer log APIs

This commit is contained in:
Konstantin Kondrashov
2024-03-11 15:44:33 +02:00
parent 7a70647a01
commit 0f4fc2bf55
11 changed files with 396 additions and 146 deletions
+3 -121
View File
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@@ -12,24 +12,13 @@
#include <inttypes.h>
#include "sdkconfig.h"
#include "esp_rom_sys.h"
#include "esp_log_level.h"
#include "esp_log_buffer.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Log level
*
*/
typedef enum {
ESP_LOG_NONE, /*!< No log output */
ESP_LOG_ERROR, /*!< Critical errors, software module can not recover on its own */
ESP_LOG_WARN, /*!< Error conditions from which recovery measures have been taken */
ESP_LOG_INFO, /*!< Information messages which describe normal flow of events */
ESP_LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
ESP_LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
} esp_log_level_t;
typedef int (*vprintf_like_t)(const char *, va_list);
/**
@@ -172,113 +161,6 @@ void esp_log_writev(esp_log_level_t level, const char* tag, const char* format,
/** @cond */
#include "esp_log_internal.h"
#ifndef LOG_LOCAL_LEVEL
#ifndef BOOTLOADER_BUILD
#define LOG_LOCAL_LEVEL CONFIG_LOG_MAXIMUM_LEVEL
#else
#define LOG_LOCAL_LEVEL CONFIG_BOOTLOADER_LOG_LEVEL
#endif
#endif
/** @endcond */
/**
* @brief Log a buffer of hex bytes at specified level, separated into 16 bytes each line.
*
* @param tag description tag
* @param buffer Pointer to the buffer array
* @param buff_len length of buffer in bytes
* @param level level of the log
*
*/
#define ESP_LOG_BUFFER_HEX_LEVEL( tag, buffer, buff_len, level ) \
do {\
if ( LOG_LOCAL_LEVEL >= (level) ) { \
esp_log_buffer_hex_internal( tag, buffer, buff_len, level ); \
} \
} while(0)
/**
* @brief Log a buffer of characters at specified level, separated into 16 bytes each line. Buffer should contain only printable characters.
*
* @param tag description tag
* @param buffer Pointer to the buffer array
* @param buff_len length of buffer in bytes
* @param level level of the log
*
*/
#define ESP_LOG_BUFFER_CHAR_LEVEL( tag, buffer, buff_len, level ) \
do {\
if ( LOG_LOCAL_LEVEL >= (level) ) { \
esp_log_buffer_char_internal( tag, buffer, buff_len, level ); \
} \
} while(0)
/**
* @brief Dump a buffer to the log at specified level.
*
* The dump log shows just like the one below:
*
* W (195) log_example: 0x3ffb4280 45 53 50 33 32 20 69 73 20 67 72 65 61 74 2c 20 |ESP32 is great, |
* W (195) log_example: 0x3ffb4290 77 6f 72 6b 69 6e 67 20 61 6c 6f 6e 67 20 77 69 |working along wi|
* W (205) log_example: 0x3ffb42a0 74 68 20 74 68 65 20 49 44 46 2e 00 |th the IDF..|
*
* It is highly recommended to use terminals with over 102 text width.
*
* @param tag description tag
* @param buffer Pointer to the buffer array
* @param buff_len length of buffer in bytes
* @param level level of the log
*/
#define ESP_LOG_BUFFER_HEXDUMP( tag, buffer, buff_len, level ) \
do { \
if ( LOG_LOCAL_LEVEL >= (level) ) { \
esp_log_buffer_hexdump_internal( tag, buffer, buff_len, level); \
} \
} while(0)
/**
* @brief Log a buffer of hex bytes at Info level
*
* @param tag description tag
* @param buffer Pointer to the buffer array
* @param buff_len length of buffer in bytes
*
* @see ``esp_log_buffer_hex_level``
*
*/
#define ESP_LOG_BUFFER_HEX(tag, buffer, buff_len) \
do { \
if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { \
ESP_LOG_BUFFER_HEX_LEVEL( tag, buffer, buff_len, ESP_LOG_INFO ); \
}\
} while(0)
/**
* @brief Log a buffer of characters at Info level. Buffer should contain only printable characters.
*
* @param tag description tag
* @param buffer Pointer to the buffer array
* @param buff_len length of buffer in bytes
*
* @see ``esp_log_buffer_char_level``
*
*/
#define ESP_LOG_BUFFER_CHAR(tag, buffer, buff_len) \
do { \
if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { \
ESP_LOG_BUFFER_CHAR_LEVEL( tag, buffer, buff_len, ESP_LOG_INFO ); \
}\
} while(0)
/** @cond */
//to be back compatible
#define esp_log_buffer_hex ESP_LOG_BUFFER_HEX
#define esp_log_buffer_char ESP_LOG_BUFFER_CHAR
#if CONFIG_LOG_COLORS
#define LOG_COLOR_BLACK "30"
#define LOG_COLOR_RED "31"
+180
View File
@@ -0,0 +1,180 @@
/*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include <stdint.h>
#include "esp_log_level.h"
#ifdef __cplusplus
extern "C" {
#endif
#if !BOOTLOADER_BUILD || __DOXYGEN__
/**
* @brief Logs a buffer of hexadecimal bytes at the specified log level.
*
* This function logs a buffer of hexadecimal bytes with 16 bytes per line. The
* log level determines the severity of the log message.
*
* @note This function does not check the log level against the ESP_LOCAL_LEVEL.
* The log level comparison should be done in esp_log.h.
*
* @param tag Description tag to identify the log.
* @param buffer Pointer to the buffer array containing the data to be logged.
* @param buff_len Length of the buffer in bytes.
* @param level Log level indicating the severity of the log message.
*/
void esp_log_buffer_hex_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t level);
/**
* @brief This function logs a buffer of characters with 16 characters per line.
* The buffer should contain only printable characters. The log level determines
* the severity of the log message.
*
* @note This function does not check the log level against the ESP_LOCAL_LEVEL.
* The log level comparison should be done in esp_log.h.
*
* @param tag Description tag to identify the log.
* @param buffer Pointer to the buffer array containing the data to be logged.
* @param buff_len Length of the buffer in bytes.
* @param level Log level indicating the severity of the log message.
*/
void esp_log_buffer_char_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t level);
/**
* @brief This function dumps a buffer to the log in a formatted hex dump style,
* displaying both the memory address and the corresponding hex and ASCII values
* of the bytes. The log level determines the severity of the log message.
*
* @note This function does not check the log level against the ESP_LOCAL_LEVEL.
* The log level comparison should be done in esp_log.h.
* @note It is recommended to use terminals with a width of at least 102
* characters to display the log dump properly.
*
* @param tag Description tag to identify the log.
* @param buffer Pointer to the buffer array containing the data to be logged.
* @param buff_len Length of the buffer in bytes.
* @param log_level Log level indicating the severity of the log message.
*/
void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level);
/**
* @brief Log a buffer of hex bytes at specified level, separated into 16 bytes each line.
*
* The hex log shows just like the one below:
*
* I (954) log_example: 54 68 65 20 77 61 79 20 74 6f 20 67 65 74 20 73
* I (962) log_example: 74 61 72 74 65 64 20 69 73 20 74 6f 20 71 75 69
* I (969) log_example: 74 20 74 61 6c 6b 69 6e 67 20 61 6e 64 20 62 65
* I (977) log_example: 67 69 6e 20 64 6f 69 6e 67 2e 20 2d 20 57 61 6c
* I (984) log_example: 74 20 44 69 73 6e 65 79 00
*
* @param tag Description tag to identify the log.
* @param buffer Pointer to the buffer array containing the data to be logged.
* @param buff_len Length of the buffer in bytes.
* @param level Log level
*/
#define ESP_LOG_BUFFER_HEX_LEVEL(tag, buffer, buff_len, level) \
do { if (LOG_LOCAL_LEVEL >= (level)) {esp_log_buffer_hex_internal(tag, buffer, buff_len, level);} } while(0)
/**
* @brief Log a buffer of characters at specified level, separated into 16 bytes each line. Buffer should contain only printable characters.
*
* The char log shows just like the one below:
*
* I (980) log_example: The way to get s
* I (985) log_example: tarted is to qui
* I (989) log_example: t talking and be
* I (994) log_example: gin doing. - Wal
* I (999) log_example: t Disney
*
* @param tag Description tag to identify the log.
* @param buffer Pointer to the buffer array containing the data to be logged.
* @param buff_len Length of the buffer in bytes.
* @param level Log level.
*
*/
#define ESP_LOG_BUFFER_CHAR_LEVEL(tag, buffer, buff_len, level) \
do { if (LOG_LOCAL_LEVEL >= (level)) {esp_log_buffer_char_internal(tag, buffer, buff_len, level);} } while(0)
/**
* @brief Dump a buffer to the log at specified level.
*
* The dump log shows just like the one below:
*
* I (1013) log_example: 0x3ffb5bc0 54 68 65 20 77 61 79 20 74 6f 20 67 65 74 20 73 |The way to get s|
* I (1024) log_example: 0x3ffb5bd0 74 61 72 74 65 64 20 69 73 20 74 6f 20 71 75 69 |tarted is to qui|
* I (1034) log_example: 0x3ffb5be0 74 20 74 61 6c 6b 69 6e 67 20 61 6e 64 20 62 65 |t talking and be|
* I (1044) log_example: 0x3ffb5bf0 67 69 6e 20 64 6f 69 6e 67 2e 20 2d 20 57 61 6c |gin doing. - Wal|
* I (1054) log_example: 0x3ffb5c00 74 20 44 69 73 6e 65 79 00 |t Disney.|
*
* @note It is highly recommended to use terminals with over 102 text width.
*
* @param tag Description tag to identify the log.
* @param buffer Pointer to the buffer array containing the data to be logged.
* @param buff_len Length of the buffer in bytes.
* @param level Log level.
*/
#define ESP_LOG_BUFFER_HEXDUMP(tag, buffer, buff_len, level) \
do { if (LOG_LOCAL_LEVEL >= (level)) {esp_log_buffer_hexdump_internal(tag, buffer, buff_len, level);} } while(0)
/**
* @brief Log a buffer of hex bytes at Info level
*
* @param tag Description tag to identify the log.
* @param buffer Pointer to the buffer array containing the data to be logged.
* @param buff_len Length of the buffer in bytes.
*
* @see ``ESP_LOG_BUFFER_HEX_LEVEL``
*
*/
#define ESP_LOG_BUFFER_HEX(tag, buffer, buff_len) \
do { if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) {ESP_LOG_BUFFER_HEX_LEVEL(tag, buffer, buff_len, ESP_LOG_INFO);} } while(0)
/**
* @brief Log a buffer of characters at Info level. Buffer should contain only printable characters.
*
* @param tag Description tag to identify the log.
* @param buffer Pointer to the buffer array containing the data to be logged.
* @param buff_len Length of the buffer in bytes.
*
* @see ``ESP_LOG_BUFFER_CHAR_LEVEL``
*
*/
#define ESP_LOG_BUFFER_CHAR(tag, buffer, buff_len) \
do { if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) {ESP_LOG_BUFFER_CHAR_LEVEL(tag, buffer, buff_len, ESP_LOG_INFO);} } while(0)
/** @cond */
/**
* @note For back compatible
* @deprecated This function is deprecated and will be removed in the future.
* Please use ESP_LOG_BUFFER_HEX
*/
// __attribute__((deprecated("Use 'ESP_LOG_BUFFER_HEX' instead")))
static inline void esp_log_buffer_hex(const char *tag, const void *buffer, uint16_t buff_len)
{
ESP_LOG_BUFFER_HEX(tag, buffer, buff_len);
}
/**
* @note For back compatible
* @deprecated This function is deprecated and will be removed in the future.
* Please use ESP_LOG_BUFFER_CHAR
*/
// __attribute__((deprecated("Use 'ESP_LOG_BUFFER_CHAR' instead")))
static inline void esp_log_buffer_char(const char *tag, const void *buffer, uint16_t buff_len)
{
ESP_LOG_BUFFER_CHAR(tag, buffer, buff_len);
}
/** @endcond */
#endif // !BOOTLOADER_BUILD || __DOXYGEN__
#ifdef __cplusplus
}
#endif
+4 -10
View File
@@ -1,15 +1,9 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef __ESP_LOG_INTERNAL_H__
#define __ESP_LOG_INTERNAL_H__
//these functions do not check level versus ESP_LOCAL_LEVEL, this should be done in esp_log.h
void esp_log_buffer_hex_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t level);
void esp_log_buffer_char_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t level);
void esp_log_buffer_hexdump_internal(const char *tag, const void *buffer, uint16_t buff_len, esp_log_level_t log_level);
#endif
#pragma once
#warning "esp_log_internal.h is deprecated, please migrate to esp_log_buffer.h"
#include "esp_log_buffer.h"
+42
View File
@@ -0,0 +1,42 @@
/*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "sdkconfig.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Log level
*/
typedef enum {
ESP_LOG_NONE = 0, /*!< No log output */
ESP_LOG_ERROR = 1, /*!< Critical errors, software module can not recover on its own */
ESP_LOG_WARN = 2, /*!< Error conditions from which recovery measures have been taken */
ESP_LOG_INFO = 3, /*!< Information messages which describe normal flow of events */
ESP_LOG_DEBUG = 4, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
ESP_LOG_VERBOSE = 5, /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
ESP_LOG_MAX = 6, /*!< Number of levels supported */
} esp_log_level_t;
/** @cond */
#ifndef LOG_LOCAL_LEVEL
#ifndef BOOTLOADER_BUILD
#define LOG_LOCAL_LEVEL CONFIG_LOG_MAXIMUM_LEVEL
#else
#define LOG_LOCAL_LEVEL CONFIG_BOOTLOADER_LOG_LEVEL
#endif
#endif
/** @endcond */
#ifdef __cplusplus
}
#endif
@@ -0,0 +1,67 @@
/*
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Convert an unsigned integer value to a string representation in the specified radix.
*
* This function converts the given unsigned integer value to a string representation in the specified radix.
* The resulting string is stored in the provided character buffer `buf`.
*
* @param val The unsigned integer value to be converted.
* @param buf Pointer to the character buffer where the resulting string will be stored.
* The buffer must have enough space to accommodate the entire converted string,
* including the null-terminator.
* @param radix The base of the numeral system to be used for the conversion.
* It determines the number of unique digits in the numeral system
* (e.g., 2 for binary, 10 for decimal, 16 for hexadecimal).
* @param digits Pointer to a character array representing the digits of the numeral system.
* The array must contain characters in the order of increasing values,
* corresponding to the digits of the radix. For example, "0123456789ABCDEF" for hexadecimal.
*
* @return The length of the resulting string (excluding the null-terminator).
*
* @note The buffer `buf` must have sufficient space to hold the entire converted string, including the null-terminator.
* The caller is responsible for ensuring the buffer's size is large enough to prevent buffer overflow.
* @note The provided `digits` array must have enough elements to cover the entire radix used for conversion. Otherwise, undefined behavior may occur.
*/
int esp_log_util_cvt(unsigned long long val, char *buf, long radix, const char *digits);
/**
* @brief Convert an unsigned integer to a hexadecimal string with optional padding.
*
* This function converts an unsigned integer value to a hexadecimal string representation.
* This function calls esp_log_util_cvt(val, buf, 16, pad, "0123456789abcdef") inside.
*
* @param val The unsigned integer value to be converted.
* @param pad The optional padding width for the resulting string.
* @param buf The buffer to store the hexadecimal string.
* @return The length of the converted string.
*/
int esp_log_util_cvt_hex(unsigned long long val, int pad, char *buf);
/**
* @brief Convert an unsigned integer to a decimal string with optional padding.
*
* This function converts an unsigned integer value to a decimal string representation.
* This function calls esp_log_util_cvt(val, buf, 10, pad, "0123456789") inside.
*
* @param val The unsigned integer value to be converted.
* @param pad The optional padding width for the resulting string.
* @param buf The buffer to store the decimal string.
* @return The length of the converted string.
*/
int esp_log_util_cvt_dec(unsigned long long val, int pad, char *buf);
#ifdef __cplusplus
}
#endif