Arduino Mavlink
Loading...
Searching...
No Matches
MavlinkLogger.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 pschatzmann
3
4#pragma once
5#include <stdarg.h>
6#include "Arduino.h"
8
9#if !defined(ESP32)
10#undef MAVLINK_USE_ESP_LOGGER
11#define MAVLINK_USE_ESP_LOGGER false
12#endif
13
14
16#define MAV_ERROR(...) ESP_LOGE("MAVLINK", __VA_ARGS__)
17#define MAV_WARN(...) ESP_LOGW("MAVLINK", __VA_ARGS__)
18#define MAV_INFO(...) ESP_LOGI("MAVLINK", __VA_ARGS__)
19#define MAV_DEBUG(...) ESP_LOGD("MAVLINK", __VA_ARGS__)
20
21#else
22namespace mavlink_controller {
23
24/// Log levels for MavlinkLogger
25enum MavlinkLogLevel { ERROR = 0, WARN = 1, INFO = 2, DEBUG = 3 };
26
27/**
28 * @class MavlinkLoggerClass
29 * @brief Simple logger with log levels and configurable Print output (default
30 * Serial).
31 *
32 * Usage:
33 * MavlinkLogger logger;
34 * logger.log(MavlinkLogLevel::INFO, "Hello %d", 42);
35 * logger.setOutput(&myPrint);
36 */
38 public:
39
40 MavlinkLoggerClass() = default;
41
42 /// Set the Print output target (e.g., Serial)
43 void setOutput(Print* out) { output = out; }
44
45 /// Set the minimum log level
46 void setLevel(MavlinkLogLevel lvl) { level = lvl; }
47
48 /// Initialize logger with log level and output (default: INFO, Serial)
49 bool begin(MavlinkLogLevel lvl = INFO, Print* out = &Serial) {
50 setLevel(lvl);
51 setOutput(out);
52 return output != nullptr;
53 }
54
55
56 /// Log an error message (varargs, printf-style)
57 size_t error(const char* fmt, ...) {
58 va_list args;
59 va_start(args, fmt);
60 size_t res = log_va(ERROR, fmt, args);
61 va_end(args);
62 return res;
63 }
64
65
66 /// Log a warning message (varargs, printf-style)
67 size_t warn(const char* fmt, ...) {
68 va_list args;
69 va_start(args, fmt);
70 size_t res = log_va(WARN, fmt, args);
71 va_end(args);
72 return res;
73 }
74
75 /// Log an info message (varargs, printf-style)
76 size_t info(const char* fmt, ...) {
77 va_list args;
78 va_start(args, fmt);
79 size_t res = log_va(INFO, fmt, args);
80 va_end(args);
81 return res;
82 }
83
84
85 /// Log a debug message (varargs, printf-style)
86 size_t debug(const char* fmt, ...) {
87 va_list args;
88 va_start(args, fmt);
89 size_t res = log_va(DEBUG, fmt, args);
90 va_end(args);
91 return res;
92 }
93
94 /// Log a message with a specific log level (varargs, printf-style)
95 size_t log(MavlinkLogLevel lvl, const char* fmt, ...) {
96 va_list args;
97 va_start(args, fmt);
98 size_t res = log_va(lvl, fmt, args);
99 va_end(args);
100 return res;
101 }
102
103 private:
104 Print* output = &Serial;
105 MavlinkLogLevel level = INFO;
106
107 // Helper for va_list logging
108 size_t log_va(MavlinkLogLevel lvl, const char* fmt, va_list args) {
109 if (lvl > level || !output) return 0;
110 char buf[MAVLINK_MAX_LOG_SIZE];
111 vsnprintf(buf, sizeof(buf), fmt, args);
112 return output->println(buf);
113 }
114};
115
116/// Global instance of the MavlinkLoggerClass
117static MavlinkLoggerClass MavlinkLogger;
118
119}
120
121#define MAV_ERROR(...) MavlinkLogger.error(__VA_ARGS__)
122#define MAV_WARN(...) MavlinkLogger.warn(__VA_ARGS__)
123#define MAV_INFO(...) MavlinkLogger.info(__VA_ARGS__)
124#define MAV_DEBUG(...) MavlinkLogger.debug(__VA_ARGS__)
125
126#endif
#define MAVLINK_MAX_LOG_SIZE
Definition: MavlinkConfig.h:57
#define MAVLINK_USE_ESP_LOGGER
Definition: MavlinkLogger.h:11