Arduino Helpers master
Utility library for Arduino
Debug.hpp
Go to the documentation of this file.
1#pragma once
2
4
7
8#ifndef FLUSH_ON_EVERY_DEBUG_STATEMENT
9#if !(defined(ESP32) || defined(ESP8266))
10
17#define FLUSH_ON_EVERY_DEBUG_STATEMENT 0
18
19#else
20
21#define FLUSH_ON_EVERY_DEBUG_STATEMENT 0
22
23#endif
24#endif
25
26#if defined(ESP32)
27#include <mutex>
28#elif defined(ARDUINO_ARCH_MBED)
29#include <mutex>
30#include <rtos/Mutex.h>
31#endif
32
33// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
34
35#ifdef ARDUINO
36
37// Uncomment this line to override Arduino debug output
38// #define DEBUG_OUT Serial
39
40#else
41
42// Uncomment this line to override PC tests debug output
43// #define DEBUG_OUT std::cout
44
45#endif
46
47// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
48
49#if FLUSH_ON_EVERY_DEBUG_STATEMENT
50#define DEBUG_ENDL endl
51#else
52#define DEBUG_ENDL "\r\n"
53#endif
54
55#if (defined(ESP32) || defined(ESP8266)) && FLUSH_ON_EVERY_DEBUG_STATEMENT
56#error "ESP32 and ESP8266 don't support flushing `Print` objects"
57#endif
58
59// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
60
61#define DEBUG_STR_HELPER(x) #x
62#define DEBUG_STR(x) DEBUG_STR_HELPER(x)
63
64#define DEBUG_FUNC_LOCATION \
65 '[' << __PRETTY_FUNCTION__ << F(" @ line " DEBUG_STR(__LINE__) "]:\t")
66#define DEBUG_LOCATION "[" __FILE__ ":" DEBUG_STR(__LINE__) "]:\t"
67
68// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
69
70#if defined(ESP32)
71#define DEBUG_LOCK_MUTEX std::lock_guard<std::mutex> lock(AH::debugmutex);
73extern std::mutex debugmutex;
75#elif defined(ARDUINO_ARCH_MBED)
76#define DEBUG_LOCK_MUTEX std::lock_guard<rtos::Mutex> lock(AH::debugmutex);
78extern rtos::Mutex debugmutex;
80#else
81#define DEBUG_LOCK_MUTEX
82#endif
83
84// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
85
89#define NAMEDVALUE(x) F(DEBUG_STR(x) " = ") << x
90
91#ifdef DEBUG_OUT // Debugging enabled ==========================================
92
95#define DEBUG(x) \
96 do { \
97 DEBUG_LOCK_MUTEX \
98 DEBUG_OUT << x << DEBUG_ENDL; \
99 } while (0)
100
105#define DEBUGREF(x) \
106 do { \
107 DEBUG_LOCK_MUTEX \
108 DEBUG_OUT << F(DEBUG_LOCATION) << x << DEBUG_ENDL; \
109 } while (0)
110
115#define DEBUGFN(x) \
116 do { \
117 DEBUG_LOCK_MUTEX \
118 DEBUG_OUT << DEBUG_FUNC_LOCATION << x << DEBUG_ENDL; \
119 } while (0)
120
121#ifdef ARDUINO
122
127#define DEBUGTIME(x) \
128 do { \
129 DEBUG_LOCK_MUTEX \
130 unsigned long t = millis(); \
131 unsigned long h = t / (60UL * 60 * 1000); \
132 unsigned long m = (t / (60UL * 1000)) % 60; \
133 unsigned long s = (t / (1000UL)) % 60; \
134 unsigned long ms = t % 1000; \
135 const char *ms_zeros = ms > 99 ? "" : (ms > 9 ? "0" : "00"); \
136 DEBUG_OUT << '[' << h << ':' << m << ':' << s << '.' << ms_zeros << ms \
137 << "]:\t" << x << DEBUG_ENDL; \
138 } while (0)
139
140#else // !ARDUINO
141
142#include <chrono>
143
145extern const decltype(std::chrono::high_resolution_clock::now()) start_time;
147
148#define DEBUGTIME(x) \
149 do { \
150 USING_AH_NAMESPACE; \
151 using namespace std::chrono; \
152 auto now = high_resolution_clock::now(); \
153 unsigned long t = \
154 duration_cast<milliseconds>(now - start_time).count(); \
155 unsigned long h = t / (60UL * 60 * 1000); \
156 unsigned long m = (t / (60UL * 1000)) % 60; \
157 unsigned long s = (t / (1000UL)) % 60; \
158 unsigned long ms = t % 1000; \
159 const char *ms_zeros = ms > 99 ? "" : (ms > 9 ? "0" : "00"); \
160 DEBUG_OUT << '[' << h << ':' << m << ':' << s << '.' << ms_zeros << ms \
161 << "]:\t" << x << DEBUG_ENDL; \
162 } while (0)
163
164#endif // ARDUINO
165
166#include "DebugVal.hpp"
167
175#define DEBUGVAL(...) DEBUGVALN(COUNT(__VA_ARGS__))(__VA_ARGS__)
176
177#else // Debugging disabled ====================================================
178
179#define DEBUG(x) \
180 do { \
181 } while (0)
182#define DEBUGREF(x) \
183 do { \
184 } while (0)
185#define DEBUGFN(x) \
186 do { \
187 } while (0)
188#define DEBUGTIME(x) \
189 do { \
190 } while (0)
191#define DEBUGVAL(...) \
192 do { \
193 } while (0)
194
195#endif
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE