Contiki-NG
Loading...
Searching...
No Matches
log.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, RISE SICS.
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@ri.se>
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#include "sys/log.h"
52#include "net/ipv6/ip64-addr.h"
53#include "net/ipv6/uiplib.h"
55
56int curr_log_level_rpl = LOG_CONF_LEVEL_RPL;
57int curr_log_level_tcpip = LOG_CONF_LEVEL_TCPIP;
58int curr_log_level_ipv6 = LOG_CONF_LEVEL_IPV6;
59int curr_log_level_6lowpan = LOG_CONF_LEVEL_6LOWPAN;
60int curr_log_level_nullnet = LOG_CONF_LEVEL_NULLNET;
61int curr_log_level_mac = LOG_CONF_LEVEL_MAC;
62int curr_log_level_framer = LOG_CONF_LEVEL_FRAMER;
63int curr_log_level_6top = LOG_CONF_LEVEL_6TOP;
64int curr_log_level_coap = LOG_CONF_LEVEL_COAP;
65int curr_log_level_dtls = LOG_CONF_LEVEL_DTLS;
66int curr_log_level_snmp = LOG_CONF_LEVEL_SNMP;
67int curr_log_level_lwm2m = LOG_CONF_LEVEL_LWM2M;
68int curr_log_level_sys = LOG_CONF_LEVEL_SYS;
69int curr_log_level_main = LOG_CONF_LEVEL_MAIN;
70
71struct log_module all_modules[] = {
72 {"rpl", &curr_log_level_rpl, LOG_CONF_LEVEL_RPL},
73 {"tcpip", &curr_log_level_tcpip, LOG_CONF_LEVEL_TCPIP},
74 {"ipv6", &curr_log_level_ipv6, LOG_CONF_LEVEL_IPV6},
75 {"6lowpan", &curr_log_level_6lowpan, LOG_CONF_LEVEL_6LOWPAN},
76 {"nullnet", &curr_log_level_nullnet, LOG_CONF_LEVEL_NULLNET},
77 {"mac", &curr_log_level_mac, LOG_CONF_LEVEL_MAC},
78 {"framer", &curr_log_level_framer, LOG_CONF_LEVEL_FRAMER},
79 {"6top", &curr_log_level_6top, LOG_CONF_LEVEL_6TOP},
80 {"coap", &curr_log_level_coap, LOG_CONF_LEVEL_COAP},
81 {"dtls", &curr_log_level_coap, LOG_CONF_LEVEL_DTLS},
82 {"snmp", &curr_log_level_snmp, LOG_CONF_LEVEL_SNMP},
83 {"lwm2m", &curr_log_level_lwm2m, LOG_CONF_LEVEL_LWM2M},
84 {"sys", &curr_log_level_sys, LOG_CONF_LEVEL_SYS},
85 {"main", &curr_log_level_main, LOG_CONF_LEVEL_MAIN},
86 {NULL, NULL, 0},
87};
88
89#if NETSTACK_CONF_WITH_IPV6
90
91/*---------------------------------------------------------------------------*/
92void
93log_6addr(const uip_ipaddr_t *ipaddr)
94{
95 char buf[UIPLIB_IPV6_MAX_STR_LEN];
96 uiplib_ipaddr_snprint(buf, sizeof(buf), ipaddr);
97 LOG_OUTPUT("%s", buf);
98}
99/*---------------------------------------------------------------------------*/
100int
101log_6addr_compact_snprint(char *buf, size_t size, const uip_ipaddr_t *ipaddr)
102{
103 if(ipaddr == NULL) {
104 return snprintf(buf, size, "6A-NULL");
105 } else {
106 char *prefix = NULL;
108 prefix = "6M";
109 } else if(uip_is_addr_linklocal(ipaddr)) {
110 prefix = "6L";
111 } else {
112 prefix = "6G";
113 }
114#if BUILD_WITH_DEPLOYMENT
115 return snprintf(buf, size, "%s-%03u", prefix, deployment_id_from_iid(ipaddr));
116#else /* BUILD_WITH_DEPLOYMENT */
117 return snprintf(buf, size, "%s-%04x", prefix, UIP_HTONS(ipaddr->u16[sizeof(uip_ipaddr_t)/2-1]));
118#endif /* BUILD_WITH_DEPLOYMENT */
119 }
120}
121/*---------------------------------------------------------------------------*/
122void
123log_6addr_compact(const uip_ipaddr_t *ipaddr)
124{
125 char buf[8];
126 log_6addr_compact_snprint(buf, sizeof(buf), ipaddr);
127 LOG_OUTPUT("%s", buf);
128}
129#endif /* NETSTACK_CONF_WITH_IPV6 */
130/*---------------------------------------------------------------------------*/
131void
132log_lladdr(const linkaddr_t *lladdr)
133{
134 if(lladdr == NULL) {
135 LOG_OUTPUT("(NULL LL addr)");
136 return;
137 } else {
138 unsigned int i;
139 for(i = 0; i < LINKADDR_SIZE; i++) {
140 if(i > 0 && i % 2 == 0) {
141 LOG_OUTPUT(".");
142 }
143 LOG_OUTPUT("%02x", lladdr->u8[i]);
144 }
145 }
146}
147/*---------------------------------------------------------------------------*/
148void
149log_lladdr_compact(const linkaddr_t *lladdr)
150{
151 if(lladdr == NULL || linkaddr_cmp(lladdr, &linkaddr_null)) {
152 LOG_OUTPUT("LL-NULL");
153 } else {
154#if BUILD_WITH_DEPLOYMENT
155 LOG_OUTPUT("LL-%04u", deployment_id_from_lladdr(lladdr));
156#else /* BUILD_WITH_DEPLOYMENT */
157#if LINKADDR_SIZE == 8
158 LOG_OUTPUT("LL-%04x", UIP_HTONS(lladdr->u16[LINKADDR_SIZE/2-1]));
159#elif LINKADDR_SIZE == 2
160 LOG_OUTPUT("LL-%04x", UIP_HTONS(lladdr->u16));
161#endif
162#endif /* BUILD_WITH_DEPLOYMENT */
163 }
164}
165/*---------------------------------------------------------------------------*/
166void
167log_bytes(const void *data, size_t length)
168{
169 const uint8_t *u8data = (const uint8_t *)data;
170 for(size_t i = 0; i < length; ++i) {
171 if(LOG_WITH_COMPACT_BYTES) {
172 LOG_OUTPUT("%02x", u8data[i]);
173 } else {
174 LOG_OUTPUT(i == 0 ? "%02x" : " %02x", u8data[i]);
175 }
176 }
177}
178/*---------------------------------------------------------------------------*/
179void
180log_string(const char *text, size_t len)
181{
182 if(text == NULL) {
183 LOG_OUTPUT("(NULL STR)");
184 return;
185 }
186
187 for(int i = 0; i < len && *text != '\0'; i++, text++) {
188 LOG_OUTPUT("%c", *text);
189 }
190}
191/*---------------------------------------------------------------------------*/
192void
193log_set_level(const char *module, int level)
194{
195 if(level >= LOG_LEVEL_NONE && level <= LOG_LEVEL_DBG) {
196 int i = 0;
197 int module_all = !strcmp("all", module);
198 while(all_modules[i].name != NULL) {
199 if(module_all || !strcmp(module, all_modules[i].name)) {
200 *all_modules[i].curr_log_level = MIN(level, all_modules[i].max_log_level);
201 }
202 i++;
203 }
204 }
205}
206/*---------------------------------------------------------------------------*/
207int
208log_get_level(const char *module)
209{
210 int i = 0;
211 if(module == NULL) {
212 return -1;
213 }
214 while(all_modules[i].name != NULL) {
215 if(!strcmp(module, all_modules[i].name)) {
216 return *all_modules[i].curr_log_level;
217 }
218 i++;
219 }
220 return -1;
221}
222/*---------------------------------------------------------------------------*/
223const char *
225{
226 switch(level) {
227 case LOG_LEVEL_NONE:
228 return "None";
229 case LOG_LEVEL_ERR:
230 return "Errors";
231 case LOG_LEVEL_WARN:
232 return "Warnings";
233 case LOG_LEVEL_INFO:
234 return "Info";
235 case LOG_LEVEL_DBG:
236 return "Debug";
237 default:
238 return "N/A";
239 }
240}
241/** @} */
242/** @} */
Per-deployment MAC <-> nodeid mapping.
uint16_t deployment_id_from_iid(const uip_ipaddr_t *ipaddr)
Get node ID from the IID of an IPv6 address.
Definition deployment.c:115
uint16_t deployment_id_from_lladdr(const linkaddr_t *lladdr)
Get node ID from a link-layer address, from the deployment mapping table.
Definition deployment.c:82
bool linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition linkaddr.c:69
const linkaddr_t linkaddr_null
The null link-layer address.
void log_set_level(const char *module, int level)
Sets a log level at run-time.
Definition log.c:193
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:101
void log_6addr_compact(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address with a compact format.
Definition log.c:123
void log_lladdr(const linkaddr_t *lladdr)
Logs a link-layer address.
Definition log.c:132
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:180
const char * log_level_to_str(int level)
Returns a textual description of a log level.
Definition log.c:224
void log_lladdr_compact(const linkaddr_t *lladdr)
Logs a link-layer address with a compact format.
Definition log.c:149
void log_6addr(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address.
Definition log.c:93
int log_get_level(const char *module)
Returns the current log level.
Definition log.c:208
void log_bytes(const void *data, size_t length)
Logs a byte array as hex characters.
Definition log.c:167
int uiplib_ipaddr_snprint(char *buf, size_t size, const uip_ipaddr_t *addr)
Write at most size - 1 characters of the IP address to the output string.
Definition uiplib.c:182
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition uip.h:1860
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Definition uip.h:1766
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition uip.h:1157
Header file for the logging system.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition uip-nd6.c:116
Header file for the IP address manipulation library.