From 64bfebde76d568808b6fa8a8d09b4b5afe13dc15 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Fri, 19 Aug 2011 01:53:52 +0200 Subject: [PATCH] Implement cleaner logging approach. --- src/log.c | 16 +++++++++++----- src/log.h | 22 +++++++++------------- src/tlsproxy.c | 8 ++++---- src/verify.c | 12 +++++++++--- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/log.c b/src/log.c index dd7c90c..a703c1c 100644 --- a/src/log.c +++ b/src/log.c @@ -26,7 +26,7 @@ #include -void log_message(int level, const char *format, ...) { +void log_message(int level, const char *file, int line, const char *format, ...) { va_list ap; const char *level_string; @@ -35,13 +35,19 @@ void log_message(int level, const char *format, ...) { } switch (level) { - case LOG_ERROR: level_string = "ERROR"; break; - case LOG_WARNING: level_string = "WARN "; break; - case LOG_DEBUG: level_string = "DEBUG"; break; - default: level_string = "UNKNOWN"; + case LOG_ERROR_LEVEL: level_string = "ERROR"; break; + case LOG_WARNING_LEVEL: level_string = "WARN "; break; + case LOG_DEBUG_LEVEL: level_string = "DEBUG"; break; + default: level_string = "UNKNOWN"; } va_start(ap, format); +#if DEBUG + fprintf(stdout, "%-12s:%-3d ", file, line); +#else + (void)file; + (void)line; +#endif fprintf(stdout, "[%s] [%d] ", level_string, (int)pthread_self()); vfprintf(stdout, format, ap); fprintf(stdout, "\n"); diff --git a/src/log.h b/src/log.h index 041f453..54ce597 100644 --- a/src/log.h +++ b/src/log.h @@ -20,28 +20,24 @@ #ifndef LOG_H #define LOG_H +/* Log constants to be used with LOG() and LOG_PERROR() macros. */ +#define LOG_ERROR LOG_ERROR_LEVEL, __FILE__, __LINE__ +#define LOG_WARNING LOG_WARNING_LEVEL, __FILE__, __LINE__ +#define LOG_DEBUG LOG_DEBUG_LEVEL, __FILE__, __LINE__ + /* Log level constants. */ -#define LOG_ERROR 0 -#define LOG_WARNING 1 -#define LOG_DEBUG 2 +#define LOG_ERROR_LEVEL 0 +#define LOG_WARNING_LEVEL 1 +#define LOG_DEBUG_LEVEL 2 -/* Helper macro for LOG/LOG_PERROR. Print file/line number if compiled with - * debug output. */ -#ifdef DEBUG -#define LOG_PRINT_LOCATION fprintf(stdout, "%-12s:%-3d ", __FILE__, __LINE__); -#else -#define LOG_PRINT_LOCATION -#endif /* Call log_message() and print current file and line number. */ #define LOG \ - LOG_PRINT_LOCATION \ log_message /* perror() replacement with debug level support. */ #define LOG_PERROR(level, message) \ - LOG_PRINT_LOCATION \ log_message(level, "%s: %s", message, strerror(errno)) -void log_message(int level, const char *format, ...); +void log_message(int level, const char *file, int line, const char *format, ...); #endif diff --git a/src/tlsproxy.c b/src/tlsproxy.c index 625ed08..3fa03e3 100644 --- a/src/tlsproxy.c +++ b/src/tlsproxy.c @@ -169,7 +169,7 @@ int main(int argc, char **argv) { } /* Fast rebinding for debug mode, could cause invalid packets. */ - if (LOG_DEBUG <= global_log_level) { + if (LOG_DEBUG_LEVEL <= global_log_level) { int socket_option = 1; setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &socket_option, sizeof(socket_option)); @@ -197,7 +197,7 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } - if (LOG_DEBUG <= global_log_level) { + if (LOG_DEBUG_LEVEL <= global_log_level) { printf("Listening for connections on port %d.\n", port); if (NULL != global_proxy_host && NULL != global_proxy_port) { @@ -266,9 +266,9 @@ static void parse_arguments(int argc, char **argv) { /* Default values. */ thread_count = 10; #ifdef DEBUG - global_log_level = LOG_DEBUG; + global_log_level = LOG_DEBUG_LEVEL; #else - global_log_level = LOG_WARNING; + global_log_level = LOG_WARNING_LEVEL; #endif global_passthrough_unknown = 0; diff --git a/src/verify.c b/src/verify.c index 8328432..5248dd6 100644 --- a/src/verify.c +++ b/src/verify.c @@ -176,9 +176,15 @@ int server_certificate_path(FILE **file, const char *hostname, /* Open the stored certificate file. */ *file = fopen(path, "rb"); if (NULL == *file) { - LOG(global_passthrough_unknown ? LOG_DEBUG : LOG_WARNING, - "server_certificate_path(): failed to open '%s': %s", - path, strerror(errno)); + if (global_passthrough_unknown) { + LOG(LOG_DEBUG, + "server_certificate_path(): failed to open '%s': %s", + path, strerror(errno)); + } else { + LOG(LOG_WARNING, + "server_certificate_path(): failed to open '%s': %s", + path, strerror(errno)); + } /* Couldn't open the file, special case. */ return -2; } -- 2.44.1