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

Commit

Permalink
access-log API finalization / cleanups and overhauls
Browse files Browse the repository at this point in the history
- the request-logging API has changed in order to closer match
  other APIs

   The following variable definitions are treated as special in the
   format while all other characters are treated as-is.
     $ua     - the user-agent
     $path   - the requested path
     $rhost  - the IP address of the request
     $meth   - the HTTP method
     $ts     - timestamp
     $proto  - HTTP proto version
     $status - the return status
     $ref    - the HTTP referrer
     $host   - either the vhost (if defined) or the value of the Host: header
   evhtp_log_new("<format>");

   This function logs a request using the compiled format above to a
   FILE
   evhtp_log_request_f(fmt, request, FILE);

- Moved debug logging functions into internal.h and removed log.h
- Moved request logging into include/evhtp/log.h and src/log.c
- Removed assert.h and moved functions into internal.h
- Updated CmakeLists to reflect various changes and add more checks.
  • Loading branch information
NathanFrench committed Aug 9, 2018
1 parent ec75536 commit 5f74f32
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 493 deletions.
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ check_include_files(stdlib.h HAVE_STDLIB_H)
check_include_files(string.h HAVE_STRING_H)
check_include_files(stdint.h HAVE_STDINT_H)
check_include_files(errno.h HAVE_ERRNO_H)
check_include_files(signal.h HAVE_SIGNAL_H)
check_include_files(strings.h HAVE_STRINGS_H)
check_include_files(inttypes.h HAVE_INTTYPES_H)
check_include_files(stdbool.h HAVE_STDBOOL_H)
check_include_files(limits.h HAVE_LIMITS_H)

check_include_files(stddef.h HAVE_STDDEF_H)
check_include_files(ctype.h HAVE_CTYPE_H)
check_include_files(unistd.h HAVE_UNISTD_H)
check_include_files(stdarg.h HAVE_STDARG_PROTOTYPES)
check_include_files(sys/tree.h HAVE_SYS_TREE)
Expand All @@ -48,17 +51,17 @@ set(LIBEVHTP_SOURCE_FILES
evhtp.c
numtoa.c
parser.c
logutils.c)
log.c)

find_package(LibEvent REQUIRED)
list(APPEND LIBEVHTP_EXTERNAL_LIBS ${LIBEVENT_LIBRARIES})
list(APPEND LIBEVHTP_EXTERNAL_INCLUDES ${LIBEVENT_INCLUDE_DIRS})

list(APPEND package_deps LibEvent)

set(evhtp_dir_headers
"include/evhtp/evhtp.h"
"include/evhtp/parser.h"
"include/evhtp/assert.h")
"include/evhtp/log.h")

if(NOT EVHTP_DISABLE_SSL)
find_package(OpenSSL)
Expand Down
2 changes: 0 additions & 2 deletions evhtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
#include "numtoa.h"
#include "evhtp/evhtp.h"

#include "log.h"

/**
* @brief structure containing a single callback and configuration
*
Expand Down
7 changes: 3 additions & 4 deletions examples/example_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
#include <evhtp.h>
#include <unistd.h>

#include "../log.h"
#include "./eutils.h"
#include "internal.h"
#include "evhtp/evhtp.h"
#include "evhtp/logutils.h"
#include "evhtp/log.h"

static void
process_request_(evhtp_request_t * req, void * arg)
{
(void)arg;

htp_log_request(arg, stderr, req);
evhtp_log_request_f(arg, req, stderr);
evhtp_send_reply(req, EVHTP_RES_OK);
}

Expand All @@ -32,7 +31,7 @@ main(int argc, char ** argv)

evbase = event_base_new();
htp = evhtp_new(evbase, NULL);
log = htp_logutil_new("$rhost $host \"$ua\" [$ts] \"$meth $path HTTP/$proto\" $status");
log = evhtp_log_new("$rhost $host '$ua' [$ts] '$meth $path HTTP/$proto' $status");

evhtp_set_cb(htp, "/", process_request_, log);
evhtp_enable_flag(htp, EVHTP_FLAG_ENABLE_ALL);
Expand Down
1 change: 0 additions & 1 deletion examples/example_chunked.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdint.h>
#include <errno.h>

#include "../log.h"
#include "./eutils.h"
#include "internal.h"
#include "evhtp/evhtp.h"
Expand Down
1 change: 0 additions & 1 deletion examples/example_pause.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <stdint.h>
#include <errno.h>

#include "../log.h"
#include "internal.h"
#include "evhtp/evhtp.h"
#include "./eutils.h"
Expand Down
1 change: 0 additions & 1 deletion examples/example_request_fini.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <stdio.h>
#include <stdlib.h>

#include "../log.h"
#include "./eutils.h"
#include "internal.h"
#include "evhtp/evhtp.h"
Expand Down
1 change: 0 additions & 1 deletion examples/example_vhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <errno.h>

#include "internal.h"
#include "../log.h"
#include "evhtp/evhtp.h"
#include "./eutils.h"

Expand Down
1 change: 0 additions & 1 deletion examples/https/example_https_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <unistd.h>
#include <getopt.h>

#include "../log.h"
#include "internal.h"
#include "evhtp/evhtp.h"

Expand Down
1 change: 0 additions & 1 deletion examples/https/example_https_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <unistd.h>
#include <getopt.h>

#include "../log.h"
#include "internal.h"
#include "evhtp/evhtp.h"

Expand Down
46 changes: 0 additions & 46 deletions include/evhtp/assert.h

This file was deleted.

1 change: 0 additions & 1 deletion include/evhtp/evhtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/

#include <evhtp/config.h>
#include <evhtp/assert.h>

#ifndef __EVHTP__H__
#define __EVHTP__H__
Expand Down
35 changes: 35 additions & 0 deletions include/evhtp/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#ifndef __EVHTP_LOG_H__
#define __EVHTP_LOG_H__


/**
* @brief create a new request-logging context with the format string `format`
* @note The following variable definitions are treated as special in the
* format:
* $ua - the user-agent
* $path - the requested path
* $rhost - the IP address of the request
* $meth - the HTTP method
* $ts - timestamp
* $proto - HTTP proto version
* $status - the return status
* $ref - the HTTP referrer
* $host - either the vhost (if defined) or the value of the Host: header
* All other characters are treated as-is.
*
* @param format - format string (see above)
*/
EVHTP_EXPORT void * evhtp_log_new(const char * format);


/**
* @brief log a HTTP request to a FILE using a compiled format.
*
* @param log - The compiled format (see evhtp_log_new)
* @param request
* @param fp
*/
EVHTP_EXPORT void evhtp_log_request_f(void * log, evhtp_request_t * request, FILE * fp);

#endif

8 changes: 0 additions & 8 deletions include/evhtp/logutils.h

This file was deleted.

97 changes: 97 additions & 0 deletions include/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,103 @@ extern "C" {
#endif


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

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

#define __log_debug_color(X) "[\x1b[1;36m" X "\x1b[0;39m]"
#define __log_error_color(X) "[\x1b[1;31m" X "\x1b[0;39m]"
#define __log_warn_color(X) "[\x1b[1;33m" X "\x1b[0;39m]"
#define __log_info_color(X) "[\x1b[32m" X "\x1b[0;39m]"
#define __log_func_color(X) "\x1b[33m" X "\x1b[39m"
#define __log_args_color(X) "\x1b[94m" X "\x1b[39m"
#define __log_errno_color(X) "\x1b[35m" X "\x1b[39m"


#if !defined(EVHTP_DEBUG)
/* compile with all debug messages removed */
#define log_debug(M, ...)
#else
#define log_debug(M, ...) \
fprintf(stderr, __log_debug_color("DEBUG") " " \
__log_func_color("%s/%s:%-9d") \
__log_args_color(M) \
"\n", \
__FILENAME__, __FUNCTION__, __LINE__, ## __VA_ARGS__)
#endif

#define log_error(M, ...) \
fprintf(stderr, __log_error_color("ERROR") " " \
__log_func_color("%s:%-9d") \
__log_args_color(M) \
" :: " \
__log_errno_color("(errno: %s)") \
"\n", \
__FILENAME__, __LINE__, ## __VA_ARGS__, clean_errno())


#define log_warn(M, ...) \
fprintf(stderr, __log_warn_color("WARN") " " \
__log_func_color("%s:%-9d") \
__log_args_color(M) \
" :: " \
__log_errno_color("(errno: %s)") \
"\n", \
__FILENAME__, __LINE__, ## __VA_ARGS__, clean_errno())

#define log_info(M, ...) \
fprintf(stderr, __log_info_color("INFO") " " \
__log_func_color("%4s:%-9d") \
__log_args_color(M) "\n", \
__FILENAME__, __LINE__, ## __VA_ARGS__)

#define evhtp_assert(x) \
do { \
if (evhtp_unlikely(!(x))) { \
fprintf(stderr, "Assertion failed: %s (%s:%s:%d)\n", # x, \
__func__, __FILE__, __LINE__); \
fflush(stderr); \
abort(); \
} \
} while (0)

#define evhtp_alloc_assert(x) \
do { \
if (evhtp_unlikely(!x)) { \
fprintf(stderr, "Out of memory (%s:%s:%d)\n", \
__func__, __FILE__, __LINE__); \
fflush(stderr); \
abort(); \
} \
} while (0)

#define evhtp_assert_fmt(x, fmt, ...) \
do { \
if (evhtp_unlikely(!(x))) { \
fprintf(stderr, "Assertion failed: %s (%s:%s:%d) " fmt "\n", \
# x, __func__, __FILE__, __LINE__, __VA_ARGS__); \
fflush(stderr); \
abort(); \
} \
} while (0)

#define evhtp_errno_assert(x) \
do { \
if (evhtp_unlikely(!(x))) { \
fprintf(stderr, "%s [%d] (%s:%s:%d)\n", \
strerror(errno), errno, \
__func__, __FILE__, __LINE__); \
fflush(stderr); \
abort(); \
} \
} while (0)




#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 5f74f32

Please sign in to comment.