This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
Debug.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #if !(defined(ESP32) || defined(ESP8266))
6 // Uncomment this line to flush the output after each debug statement
7 #define FLUSH_ON_EVERY_DEBUG_STATEMENT
8 // TODO: I should probably use Streams instead of Prints, so Espressif boards
9 // can flush as well.
10 #endif
11 
12 #ifdef FLUSH_ON_EVERY_DEBUG_STATEMENT
13 #define ENDL endl
14 #else
15 #define ENDL "\r\n"
16 #endif
17 
18 #ifdef ARDUINO
19 
20 // Uncomment this line to override Arduino IDE debug output
21 // #define DEBUG_OUT Serial
22 
23 #if (defined(ESP32) || defined(ESP8266)) && \
24  defined(FLUSH_ON_EVERY_DEBUG_STATEMENT)
25 #error "ESP32 and ESP8266 don't support flushing `Print` objects"
26 #endif
27 
28 #ifndef PRINTSTREAM_FALLBACK
29 #include "PrintStream.hpp"
30 #else
31 #include <Arduino.h> // Print
32 typedef Print &manipulator(Print &);
33 inline Print &endl(Print &printer) {
34  printer.println();
35 #if !(defined(ESP32) || defined(ESP8266))
36  printer.flush();
37 #endif
38  return printer;
39 }
40 template <class T> inline Print &operator<<(Print &printer, const T printable) {
41  printer.print(printable);
42  return printer;
43 }
44 template <> inline Print &operator<<(Print &printer, manipulator pf) {
45  return pf(printer);
46 }
47 #endif
48 
49 #else // No Arduino
50 
51 #include <Arduino.h>
52 #include "PrintStream.hpp"
53 
54 #endif
55 
56 #define STR_HELPER(x) #x
57 #define STR(x) STR_HELPER(x)
58 
59 #define FUNC_LOCATION \
60  '[' << __PRETTY_FUNCTION__ << F(" @ line " STR(__LINE__) "]:\t")
61 #define LOCATION "[" __FILE__ ":" STR(__LINE__) "]:\t"
62 
63 #define NAMEDVALUE(x) F(STR(x) " = ") << x
64 
65 #ifdef DEBUG_OUT
66 
67 #pragma message("Debugging enabled on output " STR(DEBUG_OUT))
68 
69 #define DEBUG(x) \
70  do { \
71  DEBUG_OUT << x << ENDL; \
72  } while (0)
73 
74 #define DEBUGREF(x) \
75  do { \
76  DEBUG_OUT << F(LOCATION) << x << ENDL; \
77  } while (0)
78 
79 #define DEBUGFN(x) \
80  do { \
81  DEBUG_OUT << FUNC_LOCATION << x << ENDL; \
82  } while (0)
83 
84 #ifdef ARDUINO
85 #define DEBUGTIME(x) \
86  do { \
87  unsigned long t = millis(); \
88  unsigned long h = t / (60UL * 60 * 1000); \
89  unsigned long m = (t / (60UL * 1000)) % 60; \
90  unsigned long s = (t / (1000UL)) % 60; \
91  unsigned long ms = t % 1000; \
92  const char *ms_zeros = ms > 99 ? "" : (ms > 9 ? "0" : "00"); \
93  DEBUG_OUT << '[' << h << ':' << m << ':' << s << '.' << ms_zeros << ms \
94  << "]:\t" << x << ENDL; \
95  } while (0)
96 #endif
97 
98 #define DEBUGVAL(...) DEBUGVALN(COUNT(__VA_ARGS__))(__VA_ARGS__)
99 
100 #define COUNT(...) COUNT_HELPER(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
101 #define COUNT_HELPER(N1, N2, N3, N4, N5, N6, N7, N8, N9, N10, N, ...) N
102 
103 #define DEBUGVALN(N) DEBUGVALN_HELPER(N)
104 #define DEBUGVALN_HELPER(N) DEBUGVAL##N
105 
106 #define DEBUGVAL10(x, ...) \
107  do { \
108  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
109  DEBUGVAL9(__VA_ARGS__); \
110  } while (0)
111 #define DEBUGVAL9(x, ...) \
112  do { \
113  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
114  DEBUGVAL8(__VA_ARGS__); \
115  } while (0)
116 #define DEBUGVAL8(x, ...) \
117  do { \
118  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
119  DEBUGVAL7(__VA_ARGS__); \
120  } while (0)
121 #define DEBUGVAL7(x, ...) \
122  do { \
123  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
124  DEBUGVAL6(__VA_ARGS__); \
125  } while (0)
126 #define DEBUGVAL6(x, ...) \
127  do { \
128  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
129  DEBUGVAL5(__VA_ARGS__); \
130  } while (0)
131 #define DEBUGVAL5(x, ...) \
132  do { \
133  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
134  DEBUGVAL4(__VA_ARGS__); \
135  } while (0)
136 #define DEBUGVAL4(x, ...) \
137  do { \
138  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
139  DEBUGVAL3(__VA_ARGS__); \
140  } while (0)
141 #define DEBUGVAL3(x, ...) \
142  do { \
143  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
144  DEBUGVAL2(__VA_ARGS__); \
145  } while (0)
146 #define DEBUGVAL2(x, ...) \
147  do { \
148  DEBUG_OUT << NAMEDVALUE(x) << ", "; \
149  DEBUGVAL1(__VA_ARGS__); \
150  } while (0)
151 #define DEBUGVAL1(x) \
152  do { \
153  DEBUG_OUT << NAMEDVALUE(x) << ENDL; \
154  } while (0)
155 
156 #else // Debugging disabled
157 
158 #define DEBUG(x) \
159  do { \
160  } while (0)
161 #define DEBUGREF(x) \
162  do { \
163  } while (0)
164 #define DEBUGFN(x) \
165  do { \
166  } while (0)
167 #ifdef ARDUINO
168 #define DEBUGTIME(x) \
169  do { \
170  } while (0)
171 #endif
172 #define DEBUGVAL(...) \
173  do { \
174  } while (0)
175 
176 #endif
manipulator
Print & manipulator(Print &)
PrintStream library
Definition: PrintStream.hpp:9
operator<<
Print & operator<<(Print &printer, const __FlashStringHelper *s)
Definition: PrintStream.cpp:99
endl
Print & endl(Print &printer)
Definition: PrintStream.cpp:27
PrintStream.hpp
SettingsWrapper.hpp