Skip to content
This repository was archived by the owner on Oct 28, 2023. It is now read-only.

Commit fc7aba8

Browse files
committed
Simple esp_log replacement to support the ESP_LOGx() log-messages.
1 parent 09be7ff commit fc7aba8

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

unix/esp_log.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
#include <stdarg.h>
4+
#include <time.h>
5+
#include "esp_log.h"
6+
7+
uint32_t
8+
esp_log_timestamp(void)
9+
{
10+
static time_t t_first = 0;
11+
if (t_first) {
12+
t_first = time(NULL);
13+
return 0;
14+
}
15+
return time(NULL) - t_first;
16+
}
17+
18+
void
19+
esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...)
20+
{
21+
va_list ap;
22+
va_start(ap, format);
23+
vprintf(format, ap);
24+
va_end(ap);
25+
}

unix/esp_log.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy