Control Surface main
MIDI Control Surface library for Arduino
Debug.hpp
Go to the documentation of this file.
1#pragma once
2
4
6AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
7
8#include <AH/PrintStream/PrintStream.hpp>
10
11#ifndef FLUSH_ON_EVERY_DEBUG_STATEMENT
12#if !(defined(ESP32) || defined(ESP8266))
13
20#define FLUSH_ON_EVERY_DEBUG_STATEMENT 0
21
22#else
23
24#define FLUSH_ON_EVERY_DEBUG_STATEMENT 0
25
26#endif
27#endif
28
29#if defined(ESP32)
30#include <mutex>
31#elif defined(ARDUINO_ARCH_MBED)
32#include <mutex>
33#include <rtos/Mutex.h>
34#endif
35
36// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
37
38#ifdef ARDUINO
39
40// Uncomment this line to override Arduino debug output
41// #define DEBUG_OUT Serial
42
43#else
44
45// Uncomment this line to override PC tests debug output
46// #define DEBUG_OUT std::cout
47
48#endif
49
50// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
51
52#if FLUSH_ON_EVERY_DEBUG_STATEMENT
53#define DEBUG_ENDL endl
54#else
55#define DEBUG_ENDL "\r\n"
56#endif
57
58#if (defined(ESP32) || defined(ESP8266)) && FLUSH_ON_EVERY_DEBUG_STATEMENT
59#error "ESP32 and ESP8266 don't support flushing `Print` objects"
60#endif
61
62// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
63
64#define DEBUG_STR_HELPER(x) #x
65#define DEBUG_STR(x) DEBUG_STR_HELPER(x)
66
67#define DEBUG_FUNC_LOCATION \
68 '[' << __PRETTY_FUNCTION__ << F(" @ line " DEBUG_STR(__LINE__) "]:\t")
69#define DEBUG_LOCATION "[" __FILE__ ":" DEBUG_STR(__LINE__) "]:\t"
70
71// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
72
73#if defined(ESP32)
74#define DEBUG_LOCK_MUTEX std::lock_guard<std::mutex> lock(AH::debugmutex);
76extern std::mutex debugmutex;
78#elif defined(ARDUINO_ARCH_MBED)
79#define DEBUG_LOCK_MUTEX std::lock_guard<rtos::Mutex> lock(AH::debugmutex);
81extern rtos::Mutex debugmutex;
83#else
84#define DEBUG_LOCK_MUTEX
85#endif
86
87// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
88
92#define NAMEDVALUE(x) F(DEBUG_STR(x) " = ") << x
93
94#ifdef DEBUG_OUT // Debugging enabled ==========================================
95
98#define DEBUG(x) \
99 do { \
100 DEBUG_LOCK_MUTEX \
101 DEBUG_OUT << x << DEBUG_ENDL; \
102 } while (0)
103
108#define DEBUGREF(x) \
109 do { \
110 DEBUG_LOCK_MUTEX \
111 DEBUG_OUT << F(DEBUG_LOCATION) << x << DEBUG_ENDL; \
112 } while (0)
113
118#define DEBUGFN(x) \
119 do { \
120 DEBUG_LOCK_MUTEX \
121 DEBUG_OUT << DEBUG_FUNC_LOCATION << x << DEBUG_ENDL; \
122 } while (0)
123
124#ifdef ARDUINO
125
130#define DEBUGTIME(x) \
131 do { \
132 DEBUG_LOCK_MUTEX \
133 unsigned long t = millis(); \
134 unsigned long h = t / (60UL * 60 * 1000); \
135 unsigned long m = (t / (60UL * 1000)) % 60; \
136 unsigned long s = (t / (1000UL)) % 60; \
137 unsigned long ms = t % 1000; \
138 const char *ms_zeros = ms > 99 ? "" : (ms > 9 ? "0" : "00"); \
139 DEBUG_OUT << '[' << h << ':' << m << ':' << s << '.' << ms_zeros << ms \
140 << "]:\t" << x << DEBUG_ENDL; \
141 } while (0)
142
143#else // !ARDUINO
144
145#include <chrono>
146
148extern const decltype(std::chrono::high_resolution_clock::now()) start_time;
150
151#define DEBUGTIME(x) \
152 do { \
153 USING_AH_NAMESPACE; \
154 using namespace std::chrono; \
155 auto now = high_resolution_clock::now(); \
156 unsigned long t = \
157 duration_cast<milliseconds>(now - start_time).count(); \
158 unsigned long h = t / (60UL * 60 * 1000); \
159 unsigned long m = (t / (60UL * 1000)) % 60; \
160 unsigned long s = (t / (1000UL)) % 60; \
161 unsigned long ms = t % 1000; \
162 const char *ms_zeros = ms > 99 ? "" : (ms > 9 ? "0" : "00"); \
163 DEBUG_OUT << '[' << h << ':' << m << ':' << s << '.' << ms_zeros << ms \
164 << "]:\t" << x << DEBUG_ENDL; \
165 } while (0)
166
167#endif // ARDUINO
168
169#include "DebugVal.hpp"
170
178#define DEBUGVAL(...) DEBUGVALN(COUNT(__VA_ARGS__))(__VA_ARGS__)
179
180#else // Debugging disabled ====================================================
181
182#define DEBUG(x) \
183 do { \
184 } while (0)
185#define DEBUGREF(x) \
186 do { \
187 } while (0)
188#define DEBUGFN(x) \
189 do { \
190 } while (0)
191#define DEBUGTIME(x) \
192 do { \
193 } while (0)
194#define DEBUGVAL(...) \
195 do { \
196 } while (0)
197
198#endif
199
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:37
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:36