commit 97e0e668dde5168c926722d367cc86bef2fb38c3 Author: Zack Weinberg zackw@panix.com Date: Fri Jul 8 15:38:16 2011 -0700
Clean up the logging API.
Not so many ifdefs, and now uses the standard unixy -1/error, 0/success return value convention where that makes sense, or no return value at all for functions that shouldn't ever fail. --- src/main.c | 3 +- src/util.c | 122 +++++++++++++++++++++-------------------------------------- src/util.h | 60 +++++++++++++----------------- 3 files changed, 71 insertions(+), 114 deletions(-)
diff --git a/src/main.c b/src/main.c index a8347c2..0ba1f3f 100644 --- a/src/main.c +++ b/src/main.c @@ -362,8 +362,7 @@ main(int argc, const char **argv)
log_info("Exiting.");
- if (close_obfsproxy_logfile() < 0) - printf("Failed closing logfile!\n"); + close_obfsproxy_logfile();
/* We are exiting. Clean everything. */ for (h=0;h<n_listeners;h++) diff --git a/src/util.c b/src/util.c index ce9a042..9ac2260 100644 --- a/src/util.c +++ b/src/util.c @@ -25,14 +25,6 @@ /** Any size_t larger than this amount is likely to be an underflow. */ #define SIZE_T_CEILING (SIZE_MAX/2 - 16)
-static const char *sev_to_string(int severity); -static int sev_is_valid(int severity); -static int write_logfile_prologue(int fd); -static int compose_logfile_prologue(char *buf, size_t buflen); -static int string_to_sev(const char *string); -static int open_and_set_obfsproxy_logfile(const char *filename); -static void logv(int severity, const char *format, va_list ap); - /************************ Obfsproxy Network Routines *************************/
int @@ -146,7 +138,7 @@ obfs_vsnprintf(char *str, size_t size, const char *format, va_list args) }
/************************ Logging Subsystem *************************/ -/** The code of this section was to a great extend shamelessly copied +/** The code of this section was to a great extent shamelessly copied off tor. It's basicaly a stripped down version of tor's logging system. Thank you tor. */
@@ -157,6 +149,12 @@ obfs_vsnprintf(char *str, size_t size, const char *format, va_list args) /* strlen(TRUNCATED_STR) */ #define TRUNCATED_STR_LEN 14
+/** Logging severities */ + +#define LOG_SEV_WARN 3 +#define LOG_SEV_INFO 2 +#define LOG_SEV_DEBUG 1 + /* logging method */ static int logging_method=LOG_METHOD_STDOUT; /* minimum logging severity */ @@ -199,31 +197,12 @@ string_to_sev(const char *string) static int sev_is_valid(int severity) { - return (severity == LOG_SEV_WARN || - severity == LOG_SEV_INFO || + return (severity == LOG_SEV_WARN || + severity == LOG_SEV_INFO || severity == LOG_SEV_DEBUG); }
/** - Sets the global logging 'method' and also sets and open the logfile - 'filename' in case we want to log into a file. - It returns 1 on success and -1 on fail. -*/ -int -log_set_method(int method, const char *filename) -{ - - logging_method = method; - if (method == LOG_METHOD_FILE) { - if (open_and_set_obfsproxy_logfile(filename) < 0) - return -1; - if (write_logfile_prologue(logging_logfile) < 0) - return -1; - } - return 1; -} - -/** Helper: Opens 'filename' and sets it as the obfsproxy logfile. On success it returns 1, on fail it returns -1. */ @@ -232,56 +211,55 @@ open_and_set_obfsproxy_logfile(const char *filename) { if (!filename) return -1; - logging_logfile = open(filename, + logging_logfile = open(filename, O_WRONLY|O_CREAT|O_APPEND, 0644); if (logging_logfile < 0) return -1; - return 1; + return 0; }
/** Closes the obfsproxy logfile if it exists. - Returns 0 on success or if we weren't using a logfile (that's - close()'s success return value) and -1 on failure. + Ignores errors. */ -int +void close_obfsproxy_logfile(void) { - if (logging_logfile < 0) /* no logfile. */ - return 0; - else - return close(logging_logfile); + if (logging_logfile >= 0) + close(logging_logfile); }
/** - Writes a small prologue in the logfile 'fd' that mentions the - obfsproxy version and helps separate log instances. + Writes a small prologue in the logfile 'fd' to separate log + instances. */ static int -write_logfile_prologue(int logfile) { - char buf[256]; - if (compose_logfile_prologue(buf, sizeof(buf)) < 0) - return -1; - if (write(logfile, buf, strlen(buf)) < 0) +write_logfile_prologue(int logfile) +{ + static const char prologue[] = "\nBrand new obfsproxy log:\n"; + if (write(logfile, prologue, strlen(prologue)) != strlen(prologue)) return -1; - return 1; + return 0; }
-#define TEMP_PROLOGUE "\nBrand new obfsproxy log:\n" /** - Helper: Composes the logfile prologue. + Sets the global logging 'method' and also sets and open the logfile + 'filename' in case we want to log into a file. + It returns 1 on success and -1 on fail. */ -static int -compose_logfile_prologue(char *buf, size_t buflen) -{ - if (obfs_snprintf(buf, buflen, TEMP_PROLOGUE) < 0) { - log_warn("Logfile prologue couldn't be written."); - return -1; +int +log_set_method(int method, const char *filename) +{ + logging_method = method; + if (method == LOG_METHOD_FILE) { + if (open_and_set_obfsproxy_logfile(filename) < 0) + return -1; + if (write_logfile_prologue(logging_logfile) < 0) + return -1; } - return 1; + return 0; } -#undef TEMP_PROLOGUE
/** Sets the minimum logging severity of obfsproxy to the severity @@ -289,37 +267,23 @@ compose_logfile_prologue(char *buf, size_t buflen) not a valid severity, it returns -1. */ int -log_set_min_severity(const char* sev_string) { +log_set_min_severity(const char* sev_string) +{ int severity = string_to_sev(sev_string); if (!sev_is_valid(severity)) { log_warn("Severity '%s' makes no sense.", sev_string); return -1; } logging_min_sev = severity; - return 1; + return 0; }
/** - Logging function of obfsproxy. - Don't call this directly; use the log_* macros defined in util.h - instead. - - It accepts a logging 'severity' and a 'format' string and logs the + Logging worker function. + Accepts a logging 'severity' and a 'format' string and logs the message in 'format' according to the configured obfsproxy minimum logging severity and logging method. */ -void -log_fn(int severity, const char *format, ...) -{ - - va_list ap; - va_start(ap,format); - - logv(severity, format, ap); - - va_end(ap); -} - static void logv(int severity, const char *format, va_list ap) { @@ -369,7 +333,8 @@ logv(int severity, const char *format, va_list ap) assert(0); }
-#ifdef NEED_LOG_WRAPPERS +/**** Public logging API. ****/ + void log_info(const char *format, ...) { @@ -380,6 +345,7 @@ log_info(const char *format, ...)
va_end(ap); } + void log_warn(const char *format, ...) { @@ -390,6 +356,7 @@ log_warn(const char *format, ...)
va_end(ap); } + void log_debug(const char *format, ...) { @@ -400,4 +367,3 @@ log_debug(const char *format, ...)
va_end(ap); } -#endif diff --git a/src/util.h b/src/util.h index 9a8a577..f52f335 100644 --- a/src/util.h +++ b/src/util.h @@ -8,6 +8,10 @@ #include <stdarg.h> /* for va_list */ #include <stddef.h> /* for size_t etc */
+#ifndef __GNUC__ +#define __attribute__(x) /* nothing */ +#endif + struct sockaddr; struct event_base; struct evdns_base; @@ -25,10 +29,6 @@ int init_evdns_base(struct event_base *base);
/***** String functions stuff. *****/
-#ifndef __GNUC__ -#define __attribute__(x) -#endif - int obfs_vsnprintf(char *str, size_t size, const char *format, va_list args); int obfs_snprintf(char *str, size_t size, @@ -37,30 +37,6 @@ int obfs_snprintf(char *str, size_t size,
/***** Logging subsystem stuff. *****/
-void log_fn(int severity, const char *format, ...) - __attribute__((format(printf, 2, 3))); -int log_set_method(int method, const char *filename); -int log_set_min_severity(const char* sev_string); -int close_obfsproxy_logfile(void); - -#ifdef __GNUC__ -#define log_info(args...) log_fn(LOG_SEV_INFO, args) -#define log_warn(args...) log_fn(LOG_SEV_WARN, args) -#define log_debug(args...) log_fn(LOG_SEV_DEBUG, args) -#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L -#define log_info(...) log_fn(LOG_SEV_INFO, __VA_ARGS__) -#define log_warn(...) log_fn(LOG_SEV_WARN, __VA_ARGS__) -#define log_debug(...) log_fn(LOG_SEV_DEBUG, __VA_ARGS__) -#else -#define NEED_LOG_WRAPPERS -void log_info(const char *format, ...) - __attribute__((format(printf, 1, 2))); -void log_warn(const char *format, ...) - __attribute__((format(printf, 1, 2))); -void log_debug(const char *format, ...) - __attribute__((format(printf, 1, 2))); -#endif - /** Logging methods */
/** Spit log messages on stdout. */ @@ -68,17 +44,33 @@ void log_debug(const char *format, ...) /** Place log messages in a file. */ #define LOG_METHOD_FILE 2 /** We don't want no logs. */ -#define LOG_METHOD_NULL 3 +#define LOG_METHOD_NULL 3 + +/** Set the log method, and open the logfile 'filename' if appropriate. */ +int log_set_method(int method, const char *filename); + +/** Set the minimum severity that will be logged. + 'sev_string' may be "warn", "info", or "debug" (case-insensitively). */ +int log_set_min_severity(const char* sev_string); + +/** Close the logfile if it's open. Ignores errors. */ +void close_obfsproxy_logfile(void); + +/** The actual log-emitting functions */
-/** Logging severities */ +/** Warn-level severity: for messages that only appear when something + has gone wrong. */ +void log_warn(const char *format, ...) + __attribute__((format(printf, 1, 2)));
-/** Warn-level severity: for messages that only appear when something has gone wrong. */ -#define LOG_SEV_WARN 3 /** Info-level severity: for messages that should be sent to the user during normal operation. */ -#define LOG_SEV_INFO 2 +void log_info(const char *format, ...) + __attribute__((format(printf, 1, 2))); + /** Debug-level severity: for hyper-verbose messages of no interest to anybody but developers. */ -#define LOG_SEV_DEBUG 1 +void log_debug(const char *format, ...) + __attribute__((format(printf, 1, 2)));
#endif