Arduino PulseWire Transceiver Library
Loading...
Searching...
No Matches
Logger.h
1#pragma once
2
3#include <Arduino.h>
4#include <cstdarg> // For va_list, va_start, and va_end
5
6namespace pulsewire {
7
16class Logger {
17 public:
18 enum LogLevel {
19 LOG_LEVEL_NONE,
20 LOG_LEVEL_ERROR,
21 LOG_LEVEL_WARNING,
22 LOG_LEVEL_INFO,
23 LOG_LEVEL_DEBUG
24 };
25
30 static void setLogLevel(LogLevel level) { _logLevel = level; }
31
37 static void error(const char* format, ...) {
38 if (_logLevel >= LOG_LEVEL_ERROR) {
39 Serial.print("[ERROR] ");
42 logFormatted(format, args);
43 va_end(args);
44 }
45 }
46
52 static void warning(const char* format, ...) {
53 if (_logLevel >= LOG_LEVEL_WARNING) {
54 Serial.print("[WARNING] ");
57 logFormatted(format, args);
58 va_end(args);
59 }
60 }
61
67 static void info(const char* format, ...) {
68 if (_logLevel >= LOG_LEVEL_INFO) {
69 Serial.print("[INFO] ");
72 logFormatted(format, args);
73 va_end(args);
74 }
75 }
76
82 static void debug(const char* format, ...) {
83 if (_logLevel >= LOG_LEVEL_DEBUG) {
84 Serial.print("[DEBUG] ");
87 logFormatted(format, args);
88 va_end(args);
89 }
90 }
91
97 static void setOutput(Print& print) { output = &print; }
98
99 private:
100 static LogLevel _logLevel;
101 static Print* output;
102
108 static void logFormatted(const char* format, va_list args) {
109 char buffer[256]; // Adjust the buffer size as needed
110 vsnprintf(buffer, sizeof(buffer), format, args);
111 output->println(buffer);
112 }
113};
114
115// Initialize the static member variable
116Logger::LogLevel Logger::_logLevel = Logger::LOG_LEVEL_NONE;
117Print* Logger::output = &Serial;
118
119} // namespace pulsewire
120
121#define TRACE() Logger::debug("%s:%d - %s", __FILE__, __LINE__, __func__)
122//#define TRACE()
Simple logger class for debugging and logging messages.
Definition Logger.h:16
static void debug(const char *format,...)
Log a debug message with formatting.
Definition Logger.h:82
static void error(const char *format,...)
Log an error message with formatting.
Definition Logger.h:37
static void setOutput(Print &print)
Set the Output object
Definition Logger.h:97
static void setLogLevel(LogLevel level)
Set the global log level.
Definition Logger.h:30
static void warning(const char *format,...)
Log a warning message with formatting.
Definition Logger.h:52
static void info(const char *format,...)
Log an informational message with formatting.
Definition Logger.h:67
Small, header-only vector replacement for non-STL environments.
Definition Vector.h:29