SC16IS752 Serial Bridge Arduino Library
Loading...
Searching...
No Matches
SC16IS752Logger.h
1/*
2SC16IS752 Logger - Logging utility for SC16IS752 dual UART
3*/
4
5#pragma once
6
7#include <stdarg.h>
8#include <string.h>
9#include <stdio.h>
10#include "Arduino.h"
11
14 public:
16 enum LogLevel {
17 LEVEL_DEBUG = 0,
18 LEVEL_INFO = 1,
19 LEVEL_WARNING = 2,
20 LEVEL_ERROR = 3,
21 LEVEL_CRITICAL = 4,
22 LEVEL_NONE = 5
23 };
24
25 SC16IS752Logger(Stream& stream, LogLevel level = LEVEL_INFO)
26 : stream_(stream), log_level_(level) {}
27
29 void setLogLevel(LogLevel level) { log_level_ = level; }
30
32 LogLevel getLogLevel() { return log_level_; }
33
35 void debug(const char* format, ...) {
36 if (log_level_ <= LEVEL_DEBUG) {
37 write("[DEBUG] ");
38 va_list args;
39 va_start(args, format);
40 vlog(format, args);
41 va_end(args);
42 newline();
43 }
44 }
45
47 void info(const char* format, ...) {
48 if (log_level_ <= LEVEL_INFO) {
49 write("[INFO] ");
50 va_list args;
51 va_start(args, format);
52 vlog(format, args);
53 va_end(args);
54 newline();
55 }
56 }
57
59 void warn(const char* format, ...) {
60 if (log_level_ <= LEVEL_WARNING) {
61 write("[WARN] ");
62 va_list args;
63 va_start(args, format);
64 vlog(format, args);
65 va_end(args);
66 newline();
67 }
68 }
69
71 void error(const char* format, ...) {
72 if (log_level_ <= LEVEL_ERROR) {
73 write("[ERROR] ");
74 va_list args;
75 va_start(args, format);
76 vlog(format, args);
77 va_end(args);
78 newline();
79 }
80 }
81
83 void critical(const char* format, ...) {
84 if (log_level_ <= LEVEL_CRITICAL) {
85 write("[CRITICAL] ");
86 va_list args;
87 va_start(args, format);
88 vlog(format, args);
89 va_end(args);
90 newline();
91 }
92 }
93
95 void log(const char* format, ...) {
96 if (log_level_ <= LEVEL_DEBUG) {
97 va_list args;
98 va_start(args, format);
99 vlog(format, args);
100 va_end(args);
101 }
102 }
103
105 void logln(const char* format, ...) {
106 if (log_level_ <= LEVEL_DEBUG) {
107 va_list args;
108 va_start(args, format);
109 vlog(format, args);
110 va_end(args);
111 stream_.write('\n');
112 }
113 }
114
116 void write(const char* str) { stream_.write((const uint8_t*)str, strlen(str)); }
117
119 void write(char c) { stream_.write(c); }
120
122 void write(int value) { stream_.print(value); }
123
125 void write(unsigned int value) { stream_.print(value); }
126
128 void write(long value) { stream_.print(value); }
129
131 void write(unsigned long value) { stream_.print(value); }
132
134 void write(float value) { stream_.print(value); }
135
137 void write(float value, int precision) { stream_.print(value, precision); }
138
140 void newline() { stream_.write('\n'); }
141
142 private:
143 Stream& stream_;
144 LogLevel log_level_;
145
147 void vlog(const char* format, va_list args) {
148 char buffer[256];
149 vsnprintf(buffer, sizeof(buffer), format, args);
150 stream_.write((const uint8_t*)buffer, strlen(buffer));
151 }
152};
Simple logger for SC16IS752 channels with variadic parameter support and log levels.
Definition SC16IS752Logger.h:13
void setLogLevel(LogLevel level)
Set the current log level.
Definition SC16IS752Logger.h:29
void write(long value)
Log a long integer.
Definition SC16IS752Logger.h:128
void write(char c)
Log a single character.
Definition SC16IS752Logger.h:119
void log(const char *format,...)
Log a formatted message with variadic parameters (respects log level LEVEL_DEBUG)
Definition SC16IS752Logger.h:95
void write(float value)
Log a floating point number.
Definition SC16IS752Logger.h:134
void logln(const char *format,...)
Log with newline (respects log level LEVEL_DEBUG)
Definition SC16IS752Logger.h:105
void write(unsigned int value)
Log an unsigned integer.
Definition SC16IS752Logger.h:125
void info(const char *format,...)
Log a formatted info message.
Definition SC16IS752Logger.h:47
void critical(const char *format,...)
Log a formatted critical message.
Definition SC16IS752Logger.h:83
void newline()
Log a newline.
Definition SC16IS752Logger.h:140
void warn(const char *format,...)
Log a formatted warning message.
Definition SC16IS752Logger.h:59
void write(float value, int precision)
Log a floating point number with precision.
Definition SC16IS752Logger.h:137
void debug(const char *format,...)
Log a formatted debug message.
Definition SC16IS752Logger.h:35
void error(const char *format,...)
Log a formatted error message.
Definition SC16IS752Logger.h:71
void write(unsigned long value)
Log an unsigned long integer.
Definition SC16IS752Logger.h:131
LogLevel
Log levels.
Definition SC16IS752Logger.h:16
void write(int value)
Log an integer.
Definition SC16IS752Logger.h:122
LogLevel getLogLevel()
Get the current log level.
Definition SC16IS752Logger.h:32
void write(const char *str)
Log a simple string.
Definition SC16IS752Logger.h:116