TinyRobotics
Loading...
Searching...
No Matches
LoggerClass.h
1#pragma once
2
3#include <cstdarg>
4#include <cstdio>
5#include <cstring>
6
7namespace tinyrobotics {
8
9/**
10 * @class LoggerClass
11 * @ingroup utils
12 * @brief Simple, cross-platform logger for Arduino and C++ environments.
13 *
14 * This class provides a lightweight logging facility with support for log levels (ERROR, WARN, INFO, DEBUG),
15 * printf-style formatting, and platform-adaptive output (Serial for Arduino, printf for standard C++). It is
16 * designed for embedded and robotics applications where monitoring, debugging, and event tracking are essential.
17 *
18 * ## Features
19 * - Four log levels: ERROR, WARN, INFO, DEBUG
20 * - Configurable minimum log level (messages below this are suppressed)
21 * - printf-style formatting for flexible message construction
22 * - Platform-adaptive output: uses Serial on Arduino, printf elsewhere
23 * - Thread-safe for most embedded use cases (no dynamic memory allocation)
24 * - Global logger instance (`TRLogger`) for convenience
25 *
26 *
27 * ## Usage Example
28 * @code
29 * TRLogger.setLevel(LoggerClass::DEBUG);
30 * TRLogger.info("System started, version: %d", version);
31 * TRLogger.error("Failed to open file: %s", filename);
32 * TRLogger.debug("x=%.2f, y=%.2f", x, y);
33 * @endcode
34 *
35 * ## Methods
36 * - setLevel(level): Set the minimum log level
37 * - error(fmt, ...);
38 * - warn(fmt, ...);
39 * - info(fmt, ...);
40 * - debug(fmt, ...);
41 *
42 * ## Log Levels
43 * - ERROR: Critical errors and failures
44 * - WARN: Warnings and recoverable issues
45 * - INFO: Informational messages and status updates
46 * - DEBUG: Detailed debug output for development
47 *
48 * ## Platform Support
49 * - On Arduino, output is sent to Serial (or a user-supplied Print object)
50 * - On standard C++, output is sent to stdout via printf
51 *
52 * @author Phil Schatzmann
53
54 */
55class LoggerClass {
56 public:
57 enum Level { ERROR = 0, WARN = 1, INFO = 2, DEBUG = 3 };
58
59 LoggerClass(Level level = INFO) : minLevel(level) {}
60
61#ifdef ARDUINO
62 bool begin(Level level, Print& out = Serial) {
64 p_out = &out;
65 return true;
66 }
67#else
68 bool begin(Level level) { setLevel(level); return true; }
69#endif
70
71 void setLevel(Level level) { minLevel = level; }
72
73 void error(const char* fmt, ...) {
74 va_list args;
75 va_start(args, fmt);
76 vlog(ERROR, fmt, args);
77 va_end(args);
78 }
79 void warn(const char* fmt, ...) {
80 va_list args;
81 va_start(args, fmt);
82 vlog(WARN, fmt, args);
83 va_end(args);
84 }
85 void info(const char* fmt, ...) {
86 va_list args;
87 va_start(args, fmt);
88 vlog(INFO, fmt, args);
89 va_end(args);
90 }
91 void debug(const char* fmt, ...) {
92 va_list args;
93 va_start(args, fmt);
94 vlog(DEBUG, fmt, args);
95 va_end(args);
96 }
97
98 protected:
99 Level minLevel;
100#ifdef ARDUINO
101 Print* p_out = &Serial;
102#endif
103
104 void vlog(Level level, const char* fmt, va_list args) {
105 if (level > minLevel) return;
106 const char* levelStr = levelToString(level);
107 char buf[256];
108 vsnprintf(buf, sizeof(buf), fmt, args);
109#ifdef ARDUINO
110 p_out->print(levelStr);
111 p_out->print(": ");
112 p_out->println(buf);
113#else
114 printf("%s: %s\n", levelStr, buf);
115#endif
116 }
117
118 const char* levelToString(Level level) const {
119 switch (level) {
120 case ERROR:
121 return "ERROR";
122 case WARN:
123 return "WARN";
124 case INFO:
125 return "INFO";
126 case DEBUG:
127 return "DEBUG";
128 default:
129 return "LOG";
130 }
131 }
132};
133
134
135/**
136 * @brief Global logger instance for convenience.
137 *
138 * Use `TRLogger` for simple, global logging throughout your application.
139 * Example:
140 * @code
141 * TRLogger.info("Hello, world!");
142 * @endcode
143 */
144static LoggerClass TRLogger;
145
146} // namespace tinyrobotics
Simple, cross-platform logger for Arduino and C++ environments.
Definition: LoggerClass.h:55