|
| 1 | +#ifndef ESP_LOG_H |
| 2 | +#define ESP_LOG_H |
| 3 | + |
| 4 | +/* based on Espressif's esp-idf code */ |
| 5 | + |
| 6 | +#include <stdint.h> |
| 7 | +#include <stdarg.h> |
| 8 | +#include "sdkconfig.h" |
| 9 | + |
| 10 | +typedef enum { |
| 11 | + ESP_LOG_NONE, |
| 12 | + ESP_LOG_ERROR, |
| 13 | + ESP_LOG_WARN, |
| 14 | + ESP_LOG_INFO, |
| 15 | + ESP_LOG_DEBUG, |
| 16 | + ESP_LOG_VERBOSE |
| 17 | +} esp_log_level_t; |
| 18 | + |
| 19 | +uint32_t esp_log_timestamp(void); |
| 20 | + |
| 21 | +void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) __attribute__ ((format (printf, 3, 4))); |
| 22 | + |
| 23 | + |
| 24 | +#if CONFIG_LOG_COLORS |
| 25 | +#define LOG_COLOR_BLACK "30" |
| 26 | +#define LOG_COLOR_RED "31" |
| 27 | +#define LOG_COLOR_GREEN "32" |
| 28 | +#define LOG_COLOR_BROWN "33" |
| 29 | +#define LOG_COLOR_BLUE "34" |
| 30 | +#define LOG_COLOR_PURPLE "35" |
| 31 | +#define LOG_COLOR_CYAN "36" |
| 32 | +#define LOG_COLOR(COLOR) "\033[0;" COLOR "m" |
| 33 | +#define LOG_BOLD(COLOR) "\033[1;" COLOR "m" |
| 34 | +#define LOG_RESET_COLOR "\033[0m" |
| 35 | +#define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED) |
| 36 | +#define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN) |
| 37 | +#define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN) |
| 38 | +#define LOG_COLOR_D |
| 39 | +#define LOG_COLOR_V |
| 40 | +#else //CONFIG_LOG_COLORS |
| 41 | +#define LOG_COLOR_E |
| 42 | +#define LOG_COLOR_W |
| 43 | +#define LOG_COLOR_I |
| 44 | +#define LOG_COLOR_D |
| 45 | +#define LOG_COLOR_V |
| 46 | +#define LOG_RESET_COLOR |
| 47 | +#endif //CONFIG_LOG_COLORS |
| 48 | + |
| 49 | +#define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%d) %s: " format LOG_RESET_COLOR "\n" |
| 50 | + |
| 51 | +#ifndef LOG_LOCAL_LEVEL |
| 52 | +#define LOG_LOCAL_LEVEL ((esp_log_level_t) CONFIG_LOG_DEFAULT_LEVEL) |
| 53 | +#endif |
| 54 | + |
| 55 | +#define ESP_EARLY_LOGE( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_ERROR) { ets_printf(LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 56 | +#define ESP_EARLY_LOGW( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN) { ets_printf(LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 57 | +#define ESP_EARLY_LOGI( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { ets_printf(LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 58 | +#define ESP_EARLY_LOGD( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) { ets_printf(LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 59 | +#define ESP_EARLY_LOGV( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE) { ets_printf(LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 60 | + |
| 61 | +#define ESP_LOGE( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_ERROR) { esp_log_write(ESP_LOG_ERROR, tag, LOG_FORMAT(E, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 62 | +#define ESP_LOGW( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_WARN) { esp_log_write(ESP_LOG_WARN, tag, LOG_FORMAT(W, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 63 | +#define ESP_LOGI( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_INFO) { esp_log_write(ESP_LOG_INFO, tag, LOG_FORMAT(I, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 64 | +#define ESP_LOGD( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_DEBUG) { esp_log_write(ESP_LOG_DEBUG, tag, LOG_FORMAT(D, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 65 | +#define ESP_LOGV( tag, format, ... ) if (LOG_LOCAL_LEVEL >= ESP_LOG_VERBOSE) { esp_log_write(ESP_LOG_VERBOSE, tag, LOG_FORMAT(V, format), esp_log_timestamp(), tag, ##__VA_ARGS__); } |
| 66 | + |
| 67 | +#endif // ESP_LOG_H |
0 commit comments