Skip to content

Commit

Permalink
net: ip: Improve logging by adding a dedicated sys_log level
Browse files Browse the repository at this point in the history
Let's make net stack having its own level of debugging through sys_log.
It replaces NET_DEBUG by NET_LOG_ENABLED, which is then semantically
better: someone wanting to log the errors might want that not only for
debugging.

Along with it, CONFIG_NET_LOG_GLOBAL option is added, in order to enable
all available logging in network stack. It is disabled by default but
might be found useful when warning/errors need to be logged, so it is
then unnecessary to selectively enable by hand all CONFIG_NET_DEBUG_*
options.

It is possible, locally, to override CONFIG_SYS_LOG_NET_LEVEL by setting
the level one want to NET_SYS_LOG_LEVEL. This can be useful on samples
or tests.

Change-Id: I56a8f052340bc3a932229963cc69b39912093b88
Signed-off-by: Tomasz Bursztyka <[email protected]>
  • Loading branch information
Tomasz Bursztyka committed Jan 2, 2017
1 parent 4869dc9 commit a1aa08c
Show file tree
Hide file tree
Showing 53 changed files with 230 additions and 190 deletions.
14 changes: 8 additions & 6 deletions include/net/net_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ extern "C" {

/* Network subsystem logging helpers */

#if defined(CONFIG_NET_LOG)
#if NET_DEBUG > 0
#if defined(NET_LOG_ENABLED)
#if !defined(SYS_LOG_DOMAIN)
#define SYS_LOG_DOMAIN "net"
#endif /* !SYS_LOG_DOMAIN */

#undef SYS_LOG_LEVEL
#define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#endif /* NET_DEBUG */
#ifndef NET_SYS_LOG_LEVEL
#define SYS_LOG_LEVEL CONFIG_SYS_LOG_NET_LEVEL
#else
#define SYS_LOG_LEVEL NET_SYS_LOG_LEVEL
#endif /* !NET_SYS_LOG_LEVEL */

#define NET_DBG(fmt, ...) SYS_LOG_DBG("(%p): " fmt, k_current_get(), \
##__VA_ARGS__)
Expand All @@ -53,14 +55,14 @@ extern "C" {
NET_ERR("{assert: '" #cond "' failed} " fmt, \
##__VA_ARGS__); \
} } while (0)
#else /* CONFIG_NET_LOG */
#else /* NET_LOG_ENABLED */
#define NET_DBG(...)
#define NET_ERR(...)
#define NET_INFO(...)
#define NET_WARN(...)
#define NET_ASSERT(...)
#define NET_ASSERT_INFO(...)
#endif /* CONFIG_NET_LOG */
#endif /* NET_LOG_ENABLED */

#include <kernel.h>

Expand Down
6 changes: 3 additions & 3 deletions include/net/net_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ enum net_addr_type {
NET_ADDR_MANUAL,
};

#if NET_DEBUG > 0
#if NET_LOG_ENABLED > 0
static inline char *net_addr_type2str(enum net_addr_type type)
{
switch (type) {
Expand All @@ -219,14 +219,14 @@ static inline char *net_addr_type2str(enum net_addr_type type)

return "<unknown>";
}
#else
#else /* NET_LOG_ENABLED */
static inline char *net_addr_type2str(enum net_addr_type type)
{
ARG_UNUSED(type);

return NULL;
}
#endif
#endif /* NET_LOG_ENABLED */

/** What is the current state of the network address */
enum net_addr_state {
Expand Down
4 changes: 2 additions & 2 deletions samples/net/dhcpv4_client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#if 1
#define SYS_LOG_DOMAIN "dhcpv4"
#define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_DEBUG 1
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif

#include <zephyr.h>
Expand Down
4 changes: 2 additions & 2 deletions samples/net/echo_client/src/echo-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

#if 1
#define SYS_LOG_DOMAIN "echo-client"
#define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_DEBUG 1
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif

#include <zephyr.h>
Expand Down
4 changes: 2 additions & 2 deletions samples/net/echo_server/src/echo-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#if 1
#define SYS_LOG_DOMAIN "echo-server"
#define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_DEBUG 1
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif

#include <zephyr.h>
Expand Down
4 changes: 2 additions & 2 deletions samples/net/leds_demo/src/leds-demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#if 1
#define SYS_LOG_DOMAIN "zoap-server"
#define SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_DEBUG 1
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif

#include <errno.h>
Expand Down
4 changes: 2 additions & 2 deletions samples/net/zperf/src/zperf_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
#include "zperf_session.h"

/* Get some useful debug routings from net_private.h, requires
* that NET_DEBUG is set.
* that NET_LOG_ENABLED is set.
*/
#define NET_DEBUG 1
#define NET_LOG_ENABLED 1
#include "net_private.h"

#include "ipv6.h" /* to get infinite lifetime */
Expand Down
2 changes: 1 addition & 1 deletion samples/net/zperf/src/zperf_udp_receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "zperf_session.h"

/* To get net_sprint_ipv{4|6}_addr() */
#define NET_DEBUG 1
#define NET_LOG_ENABLED 1
#include "net_private.h"

#define TAG CMD_STR_UDP_DOWNLOAD" "
Expand Down
2 changes: 1 addition & 1 deletion subsys/net/ip/6lo.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#if defined(CONFIG_NET_DEBUG_6LO)
#define SYS_LOG_DOMAIN "net/6lo"
#define NET_DEBUG 1
#define NET_LOG_ENABLED 1
#endif

#include <errno.h>
Expand Down
44 changes: 44 additions & 0 deletions subsys/net/ip/Kconfig.debug
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,50 @@ menuconfig NET_LOG

if NET_LOG

config SYS_LOG_NET_LEVEL
int
prompt "Network Stack Logging level"
default 1
range 0 4
help
Sets log level for the network stack.
Levels are:
0 OFF, do not write
1 ERROR, only write SYS_LOG_ERR
2 WARNING, write SYS_LOG_WRN in adition to previous level
3 INFO, write SYS_LOG_INF in adition to previous levels
4 DEBUG, write SYS_LOG_DBG in adition to previous levels

config NET_LOG_GLOBAL
bool "Enable global network stack logging"
default n
select NET_DEBUG_CORE
select NET_DEBUG_IF
select NET_DEBUG_UTILS
select NET_DEBUG_CONTEXT
select NET_DEBUG_NET_BUF
select NET_DEBUG_CONN
select NET_DEBUG_ROUTE if NET_ROUTE
select NET_DEBUG_IPV6 if NET_IPV6
select NET_DEBUG_ICMPV6 if NET_IPV6
select NET_DEBUG_IPV6_NBR_CACHE if NET_IPV6
select NET_DEBUG_6LO if NET_6LO
select NET_DEBUG_IPV4 if NET_IPV4
select NET_DEBUG_ICMPV4 if NET_IPV4
select NET_DEBUG_DHCPV4 if NET_DHCPV4
select NET_DEBUG_UDP if NET_UDP
select NET_DEBUG_TCP if NET_TCP
select NET_DEBUG_RPL if NET_RPL
select NET_DEBUG_TRICKLE if NET_TRICKLE
select NET_DEBUG_MGMT_EVENT if NET_MGMT
select NET_DEBUG_MGMT_EVENT_STACK if NET_MGMT_EVENT
help
By default, logging will apply only on enabled CONFIG_NET_DEBUG_*
options, on which CONFIG_SYS_LOG_NET_LEVEL would be applied.
However, if you want all the network stack logging enabled at once,
use this option. Beware logging takes a lot of ROM/RAM and kills
execution timing so it can affect your use case.

config NET_SHELL
bool "Enable network shell utilities"
default n
Expand Down
14 changes: 7 additions & 7 deletions subsys/net/ip/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#if defined(CONFIG_NET_DEBUG_CONN)
#define SYS_LOG_DOMAIN "net/conn"
#define NET_DEBUG 1
#define NET_LOG_ENABLED 1
#endif

#include <errno.h>
Expand Down Expand Up @@ -60,7 +60,7 @@ static struct net_conn conns[CONFIG_NET_MAX_CONN];
*/
#define NET_CONN_BUF(buf) ((struct net_udp_hdr *)(net_nbuf_udp_data(buf)))

#if NET_DEBUG
#if defined(CONFIG_NET_DEBUG_CONN)
static inline const char *proto2str(enum net_ip_protocol proto)
{
switch (proto) {
Expand All @@ -78,7 +78,7 @@ static inline const char *proto2str(enum net_ip_protocol proto)

return "<unknown>";
}
#endif /* NET_DEBUG */
#endif /* CONFIG_NET_DEBUG_CONN */

#if defined(CONFIG_NET_CONN_CACHE)

Expand Down Expand Up @@ -389,7 +389,7 @@ int net_conn_change_callback(struct net_conn_handle *handle,
return 0;
}

#if NET_DEBUG
#if defined(CONFIG_NET_DEBUG_CONN)
static inline
void prepare_register_debug_print(char *dst, int dst_len,
char *src, int src_len,
Expand Down Expand Up @@ -440,7 +440,7 @@ void prepare_register_debug_print(char *dst, int dst_len,
snprintf(src, src_len, "-");
}
}
#endif /* NET_DEBUG */
#endif /* CONFIG_NET_DEBUG_CONN */

int net_conn_register(enum net_ip_protocol proto,
const struct sockaddr *remote_addr,
Expand Down Expand Up @@ -558,7 +558,7 @@ int net_conn_register(enum net_ip_protocol proto,
/* Cache needs to be cleared if new entries are added. */
cache_clear();

#if NET_DEBUG
#if defined(CONFIG_NET_DEBUG_CONN)
do {
char dst[NET_IPV6_ADDR_LEN];
char src[NET_IPV6_ADDR_LEN];
Expand All @@ -575,7 +575,7 @@ int net_conn_register(enum net_ip_protocol proto,
local_addr, src, local_port,
cb, user_data);
} while (0);
#endif /* NET_DEBUG */
#endif /* CONFIG_NET_DEBUG_CONN */

if (handle) {
*handle = (struct net_conn_handle *)&conns[i];
Expand Down
2 changes: 1 addition & 1 deletion subsys/net/ip/dhcpv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#if defined(CONFIG_NET_DEBUG_DHCPV4)
#define SYS_LOG_DOMAIN "net/dhcpv4"
#define NET_DEBUG 1
#define NET_LOG_ENABLED 1
#endif

#include <errno.h>
Expand Down
20 changes: 10 additions & 10 deletions subsys/net/ip/icmpv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
* limitations under the License.
*/

#ifdef CONFIG_NET_DEBUG_ICMPV4
#if defined(CONFIG_NET_DEBUG_ICMPV4)
#define SYS_LOG_DOMAIN "net/icmpv4"
#define NET_DEBUG 1
#define NET_LOG_ENABLED 1
#endif

#include <errno.h>
Expand All @@ -39,14 +39,14 @@ static inline enum net_verdict handle_echo_request(struct net_buf *buf)
*/
struct in_addr addr;

#if NET_DEBUG > 0
#if defined(CONFIG_NET_DEBUG_ICMPV4)
char out[sizeof("xxx.xxx.xxx.xxx")];

snprintf(out, sizeof(out),
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst));
NET_DBG("Received Echo Request from %s to %s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out);
#endif /* NET_DEBUG > 0 */
#endif /* CONFIG_NET_DEBUG_ICMPV4 */

net_ipaddr_copy(&addr, &NET_IPV4_BUF(buf)->src);
net_ipaddr_copy(&NET_IPV4_BUF(buf)->src,
Expand All @@ -58,12 +58,12 @@ static inline enum net_verdict handle_echo_request(struct net_buf *buf)
NET_ICMP_BUF(buf)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf);

#if NET_DEBUG > 0
#if defined(CONFIG_NET_DEBUG_ICMPV4)
snprintf(out, sizeof(out),
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->dst));
NET_DBG("Sending Echo Reply from %s to %s",
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out);
#endif /* NET_DEBUG > 0 */
#endif /* CONFIG_NET_DEBUG_ICMPV4 */

if (net_send_data(buf) < 0) {
net_stats_update_icmp_drop();
Expand Down Expand Up @@ -141,7 +141,7 @@ int net_icmpv4_send_echo_request(struct net_if *iface,
NET_ICMP_BUF(buf)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf);

#if NET_DEBUG > 0
#if defined(CONFIG_NET_DEBUG_ICMPV4)
do {
char out[NET_IPV4_ADDR_LEN];

Expand All @@ -152,7 +152,7 @@ int net_icmpv4_send_echo_request(struct net_if *iface,
" from %s to %s", NET_ICMPV4_ECHO_REQUEST,
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out);
} while (0);
#endif /* NET_DEBUG > 0 */
#endif /* CONFIG_NET_DEBUG_ICMPV4 */

net_buf_add(buf->frags, sizeof(struct net_ipv4_hdr) +
sizeof(struct net_icmp_hdr) +
Expand Down Expand Up @@ -255,7 +255,7 @@ int net_icmpv4_send_error(struct net_buf *orig, uint8_t type, uint8_t code)
NET_ICMP_BUF(buf)->chksum = 0;
NET_ICMP_BUF(buf)->chksum = ~net_calc_chksum_icmpv4(buf);

#if NET_DEBUG > 0
#if defined(CONFIG_NET_DEBUG_ICMPV4)
do {
char out[sizeof("xxx.xxx.xxx.xxx")];

Expand All @@ -265,7 +265,7 @@ int net_icmpv4_send_error(struct net_buf *orig, uint8_t type, uint8_t code)
"from %s to %s", type, code,
net_sprint_ipv4_addr(&NET_IPV4_BUF(buf)->src), out);
} while (0);
#endif /* NET_DEBUG > 0 */
#endif /* CONFIG_NET_DEBUG_ICMPV4 */

if (net_send_data(buf) >= 0) {
net_stats_update_icmp_sent();
Expand Down
Loading

0 comments on commit a1aa08c

Please sign in to comment.