arduino-emulator
Loading...
Searching...
No Matches
ArduinoLogger.h
1/*
2 ArduinoLogger.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20#pragma once
21#include "StdioDevice.h"
22#include "api/Stream.h"
23
24namespace arduino {
25
32 public:
33 ArduinoLogger() = default;
34
39 enum LogLevel { Debug, Info, Warning, Error };
40
41 const char* LogLevelTxt[4] = {"Debug", "Info", "Warning", "Error"};
42
43 // activate the logging
44 void begin(Stream& out, LogLevel level = Warning) {
45 this->log_stream_ptr = &out;
46 this->log_level = level;
47 }
48
49 // checks if the logging is active
50 bool isLogging() { return log_stream_ptr != nullptr; }
51
52 void error(const char* str, const char* str1 = nullptr,
53 const char* str2 = nullptr) {
54 log(Error, str, str1, str2);
55 }
56
57 void info(const char* str, const char* str1 = nullptr,
58 const char* str2 = nullptr) {
59 log(Info, str, str1, str2);
60 }
61
62 void warning(const char* str, const char* str1 = nullptr,
63 const char* str2 = nullptr) {
64 log(Warning, str, str1, str2);
65 }
66
67 void debug(const char* str, const char* str1 = nullptr,
68 const char* str2 = nullptr) {
69 log(Debug, str, str1, str2);
70 }
71
72 // write an message to the log
73 void log(LogLevel current_level, const char* str, const char* str1 = nullptr,
74 const char* str2 = nullptr) {
75 if (log_stream_ptr != nullptr) {
76 if (current_level >= log_level) {
77 log_stream_ptr->print("Emulator - ");
78 log_stream_ptr->print((char*)LogLevelTxt[current_level]);
79 log_stream_ptr->print(": ");
80 log_stream_ptr->print((char*)str);
81 if (str1 != nullptr) {
82 log_stream_ptr->print(" ");
83 log_stream_ptr->print((char*)str1);
84 }
85 if (str2 != nullptr) {
86 log_stream_ptr->print(" ");
87 log_stream_ptr->print((char*)str2);
88 }
89 log_stream_ptr->println();
90 log_stream_ptr->flush();
91 }
92 }
93 }
94
95 protected:
96 Stream* log_stream_ptr = &Serial;
97 LogLevel log_level = Warning;
98};
99
100static ArduinoLogger Logger;
101
102} // namespace arduino
A simple Logger that writes messages dependent on the log level.
Definition ArduinoLogger.h:31
LogLevel
Supported log levels.
Definition ArduinoLogger.h:39
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31