TinyWifiDongle 0.1.0
Header-only USB WiFi dongle for Arduino
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1#pragma once
6#include <Print.h>
7#include <stdarg.h>
8#include <stdint.h>
9#include <stdio.h>
10#include <string.h>
11
12#ifndef TWD_PRINTF_FORMAT
13#define TWD_PRINTF_FORMAT(fmt, args) __attribute__((format(printf, fmt, args)))
14#endif
15
16namespace twd {
17
26 public:
28 enum class LogLevel : uint8_t { Debug, Info, Warning, Error };
29
31 void begin(Print& out, LogLevel level = LogLevel::Info) {
32 level_ = level;
33 out_ = &out;
34 }
35
36 void end() { out_ = nullptr; }
38 void flush() {
39 Print* out = out_;
40 if (out) out->flush();
41 }
42
44 void setLevel(LogLevel level) { level_ = level; }
45 LogLevel level() const { return level_; }
46
48 bool enabled(LogLevel level = LogLevel::Info) const { return out_ != nullptr && level >= level_; }
49
51 void log(LogLevel level, const char* fmt, ...) TWD_PRINTF_FORMAT(3, 4) {
52 va_list ap;
53 va_start(ap, fmt);
54 vlog(level, fmt, ap);
55 va_end(ap);
56 }
57 void debug(const char* fmt, ...) TWD_PRINTF_FORMAT(2, 3) {
58 va_list ap;
59 va_start(ap, fmt);
60 vlog(LogLevel::Debug, fmt, ap);
61 va_end(ap);
62 }
63 void info(const char* fmt, ...) TWD_PRINTF_FORMAT(2, 3) {
64 va_list ap;
65 va_start(ap, fmt);
66 vlog(LogLevel::Info, fmt, ap);
67 va_end(ap);
68 }
69 void warning(const char* fmt, ...) TWD_PRINTF_FORMAT(2, 3) {
70 va_list ap;
71 va_start(ap, fmt);
72 vlog(LogLevel::Warning, fmt, ap);
73 va_end(ap);
74 }
75 void error(const char* fmt, ...) TWD_PRINTF_FORMAT(2, 3) {
76 va_list ap;
77 va_start(ap, fmt);
78 vlog(LogLevel::Error, fmt, ap);
79 va_end(ap);
80 }
81
83 void vlog(LogLevel level, const char* fmt, va_list ap) {
84 Print* out = out_;
85 if (!out || level < level_) return;
86 static constexpr char kPrefix[] = "[dongle] ? ";
87 char buf[256];
88 size_t len = sizeof(kPrefix) - 1;
89 memcpy(buf, kPrefix, len);
90 buf[len - 2] = "DIWE"[(int)level];
91 int n = vsnprintf(buf + len, sizeof(buf) - len - 2, fmt, ap);
92 if (n < 0) return;
93 len += (size_t)n < sizeof(buf) - len - 2 ? (size_t)n : sizeof(buf) - len - 3;
94 buf[len++] = '\r';
95 buf[len++] = '\n';
96 out->write((const uint8_t*)buf, len);
97 }
98
99 private:
100 Print* volatile out_ = nullptr;
101 LogLevel level_ = LogLevel::Info;
102};
103
104} // namespace twd
105
static twd::LoggerClass TWD_LOGGER
The logger used by all components; configure it in the sketch with TWD_LOGGER.begin(Serial).
Definition Logger.h:107
Printf-style logger with log levels; use the global TWD_LOGGER object.
Definition Logger.h:25
void error(const char *fmt,...)
Definition Logger.h:75
void vlog(LogLevel level, const char *fmt, va_list ap)
va_list variant of log().
Definition Logger.h:83
bool enabled(LogLevel level=LogLevel::Info) const
True if lines of level are written somewhere.
Definition Logger.h:48
void flush()
Waits until the output has sent what was written (e.g. before a restart).
Definition Logger.h:38
void setLevel(LogLevel level)
Lowest level that is written.
Definition Logger.h:44
void warning(const char *fmt,...)
Definition Logger.h:69
void debug(const char *fmt,...)
Definition Logger.h:57
void log(LogLevel level, const char *fmt,...)
Formats and writes one line of level; does nothing if that level is not enabled.
Definition Logger.h:51
LogLevel
Severity of a log line; lines below the level set with begin() are discarded.
Definition Logger.h:28
void end()
Stop logging.
Definition Logger.h:36
void begin(Print &out, LogLevel level=LogLevel::Info)
Lines of level and above are written to out (e.g. Serial); call before starting the dongle.
Definition Logger.h:31
LogLevel level() const
Definition Logger.h:45
void info(const char *fmt,...)
Definition Logger.h:63