Control Surface  1.1.1
MIDI Control Surface library for Arduino
Debug.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
6 AH_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 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
30 
31 #ifdef ARDUINO
32 
33 // Uncomment this line to override Arduino debug output
34 // #define DEBUG_OUT Serial
35 
36 #else
37 
38 // Uncomment this line to override PC tests debug output
39 // #define DEBUG_OUT std::cout
40 
41 #endif
42 
43 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
44 
45 #if FLUSH_ON_EVERY_DEBUG_STATEMENT
46 #define DEBUG_ENDL endl
47 #else
48 #define DEBUG_ENDL "\r\n"
49 #endif
50 
51 #if (defined(ESP32) || defined(ESP8266)) && FLUSH_ON_EVERY_DEBUG_STATEMENT
52 #error "ESP32 and ESP8266 don't support flushing `Print` objects"
53 #endif
54 
55 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
56 
57 #define DEBUG_STR_HELPER(x) #x
58 #define DEBUG_STR(x) DEBUG_STR_HELPER(x)
59 
60 #define DEBUG_FUNC_LOCATION \
61  '[' << __PRETTY_FUNCTION__ << F(" @ line " DEBUG_STR(__LINE__) "]:\t")
62 #define DEBUG_LOCATION "[" __FILE__ ":" DEBUG_STR(__LINE__) "]:\t"
63 
64 // :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
65 
69 #define NAMEDVALUE(x) F(DEBUG_STR(x) " = ") << x
70 
71 #ifdef DEBUG_OUT // Debugging enabled ==========================================
72 
75 #define DEBUG(x) \
76  do { \
77  DEBUG_OUT << x << DEBUG_ENDL; \
78  } while (0)
79 
84 #define DEBUGREF(x) \
85  do { \
86  DEBUG_OUT << F(DEBUG_LOCATION) << x << DEBUG_ENDL; \
87  } while (0)
88 
93 #define DEBUGFN(x) \
94  do { \
95  DEBUG_OUT << DEBUG_FUNC_LOCATION << x << DEBUG_ENDL; \
96  } while (0)
97 
98 #ifdef ARDUINO
99 
104 #define DEBUGTIME(x) \
105  do { \
106  unsigned long t = millis(); \
107  unsigned long h = t / (60UL * 60 * 1000); \
108  unsigned long m = (t / (60UL * 1000)) % 60; \
109  unsigned long s = (t / (1000UL)) % 60; \
110  unsigned long ms = t % 1000; \
111  const char *ms_zeros = ms > 99 ? "" : (ms > 9 ? "0" : "00"); \
112  DEBUG_OUT << '[' << h << ':' << m << ':' << s << '.' << ms_zeros << ms \
113  << "]:\t" << x << DEBUG_ENDL; \
114  } while (0)
115 
116 #else // !ARDUINO
117 
118 #include <chrono>
119 
121 extern const decltype(std::chrono::high_resolution_clock::now()) start_time;
123 
124 #define DEBUGTIME(x) \
125  do { \
126  USING_AH_NAMESPACE; \
127  using namespace std::chrono; \
128  auto now = high_resolution_clock::now(); \
129  unsigned long t = \
130  duration_cast<milliseconds>(now - start_time).count(); \
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 #endif // ARDUINO
141 
142 #include "DebugVal.hpp"
143 
151 #define DEBUGVAL(...) DEBUGVALN(COUNT(__VA_ARGS__))(__VA_ARGS__)
152 
153 #else // Debugging disabled ====================================================
154 
155 #define DEBUG(x) \
156  do { \
157  } while (0)
158 #define DEBUGREF(x) \
159  do { \
160  } while (0)
161 #define DEBUGFN(x) \
162  do { \
163  } while (0)
164 #define DEBUGTIME(x) \
165  do { \
166  } while (0)
167 #define DEBUGVAL(...) \
168  do { \
169  } while (0)
170 
171 #endif
172 
Warnings.hpp
SettingsWrapper.hpp
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
DebugVal.hpp
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10