Contiki-NG
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, Inria.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \file
35 * Header file for the logging system
36 * \author
37 * Simon Duquennoy <simon.duquennoy@inria.fr>
38 */
39
40/** \addtogroup sys
41 * @{ */
42
43/**
44 * \defgroup log Per-module, per-level logging
45 * @{
46 *
47 * The log module performs per-module, per-level logging
48 *
49 */
50
51#ifndef LOG_H_
52#define LOG_H_
53
54#include <stdio.h>
55#include "net/linkaddr.h"
56#include "sys/log-conf.h"
57#if NETSTACK_CONF_WITH_IPV6
58#include "net/ipv6/uip.h"
59#endif /* NETSTACK_CONF_WITH_IPV6 */
60
61/* The different log levels available */
62#define LOG_LEVEL_NONE 0 /* No log */
63#define LOG_LEVEL_ERR 1 /* Errors */
64#define LOG_LEVEL_WARN 2 /* Warnings */
65#define LOG_LEVEL_INFO 3 /* Basic info */
66#define LOG_LEVEL_DBG 4 /* Detailled debug */
67
68/* Log coloring */
69#define TC_RESET "\033[0m"
70#define TC_BLACK "\033[0;30m"
71#define TC_RED "\033[0;31m"
72#define TC_GREEN "\033[0;32m"
73#define TC_YELLOW "\033[0;33m"
74#define TC_BLUE "\033[0;34m"
75#define TC_MAGENTA "\033[0;35m"
76#define TC_CYAN "\033[0;36m"
77#define TC_WHITE "\033[0;37m"
78
79#define LOG_COLOR_RESET TC_RESET
80
81#ifndef LOG_COLOR_ERR
82#define LOG_COLOR_ERR TC_RED
83#endif
84
85#ifndef LOG_COLOR_WARN
86#define LOG_COLOR_WARN TC_YELLOW
87#endif
88
89#ifndef LOG_COLOR_INFO
90#define LOG_COLOR_INFO TC_BLUE
91#endif
92
93#ifndef LOG_COLOR_DBG
94#define LOG_COLOR_DBG TC_WHITE
95#endif
96
97#ifndef LOG_COLOR_PRI
98#define LOG_COLOR_PRI TC_CYAN
99#endif
100
101/* Per-module log level */
102
103struct log_module {
104 const char *name;
105 int *curr_log_level;
106 int max_log_level;
107};
108
109extern int curr_log_level_rpl;
110extern int curr_log_level_tcpip;
111extern int curr_log_level_ipv6;
112extern int curr_log_level_6lowpan;
113extern int curr_log_level_nullnet;
114extern int curr_log_level_mac;
115extern int curr_log_level_framer;
116extern int curr_log_level_6top;
117extern int curr_log_level_coap;
118extern int curr_log_level_dtls;
119extern int curr_log_level_snmp;
120extern int curr_log_level_lwm2m;
121extern int curr_log_level_sys;
122extern int curr_log_level_main;
123extern int curr_log_level_edhoc;
124
125extern struct log_module all_modules[];
126
127#define LOG_LEVEL_RPL MIN((LOG_CONF_LEVEL_RPL), curr_log_level_rpl)
128#define LOG_LEVEL_TCPIP MIN((LOG_CONF_LEVEL_TCPIP), curr_log_level_tcpip)
129#define LOG_LEVEL_IPV6 MIN((LOG_CONF_LEVEL_IPV6), curr_log_level_ipv6)
130#define LOG_LEVEL_6LOWPAN MIN((LOG_CONF_LEVEL_6LOWPAN), curr_log_level_6lowpan)
131#define LOG_LEVEL_NULLNET MIN((LOG_CONF_LEVEL_NULLNET), curr_log_level_nullnet)
132#define LOG_LEVEL_MAC MIN((LOG_CONF_LEVEL_MAC), curr_log_level_mac)
133#define LOG_LEVEL_FRAMER MIN((LOG_CONF_LEVEL_FRAMER), curr_log_level_framer)
134#define LOG_LEVEL_6TOP MIN((LOG_CONF_LEVEL_6TOP), curr_log_level_6top)
135#define LOG_LEVEL_COAP MIN((LOG_CONF_LEVEL_COAP), curr_log_level_coap)
136#define LOG_LEVEL_DTLS MIN((LOG_CONF_LEVEL_DTLS), curr_log_level_dtls)
137#define LOG_LEVEL_SNMP MIN((LOG_CONF_LEVEL_SNMP), curr_log_level_snmp)
138#define LOG_LEVEL_LWM2M MIN((LOG_CONF_LEVEL_LWM2M), curr_log_level_lwm2m)
139#define LOG_LEVEL_SYS MIN((LOG_CONF_LEVEL_SYS), curr_log_level_sys)
140#define LOG_LEVEL_MAIN MIN((LOG_CONF_LEVEL_MAIN), curr_log_level_main)
141#define LOG_LEVEL_EDHOC MIN((LOG_CONF_LEVEL_EDHOC), curr_log_level_edhoc)
142
143/* Main log function */
144
145#define LOG(newline, level, levelstr, levelcolor, ...) do { \
146 if(level <= (LOG_LEVEL)) { \
147 if(newline) { \
148 if(LOG_WITH_COLOR) { \
149 LOG_OUTPUT(levelcolor); \
150 } \
151 if(LOG_WITH_MODULE_PREFIX) { \
152 LOG_OUTPUT_PREFIX(level, levelstr, LOG_MODULE); \
153 } \
154 if(LOG_WITH_LOC) { \
155 LOG_OUTPUT("[%s: %d] ", __FILE__, __LINE__); \
156 } \
157 if(LOG_WITH_COLOR) { \
158 LOG_OUTPUT(LOG_COLOR_RESET); \
159 } \
160 } \
161 LOG_OUTPUT(__VA_ARGS__); \
162 } \
163 } while (0)
164
165/* For Cooja annotations */
166#define LOG_ANNOTATE(...) do { \
167 if(LOG_WITH_ANNOTATE) { \
168 LOG_OUTPUT(__VA_ARGS__); \
169 } \
170 } while (0)
171
172/* Link-layer address */
173#define LOG_LLADDR(level, lladdr) do { \
174 if(level <= (LOG_LEVEL)) { \
175 if(LOG_WITH_COMPACT_ADDR) { \
176 log_lladdr_compact(lladdr); \
177 } else { \
178 log_lladdr(lladdr); \
179 } \
180 } \
181 } while (0)
182
183/* IPv6 address */
184#define LOG_6ADDR(level, ipaddr) do { \
185 if(level <= (LOG_LEVEL)) { \
186 if(LOG_WITH_COMPACT_ADDR) { \
187 log_6addr_compact(ipaddr); \
188 } else { \
189 log_6addr(ipaddr); \
190 } \
191 } \
192 } while (0)
193
194#define LOG_BYTES(level, data, length) do { \
195 if(level <= (LOG_LEVEL)) { \
196 log_bytes(data, length); \
197 } \
198 } while (0)
199
200#define LOG_STRING(level, data, length) do { \
201 if(level <= (LOG_LEVEL)) { \
202 log_string(data, length); \
203 } \
204 } while (0)
205
206/* More compact versions of LOG macros */
207#define LOG_PRINT(...) LOG(1, 0, "PRI", LOG_COLOR_PRI, __VA_ARGS__)
208#define LOG_ERR(...) LOG(1, LOG_LEVEL_ERR, "ERR", LOG_COLOR_ERR, __VA_ARGS__)
209#define LOG_WARN(...) LOG(1, LOG_LEVEL_WARN, "WARN", LOG_COLOR_WARN, __VA_ARGS__)
210#define LOG_INFO(...) LOG(1, LOG_LEVEL_INFO, "INFO", LOG_COLOR_INFO, __VA_ARGS__)
211#define LOG_DBG(...) LOG(1, LOG_LEVEL_DBG, "DBG", LOG_COLOR_DBG, __VA_ARGS__)
212
213#define LOG_PRINT_(...) LOG(0, 0, "PRI", LOG_COLOR_PRI, __VA_ARGS__)
214#define LOG_ERR_(...) LOG(0, LOG_LEVEL_ERR, "ERR", LOG_COLOR_ERR, __VA_ARGS__)
215#define LOG_WARN_(...) LOG(0, LOG_LEVEL_WARN, "WARN", LOG_COLOR_WARN, __VA_ARGS__)
216#define LOG_INFO_(...) LOG(0, LOG_LEVEL_INFO, "INFO", LOG_COLOR_INFO, __VA_ARGS__)
217#define LOG_DBG_(...) LOG(0, LOG_LEVEL_DBG, "DBG", LOG_COLOR_DBG, __VA_ARGS__)
218
219#define LOG_PRINT_LLADDR(...) LOG_LLADDR(0, __VA_ARGS__)
220#define LOG_ERR_LLADDR(...) LOG_LLADDR(LOG_LEVEL_ERR, __VA_ARGS__)
221#define LOG_WARN_LLADDR(...) LOG_LLADDR(LOG_LEVEL_WARN, __VA_ARGS__)
222#define LOG_INFO_LLADDR(...) LOG_LLADDR(LOG_LEVEL_INFO, __VA_ARGS__)
223#define LOG_DBG_LLADDR(...) LOG_LLADDR(LOG_LEVEL_DBG, __VA_ARGS__)
224
225#define LOG_PRINT_6ADDR(...) LOG_6ADDR(0, __VA_ARGS__)
226#define LOG_ERR_6ADDR(...) LOG_6ADDR(LOG_LEVEL_ERR, __VA_ARGS__)
227#define LOG_WARN_6ADDR(...) LOG_6ADDR(LOG_LEVEL_WARN, __VA_ARGS__)
228#define LOG_INFO_6ADDR(...) LOG_6ADDR(LOG_LEVEL_INFO, __VA_ARGS__)
229#define LOG_DBG_6ADDR(...) LOG_6ADDR(LOG_LEVEL_DBG, __VA_ARGS__)
230
231#define LOG_PRINT_BYTES(data, length) LOG_BYTES(0, data, length)
232#define LOG_ERR_BYTES(data, length) LOG_BYTES(LOG_LEVEL_ERR, data, length)
233#define LOG_WARN_BYTES(data, length) LOG_BYTES(LOG_LEVEL_WARN, data, length)
234#define LOG_INFO_BYTES(data, length) LOG_BYTES(LOG_LEVEL_INFO, data, length)
235#define LOG_DBG_BYTES(data, length) LOG_BYTES(LOG_LEVEL_DBG, data, length)
236
237#define LOG_PRINT_STRING(data, length) LOG_STRING(0, data, length)
238#define LOG_ERR_STRING(data, length) LOG_STRING(LOG_LEVEL_ERR, data, length)
239#define LOG_WARN_STRING(data, length) LOG_STRING(LOG_LEVEL_WARN, data, length)
240#define LOG_INFO_STRING(data, length) LOG_STRING(LOG_LEVEL_INFO, data, length)
241#define LOG_DBG_STRING(data, length) LOG_STRING(LOG_LEVEL_DBG, data, length)
242
243/* For checking log level.
244 As this builds on curr_log_level variables, this should not be used
245 in pre-processor macros. Use in a C 'if' statement instead, e.g.:
246 if(LOG_INFO_ENABLED) { ... }
247 Note that most compilers will still be able to strip the code out
248 for low enough log levels configurations. */
249#define LOG_ERR_ENABLED ((LOG_LEVEL) >= LOG_LEVEL_ERR)
250#define LOG_WARN_ENABLED ((LOG_LEVEL) >= LOG_LEVEL_WARN)
251#define LOG_INFO_ENABLED ((LOG_LEVEL) >= LOG_LEVEL_INFO)
252#define LOG_DBG_ENABLED ((LOG_LEVEL) >= LOG_LEVEL_DBG)
253
254#if NETSTACK_CONF_WITH_IPV6
255
256/**
257 * Logs an IPv6 address
258 * \param ipaddr The IPv6 address
259*/
260void log_6addr(const uip_ipaddr_t *ipaddr);
261
262/**
263 * Logs an IPv6 address with a compact format
264 * \param ipaddr The IPv6 address
265*/
266void log_6addr_compact(const uip_ipaddr_t *ipaddr);
267
268/**
269 * Write at most size - 1 characters of the IP address to the output string,
270 * in a compact representation. The output is always null-terminated, unless
271 * size is 0.
272 *
273 * \param buf A pointer to an output string with at least size bytes.
274 * \param size The max number of characters to write to the output string.
275 * \param ipaddr A pointer to a uip_ipaddr_t that will be printed with printf().
276 */
277int log_6addr_compact_snprint(char *buf, size_t size, const uip_ipaddr_t *ipaddr);
278
279#endif /* NETSTACK_CONF_WITH_IPV6 */
280
281/**
282 * Logs a link-layer address
283 * \param lladdr The link-layer address
284*/
285void log_lladdr(const linkaddr_t *lladdr);
286
287/**
288 * Logs a link-layer address with a compact format
289 * \param lladdr The link-layer address
290*/
291void log_lladdr_compact(const linkaddr_t *lladdr);
292
293/**
294 * Logs a byte array as hex characters
295 * \param data The byte array
296 * \param length The length of the byte array
297*/
298void log_bytes(const void *data, size_t length);
299
300/**
301 * Logs a string that has a length specified, but might not be zero-terminated.
302 * \param text The string
303 * \param length The max length of the string
304*/
305void log_string(const char *text, size_t length);
306
307/**
308 * Sets a log level at run-time. Logs are included in the firmware via
309 * the compile-time flags in log-conf.h, but this allows to force lower log
310 * levels, system-wide.
311 * \param module The target module string descriptor
312 * \param level The log level
313*/
314void log_set_level(const char *module, int level);
315
316/**
317 * Returns the current log level.
318 * \param module The target module string descriptor
319 * \return The current log level
320*/
321int log_get_level(const char *module);
322
323/**
324 * Returns a textual description of a log level
325 * \param level log level
326 * \return The textual description
327*/
328const char *log_level_to_str(int level);
329
330#endif /* LOG_H_ */
331
332/** @} */
333/** @} */
void log_set_level(const char *module, int level)
Sets a log level at run-time.
Definition log.c:195
int log_6addr_compact_snprint(char *buf, size_t size, const uip_ipaddr_t *ipaddr)
Write at most size - 1 characters of the IP address to the output string, in a compact representation...
Definition log.c:103
void log_6addr_compact(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address with a compact format.
Definition log.c:125
void log_lladdr(const linkaddr_t *lladdr)
Logs a link-layer address.
Definition log.c:134
void log_string(const char *text, size_t len)
Logs a string that has a length specified, but might not be zero-terminated.
Definition log.c:182
const char * log_level_to_str(int level)
Returns a textual description of a log level.
Definition log.c:226
void log_lladdr_compact(const linkaddr_t *lladdr)
Logs a link-layer address with a compact format.
Definition log.c:151
void log_6addr(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address.
Definition log.c:95
int log_get_level(const char *module)
Returns the current log level.
Definition log.c:210
void log_bytes(const void *data, size_t length)
Logs a byte array as hex characters.
Definition log.c:169
Header file for the link-layer address representation.
Default log levels for a number of modules.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition uip-nd6.c:116
Header file for the uIP TCP/IP stack.