Uniot Core
0.9.0
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2020 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
32
33#pragma once
34
42#ifndef __FUNC_NAME__
43#if defined(__GNUC__) || defined(__clang__)
44 #define __FUNC_NAME__ __func__
45#else
46 #define __FUNC_NAME__ __FUNCTION__
47#endif
48#endif
49
60#define UNIOT_LOG_LEVEL_ERROR 0
61#define UNIOT_LOG_LEVEL_WARN 1
62#define UNIOT_LOG_LEVEL_INFO 2
63#define UNIOT_LOG_LEVEL_DEBUG 3
64#define UNIOT_LOG_LEVEL_TRACE 4
65
72#ifndef UNIOT_LOG_LEVEL
73#define UNIOT_LOG_LEVEL UNIOT_LOG_LEVEL_DEBUG
74#endif
75
82// Included for both paths: UNUSED lives here, and the macros for levels that are filtered
83// out expand to it even when logging itself is enabled.
84#include <Common.h>
85
86#if UNIOT_LOG_ENABLED
87
88#include <Arduino.h>
89#include <pgmspace.h>
90
96#ifndef UNIOT_LOG_STREAM
97#define UNIOT_LOG_STREAM Serial
98#endif
99
106#ifndef UNIOT_LOG_BAUD_RATE
107#define UNIOT_LOG_BAUD_RATE 115200
108#endif
109
116#ifndef UNIOT_LOG_BUF_SIZE
117#define UNIOT_LOG_BUF_SIZE 256
118#endif
119
126#ifndef UNIOT_LOG_SET_READY
127#define UNIOT_LOG_SET_READY() \
128 do { \
129 if (!UNIOT_LOG_STREAM) { \
130 UNIOT_LOG_STREAM.begin(UNIOT_LOG_BAUD_RATE); \
131 UNIOT_LOG_STREAM.print("\n\n"); \
132 } \
133 } while (0)
134#endif
135
142#ifndef UNIOT_LOG_PRINT
143#define UNIOT_LOG_PRINT(...) \
144 do { \
145 if (UNIOT_LOG_STREAM) \
146 UNIOT_LOG_STREAM.print(__VA_ARGS__); \
147 } while (0)
148#endif
149
164#define UNIOT_LOG_STRINGIFY_(x) #x
165#define UNIOT_LOG_STRINGIFY(x) UNIOT_LOG_STRINGIFY_(x)
166
167/*
168 * The file and the line are literals, so they are concatenated into the format and go to
169 * flash with it. __func__ is a variable, not a literal, so it stays an argument in RAM.
170 */
171#define UNIOT_LOG(log_type, log_fmt, log_arg...) \
172 do { \
173 uniot_log_printf(PSTR("[" #log_type "][%lu][" __FILE__ ":" \
174 UNIOT_LOG_STRINGIFY(__LINE__) "][%s] " log_fmt "\n"), \
175 millis(), __FUNC_NAME__, ##log_arg); \
176 } while (0)
177
188#define UNIOT_LOG_IF(log_type, log_cond, log_fmt, log_arg...) \
189 do { \
190 if (log_cond) \
191 UNIOT_LOG(log_type, log_fmt, ##log_arg); \
192 } while (0)
193
201typedef void (*uniot_log_sink_t)(const char *msg);
202
211
219
234static inline int
235uniot_log_printf(PGM_P format, ...) {
236 va_list arg;
237 va_start(arg, format);
238 char buf[UNIOT_LOG_BUF_SIZE];
239 int len = vsnprintf_P(buf, sizeof(buf), format, arg);
240 va_end(arg);
241 UNIOT_LOG_PRINT(buf);
243 if (len >= (int)sizeof(buf))
244 UNIOT_LOG_PRINT(" [...]\n");
245 return len;
246}
247
248#else /* UNIOT_LOG_ENABLED not defined */
255
256#define UNIOT_LOG_SET_READY() do {} while(0)
257#define UNIOT_LOG_PRINT(...) (UNUSED(__VA_ARGS__))
258#define UNIOT_LOG(log_type, ...) (UNUSED(__VA_ARGS__))
259#define UNIOT_LOG_IF(log_type, ...) (UNUSED(__VA_ARGS__))
260
261// Sink stubs: allow sink-related code to compile when logging is disabled.
262typedef void (*uniot_log_sink_t)(const char *);
264#endif
265
266#if UNIOT_LOG_LEVEL_ERROR <= UNIOT_LOG_LEVEL
273#define UNIOT_LOG_ERROR(...) UNIOT_LOG(ERROR, __VA_ARGS__)
281#define UNIOT_LOG_ERROR_IF(log_cond, log...) UNIOT_LOG_IF(ERROR, log_cond, ##log)
282#else
283#define UNIOT_LOG_ERROR(...) (UNUSED(__VA_ARGS__))
284#define UNIOT_LOG_ERROR_IF(...) (UNUSED(__VA_ARGS__))
285#endif
286
287#if UNIOT_LOG_LEVEL_WARN <= UNIOT_LOG_LEVEL
294#define UNIOT_LOG_WARN(...) UNIOT_LOG(WARN, __VA_ARGS__)
302#define UNIOT_LOG_WARN_IF(log_cond, log...) UNIOT_LOG_IF(WARN, log_cond, ##log)
303#else
304#define UNIOT_LOG_WARN(...) (UNUSED(__VA_ARGS__))
305#define UNIOT_LOG_WARN_IF(...) (UNUSED(__VA_ARGS__))
306#endif
307
308#if UNIOT_LOG_LEVEL_INFO <= UNIOT_LOG_LEVEL
315#define UNIOT_LOG_INFO(...) UNIOT_LOG(INFO, __VA_ARGS__)
323#define UNIOT_LOG_INFO_IF(log_cond, log...) UNIOT_LOG_IF(INFO, log_cond, ##log)
324#else
329#define UNIOT_LOG_INFO(...) (UNUSED(__VA_ARGS__))
330#define UNIOT_LOG_INFO_IF(...) (UNUSED(__VA_ARGS__))
331#endif
332
333#if UNIOT_LOG_LEVEL_DEBUG <= UNIOT_LOG_LEVEL
340#define UNIOT_LOG_DEBUG(...) UNIOT_LOG(DEBUG, __VA_ARGS__)
348#define UNIOT_LOG_DEBUG_IF(log_cond, log...) UNIOT_LOG_IF(DEBUG, log_cond, ##log)
349#else
350#define UNIOT_LOG_DEBUG(...) (UNUSED(__VA_ARGS__))
351#define UNIOT_LOG_DEBUG_IF(...) (UNUSED(__VA_ARGS__))
352#endif
353
354#if UNIOT_LOG_LEVEL_TRACE <= UNIOT_LOG_LEVEL
361#define UNIOT_LOG_TRACE(...) UNIOT_LOG(TRACE, __VA_ARGS__)
369#define UNIOT_LOG_TRACE_IF(log_cond, log...) UNIOT_LOG_IF(TRACE, log_cond, ##log)
370#else
371#define UNIOT_LOG_TRACE(...) (UNUSED(__VA_ARGS__))
372#define UNIOT_LOG_TRACE_IF(...) (UNUSED(__VA_ARGS__))
373#endif
374
uniot_log_sink_t sUniotLogSink
Global log sink (C++17 inline variable, one copy per program).
Definition Logger.h:210
void uniot_log_set_sink(uniot_log_sink_t sink)
Register an external log sink.
Definition Logger.h:218
#define UNIOT_LOG_BUF_SIZE
Maximum size of the logging buffer.
Definition Logger.h:117
#define UNIOT_LOG_PRINT(...)
Print to the logging stream.
Definition Logger.h:143
void(* uniot_log_sink_t)(const char *msg)
Log sink function type.
Definition Logger.h:201