Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Added EVHTP_DEBUG option (with new logging)
Browse files Browse the repository at this point in the history
- Passing 'cmake -DEVHTP_DEBUG:STRING=ON' to cmake enables verbose
  debug logging.

- Added log.h, simple pretty-logger.
  • Loading branch information
NathanFrench committed Sep 14, 2017
1 parent aa8a989 commit f20e5cf
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 80 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ OPTION(EVHTP_BUILD_SHARED "Build shared library too" OFF)
OPTION(EVHTP_USE_JEMALLOC "Enable jemalloc allocator" OFF)
OPTION(EVHTP_USE_TCMALLOC "Enable tcmalloc allocator" OFF)

OPTION(EVHTP_DEBUG "Enable verbose debug logging" OFF)

# fun color stuff
if(NOT WIN32)
string(ASCII 27 Esc)
Expand Down
19 changes: 5 additions & 14 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "numtoa.h"
#include "evhtp/evhtp.h"

#include "log.h"

/**
* @brief structure containing a single callback and configuration
*
Expand Down Expand Up @@ -60,17 +62,6 @@ struct evhtp_callback_s {

TAILQ_HEAD(evhtp_callbacks_s, evhtp_callback_s);


#ifdef EVHTP_DEBUG
static void
htp_log_connection(evhtp_connection_t * c)
{
htp_log_debug("connection = %p\n", c);
htp_log_debug("request = %p\n", c->request);
}

#endif

#define SET_BIT(VAR, FLAG) VAR |= FLAG
#define UNSET_BIT(VAR, FLAG) VAR &= ~FLAG

Expand Down Expand Up @@ -2106,7 +2097,7 @@ htp__connection_readcb_(struct bufferevent * bev, void * arg)

nread = htparser_run(c->parser, &request_psets, (const char *)buf, avail);

htp_log_debug("nread = %zu", nread);
log_debug("nread = %zu", nread);

if (!(c->flags & EVHTP_CONN_FLAG_OWNER))
{
Expand Down Expand Up @@ -2398,7 +2389,7 @@ htp__connection_accept_(struct event_base * evbase, evhtp_connection_t * connect
connection->sock,
connection->htp->bev_flags);

htp_log_debug("enter sock=%d\n", connection->sock);
log_debug("enter sock=%d\n", connection->sock);

#ifndef EVHTP_DISABLE_SSL
end:
Expand Down Expand Up @@ -2564,7 +2555,7 @@ htp__accept_cb_(struct evconnlistener * serv, int fd, struct sockaddr * s, int s
return;
}

htp_log_debug("fd = %d, conn = %p", fd, connection);
log_debug("fd = %d, conn = %p", fd, connection);

connection->saddr = htp__malloc_(sl);
evhtp_alloc_assert(connection->saddr);
Expand Down
2 changes: 2 additions & 0 deletions include/evhtp/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {
#undef EVHTP_DISABLE_REGEX
#undef EVHTP_DISABLE_SSL
#undef EVHTP_DISABLE_EVTHR
#undef EVHTP_DEBUG

#cmakedefine EVHTP_DISABLE_EVTHR
#cmakedefine EVHTP_DISABLE_REGEX
Expand All @@ -40,6 +41,7 @@ extern "C" {
#cmakedefine EVHTP_USE_TCMALLOC
#cmakedefine EVHTP_USE_JEMALLOC
#cmakedefine EVHTP_USE_TCMALLOC
#cmakedefine EVHTP_DEBUG

#ifdef EVHTP_USE_TCMALLOC
#include <google/tcmalloc.h>
Expand Down
20 changes: 0 additions & 20 deletions include/evhtp/evhtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,6 @@
extern "C" {
#endif

#ifdef EVHTP_DEBUG
#define __QUOTE(x) # x
#define _QUOTE(x) __QUOTE(x)
#define htp_debug_strlen(x) strlen(x)

#define htp_log_debug(fmt, ...) do { \
time_t t = time(NULL); \
struct tm * dm = localtime(&t); \
\
fprintf(stdout, "[%02d:%02d:%02d] evhtp.c:[" _QUOTE(__LINE__) "]\t %-26s: " \
fmt "\n", dm->tm_hour, dm->tm_min, dm->tm_sec, __func__, ## __VA_ARGS__); \
fflush(stdout); \
} while (0)

#else
#define htp_debug_strlen(x) 0
#define htp_log_debug(fmt, ...) do { \
} while (0)
#endif

struct evhtp_callback_s;
struct evhtp_callbacks_s;

Expand Down
49 changes: 49 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <stdio.h>
#include <errno.h>
#include <string.h>

#define __FILENAME__ \
(strrchr(__FILE__, '/') ? \
strrchr(__FILE__, '/') + 1 : __FILE__)

#define clean_errno() \
(errno == 0 ? "None" : strerror(errno))


#if !defined(EVHTP_DEBUG)
/* compile with all debug messages removed */
#define log_debug(M, ...)
#else
#define log_debug(M, ...) \
fprintf(stderr, "\33[34mDEBUG\33[39m "M " \33[90m at %s (%s:%d) \33[39m\n", ## \
__VA_ARGS__, \
__func__, \
__FILE__, \
__LINE__)
#endif


#define log_error(M, ...) \
fprintf(stderr, "\33[31mERR\33[39m " M " \33[90m at %s (%s:%d) \33[94merrno: %s\33[39m\n", ## \
__VA_ARGS__, \
__func__, \
__FILE__, \
__LINE__, \
clean_errno())

#define log_warn(M, ...) \
fprintf(stderr, "\33[91mWARN\33[39m " M " \33[90m at %s (%s:%d) \33[94merrno: %s\33[39m\n", ## \
__VA_ARGS__, \
__func__, \
__FILE__, \
__LINE__, \
clean_errno())

#define log_info(M, ...) \
fprintf(stderr, "\33[32mINFO\33[39m " M " \33[90m at %s (%s:%d) \33[39m\n", ## \
__VA_ARGS__, \
__func__, \
__FILENAME__, \
__LINE__)
Loading

0 comments on commit f20e5cf

Please sign in to comment.