Rework logging module

This commit is contained in:
Simon Duquennoy 2017-06-26 14:24:47 +02:00
parent 076817faec
commit bde0123f49
3 changed files with 199 additions and 60 deletions

View File

@ -46,7 +46,37 @@
#ifndef __LOG_CONF_H__
#define __LOG_CONF_H__
/* A list of currently supported modules */
/* Log only the last 16 bytes of link-layer and IPv6 addresses */
#ifdef LOG_CONF_WITH_COMPACT_ADDR
#define LOG_WITH_COMPACT_ADDR LOG_CONF_WITH_COMPACT_ADDR
#else /* LOG_CONF_WITH_COMPACT_ADDR */
#define LOG_WITH_COMPACT_ADDR 0
#endif /* LOG_CONF_WITH_COMPACT_ADDR */
/* Prefix all logs with file name and line-of-code */
#ifdef LOG_CONF_WITH_LOC
#define LOG_WITH_LOC LOG_CONF_WITH_LOC
#else /* LOG_CONF_WITH_LOC */
#define LOG_WITH_LOC 0
#endif /* LOG_CONF_WITH_LOC */
/* Cooja annotations */
#ifdef LOG_CONF_WITH_ANNOTATE
#define LOG_WITH_ANNOTATE LOG_CONF_WITH_ANNOTATE
#else /* LOG_CONF_WITH_ANNOTATE */
#define LOG_WITH_ANNOTATE 0
#endif /* LOG_CONF_WITH_ANNOTATE */
/* Custom output function -- default is printf */
#ifdef LOG_CONF_OUTPUT
#define LOG_OUTPUT(...) LOG_CONF_OUTPUT(__VA_ARGS__)
#else /* LOG_CONF_OUTPUT */
#define LOG_OUTPUT(...) printf(__VA_ARGS__)
#endif /* LOG_CONF_OUTPUT */
/******************************************************************************/
/********************* A list of currently supported modules ******************/
/******************************************************************************/
#ifndef RPL_LOG_LEVEL
#define RPL_LOG_LEVEL LOG_LEVEL_NONE /* Only for rpl-lite */

136
core/sys/log.c Normal file
View File

@ -0,0 +1,136 @@
/*
* Copyright (c) 2017, RISE SICS.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
/**
* \file
* Header file for the logging system
* \author
* Simon Duquennoy <simon.duquennoy@ri.se>
*/
/** \addtogroup sys
* @{ */
/**
* \defgroup log Per-module, per-level logging
* @{
*
* The log module performs per-module, per-level logging
*
*/
#if NETSTACK_CONF_WITH_IPV6
#include "sys/log.h"
#include "net/ip/ip64-addr.h"
/*---------------------------------------------------------------------------*/
void
log_6addr(const uip_ipaddr_t *ipaddr)
{
uint16_t a;
unsigned int i;
int f;
if(ipaddr == NULL) {
LOG_OUTPUT("(NULL IP addr)");
return;
}
if(ip64_addr_is_ipv4_mapped_addr(ipaddr)) {
/* Printing IPv4-mapped addresses is done according to RFC 4291 */
LOG_OUTPUT("::FFFF:%u.%u.%u.%u", ipaddr->u8[12], ipaddr->u8[13], ipaddr->u8[14], ipaddr->u8[15]);
} else {
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (ipaddr->u8[i] << 8) + ipaddr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
LOG_OUTPUT("::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
LOG_OUTPUT(":");
}
LOG_OUTPUT("%x", a);
}
}
}
}
/*---------------------------------------------------------------------------*/
void
log_6addr_compact(const uip_ipaddr_t *ipaddr)
{
if(ipaddr == NULL) {
LOG_OUTPUT("6A-NULL");
} else if(uip_is_addr_mcast(ipaddr)) {
LOG_OUTPUT("6M-%04x", UIP_HTONS(ipaddr->u16[sizeof(uip_ipaddr_t)/2-1]));
} else if(uip_is_addr_linklocal(ipaddr)) {
LOG_OUTPUT("6L-%04x", UIP_HTONS(ipaddr->u16[sizeof(uip_ipaddr_t)/2-1]));
} else {
LOG_OUTPUT("6G-%04x", UIP_HTONS(ipaddr->u16[sizeof(uip_ipaddr_t)/2-1]));
}
}
#endif /* NETSTACK_CONF_WITH_IPV6 */
/*---------------------------------------------------------------------------*/
void
log_lladdr(const linkaddr_t *lladdr)
{
if(lladdr == NULL) {
LOG_OUTPUT("(NULL LL addr)");
return;
} else {
unsigned int i;
for(i = 0; i < LINKADDR_SIZE; i++) {
if(i > 0 && i % 2 == 0) {
LOG_OUTPUT(".");
}
LOG_OUTPUT("%02x", lladdr->u8[i]);
}
}
}
/*---------------------------------------------------------------------------*/
void
log_lladdr_compact(const linkaddr_t *lladdr)
{
if(lladdr == NULL || linkaddr_cmp(lladdr, &linkaddr_null)) {
LOG_OUTPUT("LL-NULL");
} else {
LOG_OUTPUT("LL-%04x", UIP_HTONS(lladdr->u16[LINKADDR_SIZE/2-1]));
}
}
/** @} */
/** @} */

View File

@ -54,74 +54,19 @@
#include <stdio.h>
#include "net/linkaddr.h"
#include "sys/log-conf.h"
#if NETSTACK_CONF_WITH_IPV6
#include "net/ip/uip.h"
#endif /* NETSTACK_CONF_WITH_IPV6 */
void net_debug_lladdr_print(const linkaddr_t *addr);
void uip_debug_ipaddr_print(const uip_ipaddr_t *addr);
/* The different log levels available */
#define LOG_LEVEL_NONE 0 /* No log */
#define LOG_LEVEL_ERR 1 /* Errors */
#define LOG_LEVEL_WARN 2 /* Warnings */
#define LOG_LEVEL_INFO 3 /* Basic info */
#define LOG_LEVEL_DBG 4 /* Detailled debug */
/* Prefix all logs with file name and line-of-code */
#ifdef LOG_CONF_WITH_LOC
#define LOG_WITH_LOC LOG_CONF_WITH_LOC
#else /* LOG_CONF_WITH_LOC */
#define LOG_WITH_LOC 0
#endif /* LOG_CONF_WITH_LOC */
/* Custom output function -- default is printf */
#ifdef LOG_CONF_OUTPUT
#define LOG_OUTPUT(...) LOG_CONF_OUTPUT(__VA_ARGS__)
#else /* LOG_CONF_OUTPUT */
#define LOG_OUTPUT(...) printf(__VA_ARGS__)
#endif /* LOG_CONF_OUTPUT */
/* Cooja annotations */
#ifdef LOG_CONF_WITH_ANNOTATE
#define LOG_WITH_ANNOTATE LOG_CONF_WITH_ANNOTATE
#else /* LOG_CONF_WITH_ANNOTATE */
#define LOG_WITH_ANNOTATE 0
#endif /* LOG_CONF_WITH_ANNOTATE */
/* Log only the last 16 bytes of linklayer and IPv6 addresses */
#ifdef LOG_CONF_WITH_COMPACT_ADDR
#define LOG_WITH_COMPACT_ADDR LOG_CONF_WITH_COMPACT_ADDR
#else /* LOG_CONF_WITH_COMPACT_ADDR */
#define LOG_WITH_COMPACT_ADDR 0
#endif /* LOG_CONF_WITH_COMPACT_ADDR */
/* Compact address representation and logging */
static inline void
log_lladdr_compact(const linkaddr_t *lladdr)
{
if(lladdr == NULL) {
LOG_OUTPUT("LL-NULL");
} else {
LOG_OUTPUT("LL-%04x", UIP_HTONS(lladdr->u16[LINKADDR_SIZE/2-1]));
}
}
static inline void
log_6addr_compact(const uip_ipaddr_t *ipaddr)
{
if(ipaddr == NULL) {
LOG_OUTPUT("6A-NULL");
} else if(uip_is_addr_mcast(ipaddr)) {
LOG_OUTPUT("6M-%04x", UIP_HTONS(ipaddr->u16[sizeof(uip_ipaddr_t)/2-1]));
} else if(uip_is_addr_linklocal(ipaddr)) {
LOG_OUTPUT("6L-%04x", UIP_HTONS(ipaddr->u16[sizeof(uip_ipaddr_t)/2-1]));
} else {
LOG_OUTPUT("6G-%04x", UIP_HTONS(ipaddr->u16[sizeof(uip_ipaddr_t)/2-1]));
}
}
/* Main log function */
#define LOG(newline, level, levelstr, ...) do { \
if(level <= LOG_LEVEL) { \
if(newline) { \
@ -147,7 +92,7 @@ log_6addr_compact(const uip_ipaddr_t *ipaddr)
if(LOG_WITH_COMPACT_ADDR) { \
log_lladdr_compact(lladdr); \
} else { \
net_debug_lladdr_print(lladdr); \
log_lladdr(lladdr); \
} \
} \
} while (0)
@ -158,7 +103,7 @@ log_6addr_compact(const uip_ipaddr_t *ipaddr)
if(LOG_WITH_COMPACT_ADDR) { \
log_6addr_compact(ipaddr); \
} else { \
uip_debug_ipaddr_print(ipaddr); \
log_6addr(ipaddr); \
} \
} \
} while (0)
@ -191,6 +136,34 @@ log_6addr_compact(const uip_ipaddr_t *ipaddr)
#define LOG_DBG_ENABLED (LOG_LEVEL >= LOG_LEVEL_DBG)
#define LOG_ANNOTATE_ENABLED (LOG_LEVEL >= LOG_LEVEL_ANNOTATE)
#if NETSTACK_CONF_WITH_IPV6
/**
* Logs an IPv6 address
* \param ipaddr The IPv6 address
*/
void log_6addr(const uip_ipaddr_t *ipaddr);
/**
* Logs an IPv6 address with a compact format
* \param ipaddr The IPv6 address
*/
void log_6addr_compact(const uip_ipaddr_t *ipaddr);
#endif /* NETSTACK_CONF_WITH_IPV6 */
/**
* Logs a link-layer address
* \param lladdr The link-layer address
*/
void log_lladdr(const linkaddr_t *lladdr);
/**
* Logs a link-layer address with a compact format
* \param lladdr The link-layer address
*/
void log_lladdr_compact(const linkaddr_t *lladdr);
#endif /* __LOG_H__ */
/** @} */