From: Simon Ruderich Date: Thu, 10 Mar 2011 22:17:49 +0000 (+0100) Subject: src/: Move log related functions/defines to log.[hc]. X-Git-Tag: 0.1~19 X-Git-Url: https://ruderich.org/simon/gitweb/?a=commitdiff_plain;h=8db64b0eebeaa80e46b2df40b91af741e533e7bc;p=tlsproxy%2Ftlsproxy.git src/: Move log related functions/defines to log.[hc]. --- diff --git a/src/Makefile.am b/src/Makefile.am index d997f84..837f651 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,2 +1,6 @@ bin_PROGRAMS = tlsproxy -tlsproxy_SOURCES = tlsproxy.c tlsproxy.h sem.h sem.c connection.h connection.c +tlsproxy_SOURCES = \ + tlsproxy.c tlsproxy.h \ + sem.h sem.c \ + connection.h connection.c \ + log.h log.c diff --git a/src/connection.c b/src/connection.c index 8e023fe..7ea6eb2 100644 --- a/src/connection.c +++ b/src/connection.c @@ -28,10 +28,6 @@ #include /* errno */ #include -/* va_*() */ -#include -/* pthread_*() */ -#include /* Maximum line of a HTTP request line. Longer request lines are aborted with @@ -43,22 +39,6 @@ #define PROXY_CA_FILE "proxy-ca.pem" #define PROXY_KEY_FILE "proxy-key.pem" -/* Helper macro for LOG/LOG_PERROR. Print file/line number if compiled with - * debug output. */ -#ifdef DEBUG -#define LOG_PRINT_LOCATION fprintf(stdout, "%s:%-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)) - static int initialize_tls_session_client(int peer_socket, const char *hostname, @@ -86,8 +66,6 @@ static int connect_to_host(const char *hostname, const char *port); static int parse_request(const char *buffer, char *host, char *port, int *version_minor); -static void log_message(int level, const char *format, ...); - void handle_connection(int client_socket) { int server_socket; @@ -736,26 +714,3 @@ static int parse_request(const char *request, char *host, char *port, return 0; } - - -static void log_message(int level, const char *format, ...) { - va_list ap; - const char *level_string; - - if (global_log_level < level) { - return; - } - - 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"; - } - - va_start(ap, format); - fprintf(stdout, "[%s] [%d] ", level_string, (int)pthread_self()); - vfprintf(stdout, format, ap); - fprintf(stdout, "\n"); - va_end(ap); -} diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..dd7c90c --- /dev/null +++ b/src/log.c @@ -0,0 +1,49 @@ +/* + * Log related functions/defines. + * + * Copyright (C) 2011 Simon Ruderich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "tlsproxy.h" +#include "log.h" + +/* va_*() */ +#include +/* pthread_*() */ +#include + + +void log_message(int level, const char *format, ...) { + va_list ap; + const char *level_string; + + if (global_log_level < level) { + return; + } + + 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"; + } + + va_start(ap, format); + fprintf(stdout, "[%s] [%d] ", level_string, (int)pthread_self()); + vfprintf(stdout, format, ap); + fprintf(stdout, "\n"); + va_end(ap); +} diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..e129e9f --- /dev/null +++ b/src/log.h @@ -0,0 +1,47 @@ +/* + * Log related functions/defines. + * + * Copyright (C) 2011 Simon Ruderich + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef LOG_H +#define LOG_H + +/* Log level constants. */ +#define LOG_ERROR 0 +#define LOG_WARNING 1 +#define LOG_DEBUG 2 + +/* Helper macro for LOG/LOG_PERROR. Print file/line number if compiled with + * debug output. */ +#ifdef DEBUG +#define LOG_PRINT_LOCATION fprintf(stdout, "%s:%-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, ...); + +#endif diff --git a/src/tlsproxy.c b/src/tlsproxy.c index 0e09568..85ca776 100644 --- a/src/tlsproxy.c +++ b/src/tlsproxy.c @@ -48,6 +48,14 @@ GCRY_THREAD_OPTION_PTHREAD_IMPL; #define DH_SIZE 1024 +/* For gnutls_*() functions. */ +#define GNUTLS_ERROR_EXIT(error, message) \ + if (GNUTLS_E_SUCCESS != error) { \ + fprintf(stderr, "%s: %s\n", message, gnutls_strerror(error)); \ + exit(EXIT_FAILURE); \ + } + + /* Server should shut down. Set by SIGINT handler. */ static volatile int done; diff --git a/src/tlsproxy.h b/src/tlsproxy.h index b2d690e..8ade4e8 100644 --- a/src/tlsproxy.h +++ b/src/tlsproxy.h @@ -29,20 +29,10 @@ /* GnuTLS */ #include +#include "log.h" -/* Log level constants. */ -#define LOG_ERROR 0 -#define LOG_WARNING 1 -#define LOG_DEBUG 2 -/* Macros for shorter error handling. */ -#define GNUTLS_ERROR_EXIT(error, message) \ - if (GNUTLS_E_SUCCESS != error) { \ - fprintf(stderr, "%s: %s\n", message, gnutls_strerror(error)); \ - exit(EXIT_FAILURE); \ - } - /* Proxy hostname and port if specified on the command line. */ char *global_proxy_host;