Contiki-NG
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"
54 #include "deployment/deployment.h"
55 
56 int curr_log_level_rpl = LOG_CONF_LEVEL_RPL;
57 int curr_log_level_tcpip = LOG_CONF_LEVEL_TCPIP;
58 int curr_log_level_ipv6 = LOG_CONF_LEVEL_IPV6;
59 int curr_log_level_6lowpan = LOG_CONF_LEVEL_6LOWPAN;
60 int curr_log_level_nullnet = LOG_CONF_LEVEL_NULLNET;
61 int curr_log_level_mac = LOG_CONF_LEVEL_MAC;
62 int curr_log_level_framer = LOG_CONF_LEVEL_FRAMER;
63 int curr_log_level_6top = LOG_CONF_LEVEL_6TOP;
64 int curr_log_level_coap = LOG_CONF_LEVEL_COAP;
65 int curr_log_level_snmp = LOG_CONF_LEVEL_SNMP;
66 int curr_log_level_lwm2m = LOG_CONF_LEVEL_LWM2M;
67 int curr_log_level_main = LOG_CONF_LEVEL_MAIN;
68 
69 struct log_module all_modules[] = {
70  {"rpl", &curr_log_level_rpl, LOG_CONF_LEVEL_RPL},
71  {"tcpip", &curr_log_level_tcpip, LOG_CONF_LEVEL_TCPIP},
72  {"ipv6", &curr_log_level_ipv6, LOG_CONF_LEVEL_IPV6},
73  {"6lowpan", &curr_log_level_6lowpan, LOG_CONF_LEVEL_6LOWPAN},
74  {"nullnet", &curr_log_level_nullnet, LOG_CONF_LEVEL_NULLNET},
75  {"mac", &curr_log_level_mac, LOG_CONF_LEVEL_MAC},
76  {"framer", &curr_log_level_framer, LOG_CONF_LEVEL_FRAMER},
77  {"6top", &curr_log_level_6top, LOG_CONF_LEVEL_6TOP},
78  {"coap", &curr_log_level_coap, LOG_CONF_LEVEL_COAP},
79  {"snmp", &curr_log_level_snmp, LOG_CONF_LEVEL_SNMP},
80  {"lwm2m", &curr_log_level_lwm2m, LOG_CONF_LEVEL_LWM2M},
81  {"main", &curr_log_level_main, LOG_CONF_LEVEL_MAIN},
82  {NULL, NULL, 0},
83 };
84 
85 #if NETSTACK_CONF_WITH_IPV6
86 
87 /*---------------------------------------------------------------------------*/
88 void
89 log_6addr(const uip_ipaddr_t *ipaddr)
90 {
91  char buf[UIPLIB_IPV6_MAX_STR_LEN];
92  uiplib_ipaddr_snprint(buf, sizeof(buf), ipaddr);
93  LOG_OUTPUT("%s", buf);
94 }
95 /*---------------------------------------------------------------------------*/
96 int
97 log_6addr_compact_snprint(char *buf, size_t size, const uip_ipaddr_t *ipaddr)
98 {
99  if(ipaddr == NULL) {
100  return snprintf(buf, size, "6A-NULL");
101  } else {
102  char *prefix = NULL;
103  if(uip_is_addr_mcast(ipaddr)) {
104  prefix = "6M";
105  } else if(uip_is_addr_linklocal(ipaddr)) {
106  prefix = "6L";
107  } else {
108  prefix = "6G";
109  }
110 #if BUILD_WITH_DEPLOYMENT
111  return snprintf(buf, size, "%s-%03u", prefix, deployment_id_from_iid(ipaddr));
112 #else /* BUILD_WITH_DEPLOYMENT */
113  return snprintf(buf, size, "%s-%04x", prefix, UIP_HTONS(ipaddr->u16[sizeof(uip_ipaddr_t)/2-1]));
114 #endif /* BUILD_WITH_DEPLOYMENT */
115  }
116 }
117 /*---------------------------------------------------------------------------*/
118 void
119 log_6addr_compact(const uip_ipaddr_t *ipaddr)
120 {
121  char buf[8];
122  log_6addr_compact_snprint(buf, sizeof(buf), ipaddr);
123  LOG_OUTPUT("%s", buf);
124 }
125 #endif /* NETSTACK_CONF_WITH_IPV6 */
126 /*---------------------------------------------------------------------------*/
127 void
128 log_lladdr(const linkaddr_t *lladdr)
129 {
130  if(lladdr == NULL) {
131  LOG_OUTPUT("(NULL LL addr)");
132  return;
133  } else {
134  unsigned int i;
135  for(i = 0; i < LINKADDR_SIZE; i++) {
136  if(i > 0 && i % 2 == 0) {
137  LOG_OUTPUT(".");
138  }
139  LOG_OUTPUT("%02x", lladdr->u8[i]);
140  }
141  }
142 }
143 /*---------------------------------------------------------------------------*/
144 void
145 log_lladdr_compact(const linkaddr_t *lladdr)
146 {
147  if(lladdr == NULL || linkaddr_cmp(lladdr, &linkaddr_null)) {
148  LOG_OUTPUT("LL-NULL");
149  } else {
150 #if BUILD_WITH_DEPLOYMENT
151  LOG_OUTPUT("LL-%04u", deployment_id_from_lladdr(lladdr));
152 #else /* BUILD_WITH_DEPLOYMENT */
153 #if LINKADDR_SIZE == 8
154  LOG_OUTPUT("LL-%04x", UIP_HTONS(lladdr->u16[LINKADDR_SIZE/2-1]));
155 #elif LINKADDR_SIZE == 2
156  LOG_OUTPUT("LL-%04x", UIP_HTONS(lladdr->u16));
157 #endif
158 #endif /* BUILD_WITH_DEPLOYMENT */
159  }
160 }
161 /*---------------------------------------------------------------------------*/
162 void
163 log_set_level(const char *module, int level)
164 {
165  if(level >= LOG_LEVEL_NONE && level <= LOG_LEVEL_DBG) {
166  int i = 0;
167  int module_all = !strcmp("all", module);
168  while(all_modules[i].name != NULL) {
169  if(module_all || !strcmp(module, all_modules[i].name)) {
170  *all_modules[i].curr_log_level = MIN(level, all_modules[i].max_log_level);
171  }
172  i++;
173  }
174  }
175 }
176 /*---------------------------------------------------------------------------*/
177 int
178 log_get_level(const char *module)
179 {
180  int i = 0;
181  if(module == NULL) {
182  return -1;
183  }
184  while(all_modules[i].name != NULL) {
185  if(!strcmp(module, all_modules[i].name)) {
186  return *all_modules[i].curr_log_level;
187  }
188  i++;
189  }
190  return -1;
191 }
192 /*---------------------------------------------------------------------------*/
193 const char *
195 {
196  switch(level) {
197  case LOG_LEVEL_NONE:
198  return "None";
199  case LOG_LEVEL_ERR:
200  return "Errors";
201  case LOG_LEVEL_WARN:
202  return "Warnings";
203  case LOG_LEVEL_INFO:
204  return "Info";
205  case LOG_LEVEL_DBG:
206  return "Debug";
207  default:
208  return "N/A";
209  }
210 }
211 /** @} */
212 /** @} */
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:116
const linkaddr_t linkaddr_null
The null link-layer address.
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
Header file for the IP address manipulation library.
Per-deployment MAC <-> nodeid mapping.
#define uip_is_addr_mcast(a)
is address a multicast address, see RFC 4291 a is of type uip_ipaddr_t*
Definition: uip.h:1961
void log_6addr(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address.
Definition: log.c:89
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
Definition: linkaddr.c:69
#define UIP_HTONS(n)
Convert 16-bit quantity from host byte order to network byte order.
Definition: uip.h:1223
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
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
#define uip_is_addr_linklocal(a)
is addr (a) a link local unicast address, see RFC 4291 i.e.
Definition: uip.h:1877
Header file for the logging system
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
void log_6addr_compact(const uip_ipaddr_t *ipaddr)
Logs an IPv6 address with a compact format.
Definition: log.c:119
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:166