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