net: lib: Convert http library to use log level

Use network log level in HTTP library.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2018-07-10 15:44:02 +03:00
commit 8ffb0fc968
4 changed files with 61 additions and 71 deletions

View file

@ -67,11 +67,11 @@ config HTTP_CLIENT_NETWORK_TIMEOUT
Default network activity timeout in seconds. This setting is used Default network activity timeout in seconds. This setting is used
for TCP connection timeout. for TCP connection timeout.
config NET_DEBUG_HTTP module=HTTP
bool "Debug HTTP" module-dep=NET_LOG
depends on HTTP && NET_LOG module-str=Log level for HTTP
help module-help=Enables routing engine debug messages.
Enables HTTP output debug messages source "subsys/net/Kconfig.template.log_config.net"
config NET_DEBUG_HTTP_CONN config NET_DEBUG_HTTP_CONN
bool "Debug HTTP connections" bool "Debug HTTP connections"

View file

@ -4,11 +4,8 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#if defined(CONFIG_NET_DEBUG_HTTP) #define LOG_MODULE_NAME net_http
#define SYS_LOG_DOMAIN "http" #define NET_LOG_LEVEL CONFIG_HTTP_LOG_LEVEL
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif
#include <zephyr.h> #include <zephyr.h>
#include <string.h> #include <string.h>

View file

@ -4,15 +4,12 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#if defined(CONFIG_NET_DEBUG_HTTP)
#if defined(CONFIG_HTTPS) #if defined(CONFIG_HTTPS)
#define SYS_LOG_DOMAIN "https/client" #define LOG_MODULE_NAME net_https_client
#else #else
#define SYS_LOG_DOMAIN "http/client" #define LOG_MODULE_NAME net_http_client
#endif
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif #endif
#define NET_LOG_LEVEL CONFIG_HTTP_LOG_LEVEL
#include <zephyr.h> #include <zephyr.h>
#include <string.h> #include <string.h>
@ -168,7 +165,6 @@ out:
return ret; return ret;
} }
#if defined(CONFIG_NET_DEBUG_HTTP)
static void sprint_addr(char *buf, int len, static void sprint_addr(char *buf, int len,
sa_family_t family, sa_family_t family,
struct sockaddr *addr) struct sockaddr *addr)
@ -181,27 +177,28 @@ static void sprint_addr(char *buf, int len,
NET_DBG("Invalid protocol family"); NET_DBG("Invalid protocol family");
} }
} }
#endif
static inline void print_info(struct http_ctx *ctx, static inline void print_info(struct http_ctx *ctx,
enum http_method method) enum http_method method)
{ {
#if defined(CONFIG_NET_DEBUG_HTTP) if (NET_LOG_LEVEL >= LOG_LEVEL_INF) {
char local[NET_IPV6_ADDR_LEN]; char local[NET_IPV6_ADDR_LEN];
char remote[NET_IPV6_ADDR_LEN]; char remote[NET_IPV6_ADDR_LEN];
sprint_addr(local, NET_IPV6_ADDR_LEN, sprint_addr(local, NET_IPV6_ADDR_LEN,
ctx->app_ctx.default_ctx->local.sa_family, ctx->app_ctx.default_ctx->local.sa_family,
&ctx->app_ctx.default_ctx->local); &ctx->app_ctx.default_ctx->local);
sprint_addr(remote, NET_IPV6_ADDR_LEN, sprint_addr(remote, NET_IPV6_ADDR_LEN,
ctx->app_ctx.default_ctx->remote.sa_family, ctx->app_ctx.default_ctx->remote.sa_family,
&ctx->app_ctx.default_ctx->remote); &ctx->app_ctx.default_ctx->remote);
NET_DBG("HTTP %s (%s) %s -> %s port %d", NET_DBG("HTTP %s (%s) %s -> %s port %d",
http_method_str(method), ctx->http.req.host, local, remote, http_method_str(method), ctx->http.req.host, local,
ntohs(net_sin(&ctx->app_ctx.default_ctx->remote)->sin_port)); remote,
#endif ntohs(net_sin(&ctx->app_ctx.default_ctx->remote)->
sin_port));
}
} }
int http_client_send_req(struct http_ctx *ctx, int http_client_send_req(struct http_ctx *ctx,
@ -273,21 +270,21 @@ out:
static void print_header_field(size_t len, const char *str) static void print_header_field(size_t len, const char *str)
{ {
#if defined(CONFIG_NET_DEBUG_HTTP) if (NET_LOG_LEVEL >= LOG_LEVEL_INF) {
#define MAX_OUTPUT_LEN 128 #define MAX_OUTPUT_LEN 128
char output[MAX_OUTPUT_LEN]; char output[MAX_OUTPUT_LEN];
/* The value of len does not count \0 so we need to increase it /* The value of len does not count \0 so we need to increase it
* by one. * by one.
*/ */
if ((len + 1) > sizeof(output)) { if ((len + 1) > sizeof(output)) {
len = sizeof(output) - 1; len = sizeof(output) - 1;
}
snprintk(output, len + 1, "%s", str);
NET_DBG("[%zd] %s", len, output);
} }
snprintk(output, len + 1, "%s", str);
NET_DBG("[%zd] %s", len, output);
#endif
} }
static int on_url(struct http_parser *parser, const char *at, size_t length) static int on_url(struct http_parser *parser, const char *at, size_t length)
@ -435,16 +432,15 @@ static int on_headers_complete(struct http_parser *parser)
static int on_message_begin(struct http_parser *parser) static int on_message_begin(struct http_parser *parser)
{ {
#if defined(CONFIG_NET_DEBUG_HTTP) && (CONFIG_SYS_LOG_NET_LEVEL > 2) if (NET_LOG_LEVEL >= LOG_LEVEL_INF) {
struct http_ctx *ctx = CONTAINER_OF(parser, struct http_ctx *ctx = CONTAINER_OF(parser,
struct http_ctx, struct http_ctx,
http.parser); http.parser);
NET_DBG("-- HTTP %s response (headers) --",
http_method_str(ctx->http.req.method));
}
NET_DBG("-- HTTP %s response (headers) --",
http_method_str(ctx->http.req.method));
#else
ARG_UNUSED(parser);
#endif
return 0; return 0;
} }

View file

@ -4,15 +4,12 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
#if defined(CONFIG_NET_DEBUG_HTTP)
#if defined(CONFIG_HTTPS) #if defined(CONFIG_HTTPS)
#define SYS_LOG_DOMAIN "https/server" #define LOG_MODULE_NAME net_https_server
#else #else
#define SYS_LOG_DOMAIN "http/server" #define LOG_MODULE_NAME net_http_server
#endif
#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG
#define NET_LOG_ENABLED 1
#endif #endif
#define NET_LOG_LEVEL CONFIG_HTTP_LOG_LEVEL
#include <zephyr.h> #include <zephyr.h>
#include <string.h> #include <string.h>
@ -81,7 +78,7 @@ void http_server_conn_monitor(http_server_cb_t cb, void *user_data)
const char * const http_state_str(enum http_state state) const char * const http_state_str(enum http_state state)
{ {
#if defined(CONFIG_NET_DEBUG_HTTP) #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
switch (state) { switch (state) {
case HTTP_STATE_CLOSED: case HTTP_STATE_CLOSED:
return "CLOSED"; return "CLOSED";
@ -94,14 +91,14 @@ const char * const http_state_str(enum http_state state)
case HTTP_STATE_OPEN: case HTTP_STATE_OPEN:
return "OPEN"; return "OPEN";
} }
#else /* CONFIG_NET_DEBUG_HTTP */ #else
ARG_UNUSED(state); ARG_UNUSED(state);
#endif /* CONFIG_NET_DEBUG_HTTP */ #endif
return ""; return "";
} }
#if defined(CONFIG_NET_DEBUG_HTTP) #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
static void validate_state_transition(struct http_ctx *ctx, static void validate_state_transition(struct http_ctx *ctx,
enum http_state current, enum http_state current,
enum http_state new) enum http_state new)
@ -127,7 +124,7 @@ static void validate_state_transition(struct http_ctx *ctx,
http_state_str(new), new); http_state_str(new), new);
} }
} }
#endif /* CONFIG_NET_DEBUG_HTTP */ #endif /* NET_LOG_LEVEL */
void _http_change_state(struct http_ctx *ctx, void _http_change_state(struct http_ctx *ctx,
enum http_state new_state, enum http_state new_state,
@ -145,9 +142,9 @@ void _http_change_state(struct http_ctx *ctx,
http_state_str(new_state), new_state, http_state_str(new_state), new_state,
func, line); func, line);
#if defined(CONFIG_NET_DEBUG_HTTP) #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
validate_state_transition(ctx, ctx->state, new_state); validate_state_transition(ctx, ctx->state, new_state);
#endif /* CONFIG_NET_DEBUG_HTTP */ #endif
ctx->state = new_state; ctx->state = new_state;
} }
@ -217,7 +214,7 @@ quit:
return ret; return ret;
} }
#if defined(CONFIG_NET_DEBUG_HTTP) && (CONFIG_SYS_LOG_NET_LEVEL > 2) #if NET_LOG_LEVEL >= LOG_LEVEL_INF
static char *sprint_ipaddr(char *buf, int buflen, const struct sockaddr *addr) static char *sprint_ipaddr(char *buf, int buflen, const struct sockaddr *addr)
{ {
if (addr->sa_family == AF_INET6) { if (addr->sa_family == AF_INET6) {
@ -300,14 +297,14 @@ static struct net_context *get_server_ctx(struct net_app_ctx *ctx,
return NULL; return NULL;
} }
#endif /* CONFIG_NET_DEBUG_HTTP */ #endif /* NET_LOG_LEVEL */
static inline void new_client(struct http_ctx *ctx, static inline void new_client(struct http_ctx *ctx,
enum http_connection_type type, enum http_connection_type type,
struct net_app_ctx *app_ctx, struct net_app_ctx *app_ctx,
const struct sockaddr *dst) const struct sockaddr *dst)
{ {
#if defined(CONFIG_NET_DEBUG_HTTP) && (CONFIG_SYS_LOG_NET_LEVEL > 2) #if NET_LOG_LEVEL >= LOG_LEVEL_INF
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
#define PORT_LEN sizeof("[]:xxxxx") #define PORT_LEN sizeof("[]:xxxxx")
#define ADDR_LEN NET_IPV6_ADDR_LEN #define ADDR_LEN NET_IPV6_ADDR_LEN
@ -331,7 +328,7 @@ static inline void new_client(struct http_ctx *ctx,
} else { } else {
NET_INFO("[%p] %s connection", ctx, type_str); NET_INFO("[%p] %s connection", ctx, type_str);
} }
#endif /* CONFIG_NET_DEBUG_HTTP */ #endif /* NET_LOG_LEVEL */
} }
static void url_connected(struct http_ctx *ctx, static void url_connected(struct http_ctx *ctx,
@ -996,7 +993,7 @@ static inline void new_server(struct http_ctx *ctx,
const char *server_banner, const char *server_banner,
const struct sockaddr *addr) const struct sockaddr *addr)
{ {
#if defined(CONFIG_NET_DEBUG_HTTP) && (CONFIG_SYS_LOG_NET_LEVEL > 2) #if NET_LOG_LEVEL >= LOG_LEVEL_INF
#if defined(CONFIG_NET_IPV6) #if defined(CONFIG_NET_IPV6)
#define PORT_STR sizeof("[]:xxxxx") #define PORT_STR sizeof("[]:xxxxx")
char buf[NET_IPV6_ADDR_LEN + PORT_STR]; char buf[NET_IPV6_ADDR_LEN + PORT_STR];
@ -1011,7 +1008,7 @@ static inline void new_server(struct http_ctx *ctx,
} else { } else {
NET_INFO("%s (%p)", server_banner, ctx); NET_INFO("%s (%p)", server_banner, ctx);
} }
#endif /* CONFIG_NET_DEBUG_HTTP */ #endif /* NET_LOG_LEVEL */
} }
static void init_net(struct http_ctx *ctx, static void init_net(struct http_ctx *ctx,