[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [obfsproxy/master] cleanups and tweaks on logging



commit 01c29282a94241121c0890279e007becfe6f99bd
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Fri Jun 10 15:26:19 2011 -0400

    cleanups and tweaks on logging
    
    The macro syntax originally used here is a gcc extension; let's also
    support the c99 version and a fallback to declaring this stuff as
    function.
    
    Also, let's use the __attribute__((format)) feature in GCC so that
    we can learn about type mismatches earlier.
    
    Also, make more functions const.
---
 src/util.c |   66 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
 src/util.h |   32 +++++++++++++++++++++++-----
 2 files changed, 79 insertions(+), 19 deletions(-)

diff --git a/src/util.c b/src/util.c
index 6ff337f..976dd59 100644
--- a/src/util.c
+++ b/src/util.c
@@ -24,9 +24,9 @@ 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(char *string);
-static int open_and_set_obfsproxy_logfile(char *filename);
-
+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 *************************/
 
@@ -182,7 +182,7 @@ sev_to_string(int severity)
 /** If 'string' is a valid log severity, return the corresponding
  * numeric value.  Otherwise, return -1. */
 static int
-string_to_sev(char *string)
+string_to_sev(const char *string)
 {
   if (!strcasecmp(string, "warn"))
     return LOG_SEV_WARN;
@@ -212,7 +212,7 @@ sev_is_valid(int severity)
    It returns 1 on success and -1 on fail.
 */
 int
-log_set_method(int method, char *filename)
+log_set_method(int method, const char *filename)
 {
   
   logging_method = method;
@@ -230,7 +230,7 @@ log_set_method(int method, char *filename)
    On success it returns 1, on fail it returns -1.
 */
 static int
-open_and_set_obfsproxy_logfile(char *filename)
+open_and_set_obfsproxy_logfile(const char *filename)
 {
   if (!filename)
     return -1;
@@ -291,7 +291,7 @@ compose_logfile_prologue(char *buf, size_t buflen)
    not a valid severity, it returns -1.
 */
 int
-log_set_min_severity(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);
@@ -301,7 +301,7 @@ log_set_min_severity(char* sev_string) {
   return 1;
 }
 
-/** 
+/**
     Logging function of obfsproxy.
     Don't call this directly; use the log_* macros defined in util.h
     instead.
@@ -313,6 +313,18 @@ log_set_min_severity(char* sev_string) {
 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)
+{
   assert(sev_is_valid(severity));
 
   if (logging_method == LOG_METHOD_NULL)
@@ -328,9 +340,6 @@ log_fn(int severity, const char *format, ...)
 
   size_t buflen = MAX_LOG_ENTRY-2;
 
-  va_list ap;
-  va_start(ap,format);
-  
   r = obfs_snprintf(buf, buflen, "[%s] ", sev_to_string(severity));
   if (r < 0)
     n = strlen(buf);
@@ -351,8 +360,6 @@ log_fn(int severity, const char *format, ...)
 
   buf[n]='\n';
   buf[n+1]='\0';
-  
-  va_end(ap);
 
   if (logging_method == LOG_METHOD_STDOUT)
     fprintf(stdout, "%s", buf);
@@ -363,3 +370,36 @@ log_fn(int severity, const char *format, ...)
   } else
     assert(0);
 }
+
+#ifdef NEED_LOG_WRAPPERS
+void
+log_info(const char *format, ...)
+{
+  va_list ap;
+  va_start(ap,format);
+
+  logv(LOG_SEV_INFO, format, ap);
+
+  va_end(ap);
+}
+void
+log_warn(const char *format, ...)
+{
+  va_list ap;
+  va_start(ap,format);
+
+  logv(LOG_SEV_WARN, format, ap);
+
+  va_end(ap);
+}
+void
+log_debug(const char *format, ...)
+{
+  va_list ap;
+  va_start(ap,format);
+
+  logv(LOG_SEV_DEBUG, format, ap);
+
+  va_end(ap);
+}
+#endif
diff --git a/src/util.h b/src/util.h
index e62e730..af3cd88 100644
--- a/src/util.h
+++ b/src/util.h
@@ -43,21 +43,41 @@ int init_evdns_base(struct event_base *base);
 /** Any size_t larger than this amount is likely to be an underflow. */
 #define SIZE_T_CEILING  ((size_t)(SSIZE_T_MAX-16))
 
-int obfs_vsnprintf(char *str, size_t size, 
+#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, 
-                  const char *format, ...);
+int obfs_snprintf(char *str, size_t size,
+                  const char *format, ...)
+  __attribute__((format(printf, 3, 4)));
 
 /***** Logging subsystem stuff. *****/
 
-void log_fn(int severity, const char *format, ...);
-int log_set_method(int method, char *filename);
-int log_set_min_severity(char* sev_string);
+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 */
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits