-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArduinoSimpleLogging.h
More file actions
68 lines (52 loc) · 1.39 KB
/
Copy pathArduinoSimpleLogging.h
File metadata and controls
68 lines (52 loc) · 1.39 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* ArduinoSimpleLogging
*
* https://github.com/janLo/ArduinoSimpleLogging
*
* Copyright (c) 2017 Jan Losinski.
* MIT License
*/
#ifndef ARDUINOLOGGING_H
#define ARDUINOLOGGING_H
#include <forward_list>
#include <Print.h>
class ArduinoSimpleLogging {
public:
enum Level {
ERROR = 0x01,
WARNING = (0x01 << 1),
INFO = (0x01 << 2),
DEBUG = (0x01 << 3)
};
class LogTarget : public Print {
public:
explicit LogTarget(const Level level) : _level(level) {}
LogTarget(const LogTarget &) = delete;
LogTarget(const LogTarget &&) = delete;
size_t write(uint8_t) override;
size_t write(const uint8_t *buffer, size_t size) override;
private:
Level _level;
};
static LogTarget debug;
static LogTarget info;
static LogTarget warning;
static LogTarget error;
size_t log(Level level, const uint8_t *buffer, size_t size);
size_t log(Level level, uint8_t);
void addHandler(Level level, Print &);
void removeHandler(Print &);
static String levelToString(Level level);
static Level stringToLevel(const String &levelName);
private:
struct LogHandler {
const uint8_t mask;
Print &stream;
static uint8_t makeMask(Level level);
LogHandler(Level level, Print &stream)
: mask(makeMask(level)), stream(stream) {}
};
std::forward_list<LogHandler> handlers;
};
extern ArduinoSimpleLogging Logger;
#endif // ARDUINOLOGGING_H