Contiki-NG
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 /* Per-module log level */
69 
70 struct log_module {
71  const char *name;
72  int *curr_log_level;
73  int max_log_level;
74 };
75 
76 extern int curr_log_level_rpl;
77 extern int curr_log_level_tcpip;
78 extern int curr_log_level_ipv6;
79 extern int curr_log_level_6lowpan;
80 extern int curr_log_level_nullnet;
81 extern int curr_log_level_mac;
82 extern int curr_log_level_framer;
83 extern int curr_log_level_6top;
84 extern int curr_log_level_coap;
85 extern int curr_log_level_snmp;
86 extern int curr_log_level_lwm2m;
87 extern int curr_log_level_main;
88 
89 extern struct log_module all_modules[];
90 
91 #define LOG_LEVEL_RPL MIN((LOG_CONF_LEVEL_RPL), curr_log_level_rpl)
92 #define LOG_LEVEL_TCPIP MIN((LOG_CONF_LEVEL_TCPIP), curr_log_level_tcpip)
93 #define LOG_LEVEL_IPV6 MIN((LOG_CONF_LEVEL_IPV6), curr_log_level_ipv6)
94 #define LOG_LEVEL_6LOWPAN MIN((LOG_CONF_LEVEL_6LOWPAN), curr_log_level_6lowpan)
95 #define LOG_LEVEL_NULLNET MIN((LOG_CONF_LEVEL_NULLNET), curr_log_level_nullnet)
96 #define LOG_LEVEL_MAC MIN((LOG_CONF_LEVEL_MAC), curr_log_level_mac)
97 #define LOG_LEVEL_FRAMER MIN((LOG_CONF_LEVEL_FRAMER), curr_log_level_framer)
98 #define LOG_LEVEL_6TOP MIN((LOG_CONF_LEVEL_6TOP), curr_log_level_6top)
99 #define LOG_LEVEL_COAP MIN((LOG_CONF_LEVEL_COAP), curr_log_level_coap)
100 #define LOG_LEVEL_SNMP MIN((LOG_CONF_LEVEL_SNMP), curr_log_level_snmp)
101 #define LOG_LEVEL_LWM2M MIN((LOG_CONF_LEVEL_LWM2M), curr_log_level_lwm2m)
102 #define LOG_LEVEL_MAIN MIN((LOG_CONF_LEVEL_MAIN), curr_log_level_main)
103 
104 /* Main log function */
105 
106 #define LOG(newline, level, levelstr, ...) do { \
107  if(level <= (LOG_LEVEL)) { \
108  if(newline) { \
109  if(LOG_WITH_MODULE_PREFIX) { \
110  LOG_OUTPUT_PREFIX(level, levelstr, LOG_MODULE); \
111  } \
112  if(LOG_WITH_LOC) { \
113  LOG_OUTPUT("[%s: %d] ", __FILE__, __LINE__); \
114  } \
115  } \
116  LOG_OUTPUT(__VA_ARGS__); \
117  } \
118  } while (0)
119 
120 /* For Cooja annotations */
121 #define LOG_ANNOTATE(...) do { \
122  if(LOG_WITH_ANNOTATE) { \
123  LOG_OUTPUT(__VA_ARGS__); \
124  } \
125  } while (0)
126 
127 /* Link-layer address */
128 #define LOG_LLADDR(level, lladdr) do { \
129  if(level <= (LOG_LEVEL)) { \
130  if(LOG_WITH_COMPACT_ADDR) { \
131  log_lladdr_compact(lladdr); \
132  } else { \
133  log_lladdr(lladdr); \
134  } \
135  } \
136  } while (0)
137 
138 /* IPv6 address */
139 #define LOG_6ADDR(level, ipaddr) do { \
140  if(level <= (LOG_LEVEL)) { \
141  if(LOG_WITH_COMPACT_ADDR) { \
142  log_6addr_compact(ipaddr); \
143  } else { \
144  log_6addr(ipaddr); \
145  } \
146  } \
147  } while (0)
148 
149 /* More compact versions of LOG macros */
150 #define LOG_PRINT(...) LOG(1, 0, "PRI", __VA_ARGS__)
151 #define LOG_ERR(...) LOG(1, LOG_LEVEL_ERR, "ERR", __VA_ARGS__)
152 #define LOG_WARN(...) LOG(1, LOG_LEVEL_WARN, "WARN", __VA_ARGS__)
153 #define LOG_INFO(...) LOG(1, LOG_LEVEL_INFO, "INFO", __VA_ARGS__)
154 #define LOG_DBG(...) LOG(1, LOG_LEVEL_DBG, "DBG", __VA_ARGS__)
155 
156 #define LOG_PRINT_(...) LOG(0, 0, "PRI", __VA_ARGS__)
157 #define LOG_ERR_(...) LOG(0, LOG_LEVEL_ERR, "ERR", __VA_ARGS__)
158 #define LOG_WARN_(...) LOG(0, LOG_LEVEL_WARN, "WARN", __VA_ARGS__)
159 #define LOG_INFO_(...) LOG(0, LOG_LEVEL_INFO, "INFO", __VA_ARGS__)
160 #define LOG_DBG_(...) LOG(0, LOG_LEVEL_DBG, "DBG", __VA_ARGS__)
161 
162 #define LOG_PRINT_LLADDR(...) LOG_LLADDR(0, __VA_ARGS__)
163 #define LOG_ERR_LLADDR(...) LOG_LLADDR(LOG_LEVEL_ERR, __VA_ARGS__)
164 #define LOG_WARN_LLADDR(...) LOG_LLADDR(LOG_LEVEL_WARN, __VA_ARGS__)
165 #define LOG_INFO_LLADDR(...) LOG_LLADDR(LOG_LEVEL_INFO, __VA_ARGS__)
166 #define LOG_DBG_LLADDR(...) LOG_LLADDR(LOG_LEVEL_DBG, __VA_ARGS__)
167 
168 #define LOG_PRINT_6ADDR(...) LOG_6ADDR(0, __VA_ARGS__)
169 #define LOG_ERR_6ADDR(...) LOG_6ADDR(LOG_LEVEL_ERR, __VA_ARGS__)
170 #define LOG_WARN_6ADDR(...) LOG_6ADDR(LOG_LEVEL_WARN, __VA_ARGS__)
171 #define LOG_INFO_6ADDR(...) LOG_6ADDR(LOG_LEVEL_INFO, __VA_ARGS__)
172 #define LOG_DBG_6ADDR(...) LOG_6ADDR(LOG_LEVEL_DBG, __VA_ARGS__)
173 
174 /* For checking log level.
175  As this builds on curr_log_level variables, this should not be used
176  in pre-processor macros. Use in a C 'if' statement instead, e.g.:
177  if(LOG_INFO_ENABLED) { ... }
178  Note that most compilers will still be able to strip the code out
179  for low enough log levels configurations. */
180 #define LOG_ERR_ENABLED ((LOG_LEVEL) >= LOG_LEVEL_ERR)
181 #define LOG_WARN_ENABLED ((LOG_LEVEL) >= LOG_LEVEL_WARN)
182 #define LOG_INFO_ENABLED ((LOG_LEVEL) >= LOG_LEVEL_INFO)
183 #define LOG_DBG_ENABLED ((LOG_LEVEL) >= LOG_LEVEL_DBG)
184 
185 #if NETSTACK_CONF_WITH_IPV6
186 
187 /**
188  * Logs an IPv6 address
189  * \param ipaddr The IPv6 address
190 */
191 void log_6addr(const uip_ipaddr_t *ipaddr);
192 
193 /**
194  * Logs an IPv6 address with a compact format
195  * \param ipaddr The IPv6 address
196 */
197 void log_6addr_compact(const uip_ipaddr_t *ipaddr);
198 
199 /**
200  * Write at most size - 1 characters of the IP address to the output string,
201  * in a compact representation. The output is always null-terminated, unless
202  * size is 0.
203  *
204  * \param buf A pointer to an output string with at least size bytes.
205  * \param size The max number of characters to write to the output string.
206  * \param ipaddr A pointer to a uip_ipaddr_t that will be printed with printf().
207  */
208 int log_6addr_compact_snprint(char *buf, size_t size, const uip_ipaddr_t *ipaddr);
209 
210 #endif /* NETSTACK_CONF_WITH_IPV6 */
211 
212 /**
213  * Logs a link-layer address
214  * \param lladdr The link-layer address
215 */
216 void log_lladdr(const linkaddr_t *lladdr);
217 
218 /**
219  * Logs a link-layer address with a compact format
220  * \param lladdr The link-layer address
221 */
222 void log_lladdr_compact(const linkaddr_t *lladdr);
223 
224 /**
225  * Sets a log level at run-time. Logs are included in the firmware via
226  * the compile-time flags in log-conf.h, but this allows to force lower log
227  * levels, system-wide.
228  * \param module The target module string descriptor
229  * \param level The log level
230 */
231 void log_set_level(const char *module, int level);
232 
233 /**
234  * Returns the current log level.
235  * \param module The target module string descriptor
236  * \return The current log level
237 */
238 int log_get_level(const char *module);
239 
240 /**
241  * Returns a textual description of a log level
242  * \param level log level
243  * \return The textual description
244 */
245 const char *log_level_to_str(int level);
246 
247 #endif /* __LOG_H__ */
248 
249 /** @} */
250 /** @} */
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:116
Default log levels for a number of modules
Header file for the link-layer address representation
void log_lladdr_compact(const linkaddr_t *lladdr)
Logs a link-layer address with a compact format.
Definition: log.c:145
void log_set_level(const char *module, int level)
Sets a log level at run-time.
Definition: log.c:163
void log_lladdr(const linkaddr_t *lladdr)
Logs a link-layer address.
Definition: log.c:128
void log_6addr(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address.
Definition: log.c:89
Header file for the uIP TCP/IP stack.
const char * log_level_to_str(int level)
Returns a textual description of a log level.
Definition: log.c:194
int log_get_level(const char *module)
Returns the current log level.
Definition: log.c:178
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:97
void log_6addr_compact(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address with a compact format.
Definition: log.c:119