From 8e90fd0e2bbfa01fca11d09dac93ab963cb5c000 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Sun, 12 Nov 2017 17:43:05 +0800 Subject: [PATCH] Improve Log Signed-off-by: Jianhui Zhao --- CMakeLists.txt | 1 - README.md | 4 ++++ src/CMakeLists.txt | 7 +++++++ src/uhttp.c | 12 ++++++------ src/uhttp_buf.c | 4 ++++ src/uhttp_config.h.in | 2 ++ src/uhttp_log.c | 2 +- src/uhttp_log.h | 3 ++- 8 files changed, 26 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bdc687..358a872 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,6 @@ project(libuhttp C) list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/") #set(CMAKE_VERBOSE_MAKEFILE ON) -#add_definitions(-DUH_DEBUG) add_definitions(-O -Wall -Werror --std=gnu99 -D_GNU_SOURCE) diff --git a/README.md b/README.md index d1a73ae..04d80b9 100755 --- a/README.md +++ b/README.md @@ -56,6 +56,10 @@ Explicit use OpenSSL as its SSL backend Explicit use CyaSSl(wolfssl) as its SSL backend ~/libuhttp/build$ cmake .. -DUHTTP_USE_CYASSL=1 + +Turn on debug + + ~/libuhttp/build$ cmake .. -DUHTTP_DEBUG=1 ## Build and install libuhttp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fd05f5b..dd3d32c 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,6 +14,12 @@ include_directories(${LIBEV_INCLUDE_DIRS} ${HTTPPARSER_INCLUDE_DIRS}) set(EXTRA_LIBS ${LIBEV_LIBRARIES} ${HTTPPARSER_LIBRARIES}) set(SOURCE_FILES uhttp.c uhttp_log.c uhttp_buf.c uhttp_ssl.c) +set(UHTTP_DEBUG_CONFIG 0) +option(UHTTP_DEBUG "Turn on debug" OFF) +if(UHTTP_DEBUG) + set(UHTTP_DEBUG_CONFIG 1) +endif() + option(UHTTP_DISABLE_SSL "Disable ssl support" OFF) option(UHTTP_USE_OPENSSL "Explicit use OpenSSL as SSL backend" OFF) option(UHTTP_USE_CYASSL "Explicit use CyaSSL as SSL backend" OFF) @@ -91,4 +97,5 @@ message(STATUS "UHTTP_SSL_ENABLED: OpenSSL") elseif(UHTTP_USE_CYASSL_CONFIG) message(STATUS "UHTTP_SSL_ENABLED: CyaSSL") endif() +message(STATUS "DEBUG: ${UHTTP_DEBUG}") message("") diff --git a/src/uhttp.c b/src/uhttp.c index abb3782..5348c1b 100755 --- a/src/uhttp.c +++ b/src/uhttp.c @@ -148,18 +148,18 @@ static int on_message_complete(http_parser *parser) { struct uh_connection *con = container_of(parser, struct uh_connection, parser); struct uh_route *r; -#ifdef UH_DEBUG +#if (UHTTP_DEBUG) int i; struct uh_header *header = con->req.header; - printf("Url:[%.*s]\n", (int)con->req.url.len, con->req.url.at); + uh_log_debug("Url:[%.*s]\n", (int)con->req.url.len, con->req.url.at); for (i = 0; i < con->req.header_num; i++) { - printf("[%.*s:%.*s]\n", (int)header[i].field.len, header[i].field.at, + uh_log_debug("[%.*s:%.*s]\n", (int)header[i].field.len, header[i].field.at, (int)header[i].value.len, header[i].value.at); } - printf("Body:[%.*s]\n", (int)con->req.body.len, con->req.body.at); + uh_log_debug("Body:[%.*s]\n", (int)con->req.body.len, con->req.body.at); #endif list_for_each_entry(r, &con->srv->routes, list) { @@ -217,8 +217,8 @@ handshake_done: buf->len += len; -#ifdef UH_DEBUG - printf("read:[%.*s]\n", len, base); +#if (UHTTP_DEBUG) + uh_log_debug("read:[%.*s]\n", len, base); #endif parsered = http_parser_execute(&con->parser, &parser_settings, base, len); diff --git a/src/uhttp_buf.c b/src/uhttp_buf.c index e7332bd..b4ab1e2 100755 --- a/src/uhttp_buf.c +++ b/src/uhttp_buf.c @@ -1,6 +1,7 @@ #include #include #include "uhttp_buf.h" +#include "uhttp_log.h" int uh_buf_init(struct uh_buf *buf, size_t initial_size) { @@ -29,6 +30,9 @@ int uh_buf_grow(struct uh_buf *buf, size_t size) buf->base = base; buf->size += size; + + uh_log_debug("uh_buf_grow:%p +%d", buf, size); + return 0; } diff --git a/src/uhttp_config.h.in b/src/uhttp_config.h.in index 1662641..8ebd119 100755 --- a/src/uhttp_config.h.in +++ b/src/uhttp_config.h.in @@ -5,6 +5,8 @@ #define UHTTP_VERSION_MINOR @UHTTP_VERSION_MINOR@ #define UHTTP_VERSION_STRING "@UHTTP_VERSION_MAJOR@.@UHTTP_VERSION_MINOR@" +#define UHTTP_DEBUG @UHTTP_DEBUG_CONFIG@ + #define UHTTP_SSL_ENABLED @UHTTP_SSL_ENABLED_CONFIG@ #define UHTTP_USE_OPENSSL @UHTTP_USE_OPENSSL_CONFIG@ #define UHTTP_USE_CYASSL @UHTTP_USE_CYASSL_CONFIG@ diff --git a/src/uhttp_log.c b/src/uhttp_log.c index 01e811a..652c3f3 100755 --- a/src/uhttp_log.c +++ b/src/uhttp_log.c @@ -20,7 +20,7 @@ void __uh_log(const char *filename, int line, int priority, const char *format, -#ifdef UH_DEBUG +#if (UHTTP_DEBUG) fprintf(stderr, "%s\n", buf); #else if (priority == LOG_ERR) diff --git a/src/uhttp_log.h b/src/uhttp_log.h index c7a106e..8d8a49a 100755 --- a/src/uhttp_log.h +++ b/src/uhttp_log.h @@ -6,6 +6,7 @@ #include #include #include +#include "uhttp_config.h" #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) @@ -14,7 +15,7 @@ */ #define uh_log(priority, format...) __uh_log(__FILENAME__, __LINE__, priority, format) -#ifdef UH_DEBUG +#if (UHTTP_DEBUG) #define uh_log_debug(format...) uh_log(LOG_DEBUG, format) #else #define uh_log_debug(format...)