AsyncTelegram2

Loading...
Searching...
No Matches
serial_log.h
Go to the documentation of this file.
1#ifndef __LOG_H__
2#define __LOG_H__
3
4#include <stdio.h>
5#include <string.h>
6
7#ifdef __cplusplus
8 extern "C"
9{
10#endif
11
12// #define _LOG_FORMAT(letter, format) "\n[" #letter "][%s:%u] %s():\t" format, __FILE_NAME__, __LINE__, __FUNCTION__
13
14#if DEBUG_ENABLE
15// Windows
16#define __FILE_NAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
17// Linux, Mac
18// #define __FILE_NAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
19#if defined(ESP32) || defined(ESP8266)
20#define PRINT_TIME() {struct tm t; time_t now = time(nullptr); t = *localtime(&now); \
21 char str[30]; strftime(str, sizeof(str), "%c", &t); Serial.print(str);}
22#else
23#define PRINT_TIME() Serial.print(millis());
24#endif
25
26
27#define PRINT_FILE_LINE() {PRINT_TIME() Serial.print(" [");Serial.print(__FILE_NAME__); \
28Serial.print(":");Serial.print(__LINE__);Serial.print(" "); Serial.print(__FUNCTION__); Serial.print("()]\t");}
29#if defined(ESP32) || defined(ESP8266)
30#define log_debug(format, args...) { PRINT_FILE_LINE() Serial.printf(format, args); Serial.println();}
31#else
32#define log_debug(format, args...) { PRINT_FILE_LINE() char buf[250]; sprintf(buf, format, args); Serial.println(buf);}
33#endif
34
35#define log_error(x) {PRINT_FILE_LINE() Serial.println(x);}
36#define log_info(x) { PRINT_FILE_LINE() Serial.println(x);}
37#define log_warning(args) { PRINT_FILE_LINE() Serial.print("-W-"); Serial.println(args);}
38// #define log_debug(format, ...) Serial.printf(_LOG_FORMAT(D, format), ##__VA_ARGS__)
39// #define log_error(format, ...) { Serial.println(); Serial.printf(_LOG_FORMAT(E, format), ##__VA_ARGS__); }
40#define debugJson(X, Y) { PRINT_FILE_LINE() Serial.println(); serializeJsonPretty(X, Y); Serial.println();}
41#define errorJson(E) { PRINT_FILE_LINE() Serial.println(); Serial.println(E); }
42
43#else
44#define log_debug(format, ...)
45#define log_warning(args)
46#define log_info(x)
47#define log_error(x)
48
49#define debugJson(X, Y)
50#define errorJson(E)
51#endif
52
53
54#ifdef __cplusplus
55}
56#endif
57
58#endif