Uniot Core
0.8.1
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#if UNIOT_LOG_ENABLED
83
84#include <Arduino.h>
85
91#ifndef UNIOT_LOG_STREAM
92#define UNIOT_LOG_STREAM Serial
93#endif
94
101#ifndef UNIOT_LOG_BAUD_RATE
102#define UNIOT_LOG_BAUD_RATE 115200
103#endif
104
111#ifndef UNIOT_LOG_BUF_SIZE
112#define UNIOT_LOG_BUF_SIZE 256
113#endif
114
121#ifndef UNIOT_LOG_SET_READY
122#define UNIOT_LOG_SET_READY() \
123 do { \
124 if (!UNIOT_LOG_STREAM) { \
125 UNIOT_LOG_STREAM.begin(UNIOT_LOG_BAUD_RATE); \
126 UNIOT_LOG_STREAM.print("\n\n"); \
127 } \
128 } while (0)
129#endif
130
137#ifndef UNIOT_LOG_PRINT
138#define UNIOT_LOG_PRINT(...) \
139 do { \
140 if (UNIOT_LOG_STREAM) \
141 UNIOT_LOG_STREAM.print(__VA_ARGS__); \
142 } while (0)
143#endif
144
159#define UNIOT_LOG(log_type, log_fmt, log_arg...) \
160 do { \
161 uniot_log_printf("[" #log_type "][%lu][%s:%d][%s] " log_fmt "\n", \
162 millis(), __FILE__, __LINE__, __FUNC_NAME__, ##log_arg); \
163 } while (0)
164
175#define UNIOT_LOG_IF(log_type, log_cond, log_fmt, log_arg...) \
176 do { \
177 if (log_cond) \
178 UNIOT_LOG(log_type, log_fmt, ##log_arg); \
179 } while (0)
180
191static inline int
192uniot_log_printf(const char *format, ...) {
193 va_list arg;
194 va_start(arg, format);
195 char buf[UNIOT_LOG_BUF_SIZE];
196 int len = vsnprintf(buf, sizeof(buf), format, arg);
197 va_end(arg);
198 UNIOT_LOG_PRINT(buf);
199 if (len >= (int)sizeof(buf))
200 UNIOT_LOG_PRINT(" [...]\n");
201 return len;
202}
203
204#else /* UNIOT_LOG_ENABLED not defined */
211#include <Common.h>
212
213#define UNIOT_LOG_SET_READY() do {} while(0)
214#define UNIOT_LOG_PRINT(...) (UNUSED(__VA_ARGS__))
215#define UNIOT_LOG(log_type, ...) (UNUSED(__VA_ARGS__))
216#define UNIOT_LOG_IF(log_type, ...) (UNUSED(__VA_ARGS__))
217#endif
218
219#if UNIOT_LOG_LEVEL_ERROR <= UNIOT_LOG_LEVEL
226#define UNIOT_LOG_ERROR(...) UNIOT_LOG(ERROR, __VA_ARGS__)
234#define UNIOT_LOG_ERROR_IF(log_cond, log...) UNIOT_LOG_IF(ERROR, log_cond, ##log)
235#else
236#define UNIOT_LOG_ERROR(...) (UNUSED(__VA_ARGS__))
237#define UNIOT_LOG_ERROR_IF(...) (UNUSED(__VA_ARGS__))
238#endif
239
240#if UNIOT_LOG_LEVEL_WARN <= UNIOT_LOG_LEVEL
247#define UNIOT_LOG_WARN(...) UNIOT_LOG(WARN, __VA_ARGS__)
255#define UNIOT_LOG_WARN_IF(log_cond, log...) UNIOT_LOG_IF(WARN, log_cond, ##log)
256#else
257#define UNIOT_LOG_WARN(...) (UNUSED(__VA_ARGS__))
258#define UNIOT_LOG_WARN_IF(...) (UNUSED(__VA_ARGS__))
259#endif
260
261#if UNIOT_LOG_LEVEL_INFO <= UNIOT_LOG_LEVEL
268#define UNIOT_LOG_INFO(...) UNIOT_LOG(INFO, __VA_ARGS__)
276#define UNIOT_LOG_INFO_IF(log_cond, log...) UNIOT_LOG_IF(INFO, log_cond, ##log)
277#else
282#define UNIOT_LOG_INFO(...) (UNUSED(__VA_ARGS__))
283#define UNIOT_LOG_INFO_IF(...) (UNUSED(__VA_ARGS__))
284#endif
285
286#if UNIOT_LOG_LEVEL_DEBUG <= UNIOT_LOG_LEVEL
293#define UNIOT_LOG_DEBUG(...) UNIOT_LOG(DEBUG, __VA_ARGS__)
301#define UNIOT_LOG_DEBUG_IF(log_cond, log...) UNIOT_LOG_IF(DEBUG, log_cond, ##log)
302#else
303#define UNIOT_LOG_DEBUG(...) (UNUSED(__VA_ARGS__))
304#define UNIOT_LOG_DEBUG_IF(...) (UNUSED(__VA_ARGS__))
305#endif
306
307#if UNIOT_LOG_LEVEL_TRACE <= UNIOT_LOG_LEVEL
314#define UNIOT_LOG_TRACE(...) UNIOT_LOG(TRACE, __VA_ARGS__)
322#define UNIOT_LOG_TRACE_IF(log_cond, log...) UNIOT_LOG_IF(TRACE, log_cond, ##log)
323#else
324#define UNIOT_LOG_TRACE(...) (UNUSED(__VA_ARGS__))
325#define UNIOT_LOG_TRACE_IF(...) (UNUSED(__VA_ARGS__))
326#endif
327
#define UNIOT_LOG_BUF_SIZE
Maximum size of the logging buffer.
Definition Logger.h:112
#define UNIOT_LOG_PRINT(...)
Print to the logging stream.
Definition Logger.h:138