DebugUtils
1.0.0
Cross-platform logging macros for Arduino and native tests
Loading...
Searching...
No Matches
src
debug.h
Go to the documentation of this file.
1
26
#pragma once
27
28
#include <Arduino.h>
29
87
#if defined(NATIVE_TEST)
89
// Native test environment: print to stdout via stringstream
90
#include <iostream>
91
#include <sstream>
92
#include <string>
93
98
class
String {
99
private
:
100
std::string data;
101
102
public
:
103
String() : data() {}
104
String(
const
char
*str) : data(str ? str :
""
) {}
105
String(
const
std::string &str) : data(str) {}
106
String(
char
c) : data(1, c) {}
107
String(
int
n,
int
base = 10);
108
String(
unsigned
int
n,
int
base = 10);
109
String(
long
n,
int
base = 10);
110
String(
unsigned
long
n,
int
base = 10);
111
112
const
char
*c_str()
const
{
return
data.c_str(); }
113
size_t
length()
const
{
return
data.length(); }
114
operator
const
char
*()
const
{
return
c_str(); }
115
116
String &operator=(
const
char
*str) {
117
data = str ? str :
""
;
118
return
*
this
;
119
}
120
String &operator=(
const
std::string &str) {
121
data = str;
122
return
*
this
;
123
}
124
};
125
130
class
__FlashStringHelper;
131
#define F(string_literal) \
132
(reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
133
#define PSTR(s) (s)
134
135
namespace
DebugSerial {
136
// Define print base constants if not already defined
137
#ifndef DEC
138
#define DEC 10
139
#define HEX 16
140
#define OCT 8
141
#define BIN 2
142
#endif
143
148
class
NativeSerial {
149
private
:
150
size_t
printNumber(
unsigned
long
long
n,
int
base);
151
size_t
printFloat(
double
number,
int
digits);
152
153
public
:
154
void
begin(
unsigned
long
baud = 0);
155
size_t
write(uint8_t c);
156
size_t
write(
const
uint8_t *buffer,
size_t
size);
157
void
flush();
158
159
// Print overloads matching Print.h interface
160
size_t
print(
const
__FlashStringHelper *);
161
size_t
print(
const
String &s);
162
size_t
print(
const
std::string &s);
163
size_t
print(
const
char
str[]);
164
size_t
print(
char
c);
165
size_t
print(
unsigned
char
n,
int
base = DEC);
166
size_t
print(
int
n,
int
base = DEC);
167
size_t
print(
unsigned
int
n,
int
base = DEC);
168
size_t
print(
long
n,
int
base = DEC);
169
size_t
print(
unsigned
long
n,
int
base = DEC);
170
size_t
print(
long
long
n,
int
base = DEC);
171
size_t
print(
unsigned
long
long
n,
int
base = DEC);
172
size_t
print(
double
n,
int
digits = 2);
173
174
// Println overloads matching Print.h interface
175
size_t
println(
const
__FlashStringHelper *);
176
size_t
println(
const
String &s);
177
size_t
println(
const
std::string &s);
178
size_t
println(
const
char
c[]);
179
size_t
println(
char
c);
180
size_t
println(
unsigned
char
n,
int
base = DEC);
181
size_t
println(
int
n,
int
base = DEC);
182
size_t
println(
unsigned
int
n,
int
base = DEC);
183
size_t
println(
long
n,
int
base = DEC);
184
size_t
println(
unsigned
long
n,
int
base = DEC);
185
size_t
println(
long
long
n,
int
base = DEC);
186
size_t
println(
unsigned
long
long
n,
int
base = DEC);
187
size_t
println(
double
n,
int
digits = 2);
188
size_t
println(
void
);
189
};
190
extern
NativeSerial instance;
191
}
// namespace DebugSerial
192
193
#ifdef SERIAL_PORT
194
#undef SERIAL_PORT
195
#endif
/* SERIAL_PORT */
200
#define SERIAL_PORT DebugSerial::instance
202
203
#elif defined(_DEBUG_)
204
#ifdef SERIAL_PORT
205
#undef SERIAL_PORT
206
#endif
/* SERIAL_PORT */
207
// Hardware environment: use SerialRTT
208
#include "SerialRTT.h"
214
#define SERIAL_PORT SerialRTT
215
#else
216
#ifndef SERIAL_PORT
217
// Default to Serial
223
#define SERIAL_PORT Serial
224
#endif
/*SERIAL_PORT*/
225
#endif
/*NATIVE_TEST*/
226
232
#define LOG_ERROR 1
234
#define LOG_WARN 2
236
#define LOG_INFO 3
238
#define LOG_DEBUG 4
240
247
#ifndef LOG_LEVEL
252
#define LOG_LEVEL LOG_INFO
253
#endif
255
256
// Logging macros - controlled by consuming project
258
#if defined(_DEBUG_) || defined(NATIVE_TEST)
259
// Hardware or native test: use SERIAL_PORT (SerialRTT or NativeSerial)
260
// Supports multiple arguments: _PL(value) or _PL(value, HEX) or _PL(value,
261
// BIN)
263
#define LOG_PRINT(level, ...) \
264
if (level <= LOG_LEVEL) \
265
SERIAL_PORT.print(__VA_ARGS__);
267
#define LOG_PRINTLN(level, ...) \
268
if (level <= LOG_LEVEL) \
269
SERIAL_PORT.println(__VA_ARGS__);
270
#else
271
// _DEBUG_ and NATIVE_TEST not defined: no logging
273
#define LOG_PRINT(level, ...)
275
#define LOG_PRINTLN(level, ...)
276
#endif
278
284
#define _PPE(...) LOG_PRINT(LOG_ERROR, __VA_ARGS__)
286
#define _PPW(...) LOG_PRINT(LOG_WARN, __VA_ARGS__)
288
#define _PP(...) LOG_PRINT(LOG_INFO, __VA_ARGS__)
290
#define _PPD(...) LOG_PRINT(LOG_DEBUG, __VA_ARGS__)
292
#define _PLE(...) LOG_PRINTLN(LOG_ERROR, __VA_ARGS__)
294
#define _PLW(...) LOG_PRINTLN(LOG_WARN, __VA_ARGS__)
296
#define _PL(...) LOG_PRINTLN(LOG_INFO, __VA_ARGS__)
298
#define _PLD(...) LOG_PRINTLN(LOG_DEBUG, __VA_ARGS__)
Generated by
1.9.8