|
1 | 1 | #pragma once |
2 | 2 |
|
3 | | -#include "Stream.h" |
4 | 3 | #include "ArdStdio.h" |
| 4 | +#include "Stream.h" |
5 | 5 |
|
6 | 6 | namespace arduino { |
7 | 7 |
|
8 | | - |
9 | 8 | /** |
10 | 9 | * @brief A simple Logger that writes messages dependent on the log level |
11 | | - * |
| 10 | + * |
12 | 11 | */ |
13 | 12 |
|
14 | 13 | class ArduinoLogger { |
15 | | - public: |
16 | | - /** |
17 | | - * @brief Supported log levels |
18 | | - * |
19 | | - */ |
20 | | - enum LogLevel { |
21 | | - Debug, |
22 | | - Info, |
23 | | - Warning, |
24 | | - Error |
25 | | - }; |
26 | | - |
27 | | - const char* LogLevelTxt[4] = {"Debug", "Info", "Warning", "Error"}; |
28 | | - |
29 | | - // activate the logging |
30 | | - virtual void setLogger(Stream& out, LogLevel level=Error){ |
31 | | - this->log_stream_ptr = &out; |
32 | | - this->log_level = level; |
| 14 | + public: |
| 15 | + /** |
| 16 | + * @brief Supported log levels |
| 17 | + * |
| 18 | + */ |
| 19 | + enum LogLevel { Debug, Info, Warning, Error }; |
| 20 | + |
| 21 | + const char* LogLevelTxt[4] = {"Debug", "Info", "Warning", "Error"}; |
| 22 | + |
| 23 | + // activate the logging |
| 24 | + virtual void setLogger(Stream& out, LogLevel level = Error) { |
| 25 | + this->log_stream_ptr = &out; |
| 26 | + this->log_level = level; |
| 27 | + } |
| 28 | + |
| 29 | + // checks if the logging is active |
| 30 | + virtual bool isLogging() { return log_stream_ptr != nullptr; } |
| 31 | + |
| 32 | + virtual void error(const char* str, const char* str1 = nullptr, |
| 33 | + const char* str2 = nullptr) { |
| 34 | + log(Error, str, str1, str2); |
| 35 | + } |
| 36 | + |
| 37 | + virtual void info(const char* str, const char* str1 = nullptr, |
| 38 | + const char* str2 = nullptr) { |
| 39 | + log(Info, str, str1, str2); |
| 40 | + } |
| 41 | + |
| 42 | + virtual void warning(const char* str, const char* str1 = nullptr, |
| 43 | + const char* str2 = nullptr) { |
| 44 | + log(Warning, str, str1, str2); |
| 45 | + } |
| 46 | + |
| 47 | + virtual void debug(const char* str, const char* str1 = nullptr, |
| 48 | + const char* str2 = nullptr) { |
| 49 | + log(Debug, str, str1, str2); |
| 50 | + } |
| 51 | + |
| 52 | + // write an message to the log |
| 53 | + virtual void log(LogLevel current_level, const char* str, |
| 54 | + const char* str1 = nullptr, const char* str2 = nullptr) { |
| 55 | + if (log_stream_ptr != nullptr) { |
| 56 | + if (current_level >= log_level) { |
| 57 | + log_stream_ptr->print("Emulator - "); |
| 58 | + log_stream_ptr->print((char*)LogLevelTxt[current_level]); |
| 59 | + log_stream_ptr->print(": "); |
| 60 | + log_stream_ptr->print((char*)str); |
| 61 | + if (str1 != nullptr) { |
| 62 | + log_stream_ptr->print(" "); |
| 63 | + log_stream_ptr->print((char*)str1); |
33 | 64 | } |
34 | | - |
35 | | - // checks if the logging is active |
36 | | - virtual bool isLogging(){ |
37 | | - return log_stream_ptr!=nullptr; |
38 | | - } |
39 | | - |
40 | | - virtual void error(const char *str, const char* str1=nullptr, const char* str2=nullptr){ |
41 | | - log(Error, str, str1, str2); |
42 | | - } |
43 | | - |
44 | | - virtual void info(const char *str, const char* str1=nullptr, const char* str2=nullptr){ |
45 | | - log(Info, str, str1, str2); |
46 | | - } |
47 | | - |
48 | | - virtual void warning(const char *str, const char* str1=nullptr, const char* str2=nullptr){ |
49 | | - log(Warning, str, str1, str2); |
| 65 | + if (str2 != nullptr) { |
| 66 | + log_stream_ptr->print(" "); |
| 67 | + log_stream_ptr->print((char*)str2); |
50 | 68 | } |
51 | | - |
52 | | - virtual void debug(const char *str, const char* str1=nullptr, const char* str2=nullptr){ |
53 | | - log(Debug, str, str1, str2); |
54 | | - } |
55 | | - |
56 | | - |
57 | | - // write an message to the log |
58 | | - virtual void log(LogLevel current_level, const char *str, const char* str1=nullptr, const char* str2=nullptr){ |
59 | | - if (log_stream_ptr!=nullptr){ |
60 | | - if (current_level >= log_level){ |
61 | | - log_stream_ptr->print("Emulator - "); |
62 | | - log_stream_ptr->print((char*)LogLevelTxt[current_level]); |
63 | | - log_stream_ptr->print(": "); |
64 | | - log_stream_ptr->print((char*)str); |
65 | | - if (str1!=nullptr){ |
66 | | - log_stream_ptr->print(" "); |
67 | | - log_stream_ptr->print((char*)str1); |
68 | | - } |
69 | | - if (str2!=nullptr){ |
70 | | - log_stream_ptr->print(" "); |
71 | | - log_stream_ptr->print((char*)str2); |
72 | | - } |
73 | | - log_stream_ptr->println(); |
74 | | - log_stream_ptr->flush(); |
75 | | - } |
76 | | - } |
77 | | - } |
78 | | - |
79 | | - protected: |
80 | | - Stream *log_stream_ptr = &Serial; |
81 | | - LogLevel log_level = Warning; |
82 | | - |
83 | | - |
| 69 | + log_stream_ptr->println(); |
| 70 | + log_stream_ptr->flush(); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + protected: |
| 76 | + Stream* log_stream_ptr = &Serial; |
| 77 | + LogLevel log_level = Warning; |
84 | 78 | }; |
85 | 79 |
|
86 | 80 | extern ArduinoLogger Logger; |
87 | 81 |
|
88 | | -} |
89 | | - |
90 | | - |
| 82 | +} // namespace arduino |
0 commit comments