BresserWeatherSensorLW
Bresser 868 MHz Weather Sensor Radio Receiver based on ESP32/RP2040 and SX1262/SX1276/LR1121 - sends data to a LoRaWAN Network
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1
2// logging.h
3//
4// Replacement for
5// https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-log.h
6// on RP2040
7//
8// - DEBUG_RP2040_PORT is set in Arduino IDE:
9// Tools->Debug port: "<Disabled>|<Serial>|<Serial1>|<Serial2>"
10// - CORE_DEBUG_LEVEL has to be set manually below
11//
12// created: 09/2023
13//
14//
15// MIT License
16//
17// Copyright (c) 2023 Matthias Prinke
18//
19// Permission is hereby granted, free of charge, to any person obtaining a copy
20// of this software and associated documentation files (the "Software"), to deal
21// in the Software without restriction, including without limitation the rights
22// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
23// copies of the Software, and to permit persons to whom the Software is
24// furnished to do so, subject to the following conditions:
25//
26// The above copyright notice and this permission notice shall be included in all
27// copies or substantial portions of the Software.
28//
29// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
34// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35// SOFTWARE.
36//
37//
38// History:
39//
40// 20230927 Created from BresserWeatherSensorReceiver
41// 20231004 Added function names and line numbers to ESP8266/RP2040 debug logging
42// 20231005 Allowed re-definition of CORE_DEBUG_LEVEL and log_* macros
43// 20251031 Added option for logging via Serial2
44//
45// ToDo:
46// -
47//
49
61
62#ifndef LOGGING_H
63#define LOGGING_H
64
65// Enable to use Serial2 as debug output port
66// This is useful to debug power management without powering/charging via USB
67// #define SERIAL2_LOG_ENABLE
68
69#if CORE_DEBUG_LEVEL > ARDUHAL_LOG_LEVEL_NONE
70const char *pathToFileName(const char *path);
71
72#if defined(SERIAL2_LOG_ENABLE)
73
74// Use Serial2 as debug port
75#define DEBUG_PORT Serial2
76#undef log_i
77#define log_i(...) { DEBUG_PORT.printf("[I][%s:%d] %s(): ", pathToFileName(__FILE__), __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
78#undef log_d
79#define log_d(...) { DEBUG_PORT.printf("[D][%s:%d] %s(): ", pathToFileName(__FILE__), __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
80#undef log_w
81#define log_w(...) { DEBUG_PORT.printf("[W][%s:%d] %s(): ", pathToFileName(__FILE__), __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
82#undef log_e
83#define log_e(...) { DEBUG_PORT.printf("[E][%s:%d] %s(): ", pathToFileName(__FILE__), __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
84#undef log_v
85#define log_v(...) { DEBUG_PORT.printf("[V][%s:%d] %s(): ", pathToFileName(__FILE__), __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
86#endif // SERIAL2_LOG_ENABLE
87#endif // CORE_DEBUG_LEVEL > ARDUHAL_LOG_LEVEL_NONE
88
89#if defined(ARDUINO_ARCH_RP2040)
90 const char *pathToFileName(const char *path);
91
92 #if defined(DEBUG_RP2040_PORT)
93 #define DEBUG_PORT DEBUG_RP2040_PORT
94 #endif
95
96 #define ARDUHAL_LOG_LEVEL_NONE 0
97 #define ARDUHAL_LOG_LEVEL_ERROR 1
98 #define ARDUHAL_LOG_LEVEL_WARN 2
99 #define ARDUHAL_LOG_LEVEL_INFO 3
100 #define ARDUHAL_LOG_LEVEL_DEBUG 4
101 #define ARDUHAL_LOG_LEVEL_VERBOSE 5
102
103 // '#undef' allows to change a previous definition from WeatherSensor.h
104 #undef log_e
105 #undef log_w
106 #undef log_i
107 #undef log_d
108 #undef log_v
109
110 // Set desired level here!
111 #undef CORE_DEBUG_LEVEL
112 #define CORE_DEBUG_LEVEL ARDUHAL_LOG_LEVEL_DEBUG
113
114 #if defined(DEBUG_PORT) && CORE_DEBUG_LEVEL > ARDUHAL_LOG_LEVEL_NONE
115 #define log_e(...) { DEBUG_PORT.printf("[E][%d] %s(): ", __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
116 #else
117 #define log_e(...) {}
118 #endif
119 #if defined(DEBUG_PORT) && CORE_DEBUG_LEVEL > ARDUHAL_LOG_LEVEL_ERROR
120 #define log_w(...) { DEBUG_PORT.printf("[W][%d] %s(): ", __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
121 #else
122 #define log_w(...) {}
123 #endif
124 #if defined(DEBUG_PORT) && CORE_DEBUG_LEVEL > ARDUHAL_LOG_LEVEL_WARN
125 #define log_i(...) { DEBUG_PORT.printf("[I][%d] %s(): ", __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
126 #else
127 #define log_i(...) {}
128 #endif
129 #if defined(DEBUG_PORT) && CORE_DEBUG_LEVEL > ARDUHAL_LOG_LEVEL_INFO
130 #define log_d(...) { DEBUG_PORT.printf("[D][%d] %s(): ", __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
131 #else
132 #define log_d(...) {}
133 #endif
134 #if defined(DEBUG_PORT) && CORE_DEBUG_LEVEL > ARDUHAL_LOG_LEVEL_DEBUG
135 #define log_v(...) { DEBUG_PORT.printf("[V][%d] %s(): ", __LINE__, __func__); DEBUG_PORT.printf(__VA_ARGS__); DEBUG_PORT.println(); }
136 #else
137 #define log_v(...) {}
138 #endif
139
140#endif // defined(ARDUINO_ARCH_RP2040)
141#endif // LOGGING_H