-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.h
48 lines (40 loc) · 956 Bytes
/
logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once
#include <Arduino.h>
class Logger {
public:
void log(const char *format, ...) {
if (!verbose) return;
// construct message
char msg[100];
va_list args;
va_start(args, format);
vsnprintf(msg, sizeof(msg), format, args);
va_end(args);
if (logName != nullptr) {
print(logName);
print(": ");
}
print(msg);
print("\n");
}
inline void setLogOutput(Print& output) { logOutput = &output; }
inline void setVerbosity(const bool _verbose) { verbose = _verbose; }
protected:
inline void setLogName(const char* name) { logName = name; }
private:
const char* logName = nullptr;
Print *logOutput = nullptr;
bool verbose = true;
void print(const char *msg) {
if (logOutput != nullptr) {
logOutput->print(msg);
} else {
#ifdef ESP32
log_printf("%s", msg);
#else
// default to Serial
Serial.print(msg);
#endif
}
}
};