[or-cvs] r13875: Implement domain-selection for logging. Source is documented (in tor/trunk: . src/common src/or src/tools)

nickm at seul.org nickm at seul.org
Wed Mar 5 22:31:40 UTC 2008


Author: nickm
Date: 2008-03-05 17:31:39 -0500 (Wed, 05 Mar 2008)
New Revision: 13875

Modified:
   tor/trunk/
   tor/trunk/ChangeLog
   tor/trunk/src/common/log.c
   tor/trunk/src/common/log.h
   tor/trunk/src/or/config.c
   tor/trunk/src/or/test.c
   tor/trunk/src/tools/tor-gencert.c
   tor/trunk/src/tools/tor-resolve.c
Log:
 r18630 at catbus:  nickm | 2008-03-05 17:31:33 -0500
 Implement domain-selection for logging.  Source is documented; needs documentation in manpage (maybe).  For now, see doxygen comment on parse_log_severity_config in log.c



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r18630] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2008-03-05 17:18:04 UTC (rev 13874)
+++ tor/trunk/ChangeLog	2008-03-05 22:31:39 UTC (rev 13875)
@@ -8,7 +8,13 @@
       from lodger, Karsten Loesing and Sebastian Hahn.  Fixes bug 616.
       Bugfix on 0.2.0.20-rc.
 
+  o Minor features:
+    - Allow separate log levels to be configured for different logging
+      domains.  For example, this allows one to log all notices, warnings, or
+      errors, plus all memory management messages of level debug or higher,
+      with: Log [MM] debug-err [*] notice-err file /var/log/tor.
 
+
 Changes in version 0.2.0.21-rc - 2008-03-02
   o Major bugfixes:
     - The control port should declare that it requires password auth

Modified: tor/trunk/src/common/log.c
===================================================================
--- tor/trunk/src/common/log.c	2008-03-05 17:18:04 UTC (rev 13874)
+++ tor/trunk/src/common/log.c	2008-03-05 22:31:39 UTC (rev 13875)
@@ -23,8 +23,9 @@
 #ifdef HAVE_TIME_H
 #include <time.h>
 #endif
-#include "./util.h"
-#include "./log.h"
+#include "util.h"
+#include "log.h"
+#include "container.h"
 
 #include <event.h>
 
@@ -38,13 +39,14 @@
   FILE *file; /**< Stream to receive log messages. */
   int seems_dead; /**< Boolean: true if the stream seems to be kaput. */
   int needs_close; /**< Boolean: true if the stream gets closed on shutdown. */
-  int min_loglevel; /**< Lowest severity level to send to this stream. */
-  int max_loglevel; /**< Highest severity level to send to this stream. */
   int is_temporary; /**< Boolean: close after initializing logging subsystem.*/
   int is_syslog; /**< Boolean: send messages to syslog. */
   log_callback callback; /**< If not NULL, send messages to this function. */
+  log_severity_list_t *severities; /**< DOCDOC */
 } logfile_t;
 
+static void log_free(logfile_t *victim);
+
 /** Helper: map a log severity to descriptive string. */
 static INLINE const char *
 sev_to_string(int severity)
@@ -61,7 +63,7 @@
 
 /** Helper: decide whether to include the function name in the log message. */
 static INLINE int
-should_log_function_name(uint32_t domain, int severity)
+should_log_function_name(log_domain_mask_t domain, int severity)
 {
   switch (severity) {
     case LOG_DEBUG:
@@ -69,7 +71,7 @@
       /* All debugging messages occur in interesting places. */
       return 1;
     case LOG_NOTICE:
-    case LOG_WARN:
+  case LOG_WARN:
     case LOG_ERR:
       /* We care about places where bugs occur. */
       return (domain == LD_BUG);
@@ -194,7 +196,7 @@
  */
 static INLINE char *
 format_msg(char *buf, size_t buf_len,
-           uint32_t domain, int severity, const char *funcname,
+           log_domain_mask_t domain, int severity, const char *funcname,
            const char *format, va_list ap)
 {
   size_t n;
@@ -246,8 +248,8 @@
  * message.  The actual message is derived as from tor_snprintf(format,ap).
  */
 static void
-logv(int severity, uint32_t domain, const char *funcname, const char *format,
-     va_list ap)
+logv(int severity, log_domain_mask_t domain, const char *funcname,
+     const char *format, va_list ap)
 {
   char buf[10024];
   int formatted = 0;
@@ -258,7 +260,7 @@
   LOCK_LOGS();
   lf = logfiles;
   while (lf) {
-    if (severity > lf->min_loglevel || severity < lf->max_loglevel) {
+    if (! (lf->severities->masks[SEVERITY_MASK_IDX(severity)] & domain)) {
       lf = lf->next;
       continue;
     }
@@ -302,7 +304,7 @@
 
 /** Output a message to the log. */
 void
-_log(int severity, uint32_t domain, const char *format, ...)
+_log(int severity, log_domain_mask_t domain, const char *format, ...)
 {
   va_list ap;
   if (severity > _log_global_min_severity)
@@ -315,7 +317,8 @@
 /** Output a message to the log, prefixed with a function name <b>fn</b>. */
 #ifdef __GNUC__
 void
-_log_fn(int severity, uint32_t domain, const char *fn, const char *format, ...)
+_log_fn(int severity, log_domain_mask_t domain, const char *fn,
+        const char *format, ...)
 {
   va_list ap;
   if (severity > _log_global_min_severity)
@@ -327,7 +330,7 @@
 #else
 const char *_log_fn_function_name=NULL;
 void
-_log_fn(int severity, uint32_t domain, const char *format, ...)
+_log_fn(int severity, log_domain_mask_t domain, const char *format, ...)
 {
   va_list ap;
   if (severity > _log_global_min_severity)
@@ -338,7 +341,7 @@
   _log_fn_function_name = NULL;
 }
 void
-_log_debug(uint32_t domain, const char *format, ...)
+_log_debug(log_domain_mask_t domain, const char *format, ...)
 {
   va_list ap;
   /* For GCC we do this check in the macro. */
@@ -350,7 +353,7 @@
   _log_fn_function_name = NULL;
 }
 void
-_log_info(uint32_t domain, const char *format, ...)
+_log_info(log_domain_mask_t domain, const char *format, ...)
 {
   va_list ap;
   if (LOG_INFO > _log_global_min_severity)
@@ -361,7 +364,7 @@
   _log_fn_function_name = NULL;
 }
 void
-_log_notice(uint32_t domain, const char *format, ...)
+_log_notice(log_domain_mask_t domain, const char *format, ...)
 {
   va_list ap;
   if (LOG_NOTICE > _log_global_min_severity)
@@ -372,7 +375,7 @@
   _log_fn_function_name = NULL;
 }
 void
-_log_warn(uint32_t domain, const char *format, ...)
+_log_warn(log_domain_mask_t domain, const char *format, ...)
 {
   va_list ap;
   if (LOG_WARN > _log_global_min_severity)
@@ -383,7 +386,7 @@
   _log_fn_function_name = NULL;
 }
 void
-_log_err(uint32_t domain, const char *format, ...)
+_log_err(log_domain_mask_t domain, const char *format, ...)
 {
   va_list ap;
   if (LOG_ERR > _log_global_min_severity)
@@ -395,6 +398,15 @@
 }
 #endif
 
+/** DOCDOC */
+static void
+log_free(logfile_t *victim)
+{
+  tor_free(victim->severities);
+  tor_free(victim->filename);
+  tor_free(victim);
+}
+
 /** Close all open log files, and free other static memory. */
 void
 logs_free_all(void)
@@ -408,8 +420,7 @@
     victim = next;
     next = next->next;
     close_log(victim);
-    tor_free(victim->filename);
-    tor_free(victim);
+    log_free(victim);
   }
   tor_free(appname);
 }
@@ -436,8 +447,7 @@
       return;
     tmpl->next = victim->next;
   }
-  tor_free(victim->filename);
-  tor_free(victim);
+  log_free(victim);
 }
 
 /** Helper: release system resources (but not memory) held by a single
@@ -457,18 +467,29 @@
   }
 }
 
-/** Add a log handler named <b>name</b> to send all messages of severity
- * between <b>loglevelMin</b> and <b>loglevelMax</b> (inclusive) to
- * <b>stream</b>. */
+/** DOCDOC XXXX021 remove me. */
+static log_severity_list_t *
+new_severity_list(int loglevelMin, int loglevelMax)
+{
+  log_severity_list_t *out = tor_malloc_zero(sizeof(log_severity_list_t));
+  int i;
+  for (i = loglevelMin; i >= loglevelMax; --i) {
+    out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
+  }
+  return out;
+}
+
+/** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
+ * to <b>stream</b>. Steals a reference to <b>severity</b>; the caller must
+ * not use it after calling this function. Helper: does no locking. */
 static void
-add_stream_log_impl(int loglevelMin, int loglevelMax,
+add_stream_log_impl(log_severity_list_t *severity,
                     const char *name, FILE *stream)
 {
   logfile_t *lf;
   lf = tor_malloc_zero(sizeof(logfile_t));
   lf->filename = tor_strdup(name);
-  lf->min_loglevel = loglevelMin;
-  lf->max_loglevel = loglevelMax;
+  lf->severities = severity;
   lf->file = stream;
   lf->next = logfiles;
 
@@ -476,14 +497,15 @@
   _log_global_min_severity = get_min_log_level();
 }
 
-/** Add a log handler to send all messages of severity <b>loglevel</b>
- * or higher to <b>stream</b>. */
+/** Add a log handler named <b>name</b> to send all messages in <b>severity</b>
+ * to <b>stream</b>. Steals a reference to <b>severity</b>; the caller must
+ * not use it after calling this function. */
 void
-add_stream_log(int loglevelMin, int loglevelMax,
+add_stream_log(log_severity_list_t *severity,
                const char *name, FILE *stream)
 {
   LOCK_LOGS();
-  add_stream_log_impl(loglevelMin, loglevelMax, name, stream);
+  add_stream_log_impl(severity, name, stream);
   UNLOCK_LOGS();
 }
 
@@ -502,23 +524,22 @@
 add_temp_log(void)
 {
   LOCK_LOGS();
-  add_stream_log_impl(LOG_NOTICE, LOG_ERR, "<temp>", stdout);
+  add_stream_log_impl(new_severity_list(LOG_NOTICE, LOG_ERR),
+                      "<temp>", stdout);
   logfiles->is_temporary = 1;
   UNLOCK_LOGS();
 }
 
 /**
- * Add a log handler to send messages of severity between
- * <b>logLevelmin</b> and <b>logLevelMax</b> to the function
- * <b>cb</b>.
+ * Add a log handler to send messages in <b>severity</b>
+ * to the function <b>cb</b>.
  */
 int
-add_callback_log(int loglevelMin, int loglevelMax, log_callback cb)
+add_callback_log(log_severity_list_t *severity, log_callback cb)
 {
   logfile_t *lf;
   lf = tor_malloc_zero(sizeof(logfile_t));
-  lf->min_loglevel = loglevelMin;
-  lf->max_loglevel = loglevelMax;
+  lf->severities = severity;
   lf->filename = tor_strdup("<callback>");
   lf->callback = cb;
   lf->next = logfiles;
@@ -540,8 +561,8 @@
   LOCK_LOGS();
   for (lf = logfiles; lf; lf = lf->next) {
     if (lf->callback == cb) {
-      lf->min_loglevel = loglevelMin;
-      lf->max_loglevel = loglevelMax;
+      tor_free(lf->severities);
+      lf->severities = new_severity_list(loglevelMin, loglevelMax);
     }
   }
   _log_global_min_severity = get_min_log_level();
@@ -561,8 +582,7 @@
       /* we use *p here to handle the edge case of the head of the list */
       *p = (*p)->next;
       close_log(lf);
-      tor_free(lf->filename);
-      tor_free(lf);
+      log_free(lf);
     } else {
       p = &((*p)->next);
     }
@@ -602,14 +622,14 @@
  * (by fopen).
  */
 int
-add_file_log(int loglevelMin, int loglevelMax, const char *filename)
+add_file_log(log_severity_list_t *severity, const char *filename)
 {
   FILE *f;
   logfile_t *lf;
   f = fopen(filename, "a");
   if (!f) return -1;
   LOCK_LOGS();
-  add_stream_log_impl(loglevelMin, loglevelMax, filename, f);
+  add_stream_log_impl(severity, filename, f);
   logfiles->needs_close = 1;
   lf = logfiles;
   _log_global_min_severity = get_min_log_level();
@@ -629,7 +649,7 @@
  * Add a log handler to send messages to they system log facility.
  */
 int
-add_syslog_log(int loglevelMin, int loglevelMax)
+add_syslog_log(log_severity_list_t *severity)
 {
   logfile_t *lf;
   if (syslog_count++ == 0)
@@ -637,9 +657,9 @@
     openlog("Tor", LOG_PID | LOG_NDELAY, LOGFACILITY);
 
   lf = tor_malloc_zero(sizeof(logfile_t));
-  lf->min_loglevel = loglevelMin;
+  lf->severities = severity;
   lf->filename = tor_strdup("<syslog>");
-  lf->max_loglevel = loglevelMax;
+
   lf->is_syslog = 1;
 
   LOCK_LOGS();
@@ -676,15 +696,163 @@
   return sev_to_string(level);
 }
 
+/** DOCDOC */
+static const char *domain_list[] = {
+  "GENERAL", "CRYPTO", "NET", "CONFIG", "FS", "PROTOCOL", "MM",
+  "HTTP", "APP", "CONTROL", "CIRC", "REND", "BUG", "DIR", "DIRSERV",
+  "OR", "EDGE", "ACCT", NULL
+};
+
+/** DOCDOC */
+static log_domain_mask_t
+parse_log_domain(const char *domain)
+{
+  int i;
+  for (i=0; domain_list[i]; ++i) {
+    if (!strcasecmp(domain, domain_list[i]))
+      return (1u<<i);
+  }
+  return 0;
+}
+#if 0
+/** DOCDOC */
+static const char *
+domain_to_string(log_domain_mask_t domain)
+{
+  int bit = tor_log2(domain);
+  if ((bit == 0 && domain == 0) || bit >= N_LOGGING_DOMAINS)
+    return NULL;
+  return domain_list[bit];
+}
+#endif
+
+/** Parse a log severity pattern in *<b>cfg_ptr</b>.  Advance cfg_ptr after
+ * the end of the severityPattern.  Set the value of <b>severity_out</b> to
+ * the parsed pattern.  Return 0 on success, -1 on failure.
+ *
+ * The syntax for a SeverityPattern is:
+ * <pre>
+ *   SeverityPattern = *(DomainSeverity SP)* DomainSeverity
+ *   DomainSeverity = (DomainList SP)? SeverityRange
+ *   SeverityRange = MinSeverity ("-" MaxSeverity )?
+ *   DomainList = "[" (SP? DomainSpec SP? ",") SP? DomainSpec "]"
+ *   DomainSpec = "*" | Domain | "~" Domain
+ * </pre>
+ * A missing MaxSeverity defaults to ERR.  Severities and domains are
+ * case-insensitive.  "~" indicates negation for a domain; negation happens
+ * last inside a DomainList.  Only one SeverityRange without a DomainList is
+ * allowed per line.
+ */
+int
+parse_log_severity_config(const char **cfg_ptr,
+                          log_severity_list_t *severity_out)
+{
+  const char *cfg = *cfg_ptr;
+  int got_anything = 0;
+  int got_an_unqualified_range = 0;
+  memset(severity_out, 0, sizeof(*severity_out));
+
+  cfg = eat_whitespace(cfg);
+  while (*cfg) {
+    const char *dash, *space;
+    char *sev_lo, *sev_hi;
+    int low, high, i;
+    log_domain_mask_t domains = ~0u;
+
+    if (*cfg == '[') {
+      int err = 0;
+      char *domains_str;
+      smartlist_t *domains_list;
+      log_domain_mask_t neg_domains = 0;
+      const char *closebracket = strchr(cfg, ']');
+      if (!closebracket)
+        return -1;
+      domains = 0;
+      domains_str = tor_strndup(cfg+1, closebracket-cfg-1);
+      domains_list = smartlist_create();
+      smartlist_split_string(domains_list, domains_str, ",", SPLIT_SKIP_SPACE,
+                             -1);
+      tor_free(domains_str);
+      SMARTLIST_FOREACH(domains_list, const char *, domain,
+          {
+            if (!strcmp(domain, "*")) {
+              domains = ~0u;
+            } else {
+              int d;
+              int negate=0;
+              if (*domain == '~') {
+                negate = 1;
+                ++domain;
+              }
+              d = parse_log_domain(domain);
+              if (!d) {
+                log_warn(LD_CONFIG, "No such loggging domain as %s", domain);
+                err = 1;
+              } else {
+                if (negate)
+                  neg_domains |= d;
+                else
+                  domains |= d;
+              }
+            }
+          });
+      SMARTLIST_FOREACH(domains_list, char *, d, tor_free(d));
+      smartlist_free(domains_list);
+      if (err)
+        return -1;
+      domains &= ~neg_domains;
+      cfg = eat_whitespace(closebracket+1);
+    } else {
+      ++got_an_unqualified_range;
+    }
+    if (!strcasecmpstart(cfg, "file") ||
+        !strcasecmpstart(cfg, "stderr") ||
+        !strcasecmpstart(cfg, "stdout") ||
+        !strcasecmpstart(cfg, "syslog")) {
+      goto done;
+    }
+    if (got_an_unqualified_range > 1)
+      return -1;
+
+    space = strchr(cfg, ' ');
+    dash = strchr(cfg, '-');
+    if (!space)
+      space = strchr(cfg, '\0');
+    if (dash && dash < space) {
+      sev_lo = tor_strndup(cfg, dash-cfg);
+      sev_hi = tor_strndup(dash+1, space-(dash+1));
+    } else {
+      sev_lo = tor_strndup(cfg, space-cfg);
+      sev_hi = tor_strdup("ERR");
+    }
+    if ((low = parse_log_level(sev_lo)) == -1)
+      return -1;
+    if ((high = parse_log_level(sev_hi)) == -1)
+      return -1;
+
+    got_anything = 1;
+    for (i=low; i >= high; --i)
+      severity_out->masks[SEVERITY_MASK_IDX(i)] |= domains;
+
+    cfg = eat_whitespace(space);
+  }
+
+ done:
+  *cfg_ptr = cfg;
+  return got_anything ? 0 : -1;
+}
+
 /** Return the least severe log level that any current log is interested in. */
 int
 get_min_log_level(void)
 {
   logfile_t *lf;
+  int i;
   int min = LOG_ERR;
   for (lf = logfiles; lf; lf = lf->next) {
-    if (lf->min_loglevel > min)
-      min = lf->min_loglevel;
+    for (i = LOG_DEBUG; i > min; --i)
+      if (lf->severities->masks[SEVERITY_MASK_IDX(i)])
+        min = i;
   }
   return min;
 }
@@ -694,9 +862,11 @@
 switch_logs_debug(void)
 {
   logfile_t *lf;
+  int i;
   LOCK_LOGS();
   for (lf = logfiles; lf; lf=lf->next) {
-    lf->min_loglevel = LOG_DEBUG;
+    for (i = LOG_DEBUG; i >= LOG_ERR; --i)
+      lf->severities->masks[i] = ~0u;
   }
   UNLOCK_LOGS();
 }

Modified: tor/trunk/src/common/log.h
===================================================================
--- tor/trunk/src/common/log.h	2008-03-05 17:18:04 UTC (rev 13874)
+++ tor/trunk/src/common/log.h	2008-03-05 22:31:39 UTC (rev 13875)
@@ -91,19 +91,38 @@
 /** Bandwidth accounting. */
 #define LD_ACCT     (1u<<17)
 
+/** Number of logging domains in the code. */
+#define N_LOGGING_DOMAINS 18
+
+typedef uint32_t log_domain_mask_t;
+
+/** Configures which severities are logged for each logging domain for a given
+ * log target. */
+typedef struct log_severity_list_t {
+  /** For each log severity, a bitmask of which domains a given logger is
+   * logging. */
+  log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
+} log_severity_list_t;
+
+/** Given a severity, yields an index into log_severity_list_t.masks to use
+ * for that severity. */
+#define SEVERITY_MASK_IDX(sev) ((sev) - LOG_ERR)
+
 /** Callback type used for add_callback_log. */
 typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
 
 void init_logging(void);
 int parse_log_level(const char *level);
 const char *log_level_to_string(int level);
-void add_stream_log(int severityMin, int severityMax, const char *name,
+int parse_log_severity_config(const char **cfg,
+                              log_severity_list_t *severity_out);
+void add_stream_log(log_severity_list_t *severity, const char *name,
                     FILE *stream);
-int add_file_log(int severityMin, int severityMax, const char *filename);
+int add_file_log(log_severity_list_t *severity, const char *filename);
 #ifdef HAVE_SYSLOG_H
-int add_syslog_log(int loglevelMin, int loglevelMax);
+int add_syslog_log(log_severity_list_t *severity);
 #endif
-int add_callback_log(int loglevelMin, int loglevelMax, log_callback cb);
+int add_callback_log(log_severity_list_t *severity, log_callback cb);
 int get_min_log_level(void);
 void switch_logs_debug(void);
 void logs_free_all(void);
@@ -118,14 +137,14 @@
 void log_set_application_name(const char *name);
 
 /* Outputs a message to stdout */
-void _log(int severity, uint32_t domain, const char *format, ...)
+void _log(int severity, log_domain_mask_t domain, const char *format, ...)
   CHECK_PRINTF(3,4);
 #define log _log /* hack it so we don't conflict with log() as much */
 
 #ifdef __GNUC__
 extern int _log_global_min_severity;
 
-void _log_fn(int severity, uint32_t domain,
+void _log_fn(int severity, log_domain_mask_t domain,
              const char *funcname, const char *format, ...)
   CHECK_PRINTF(4,5);
 /** Log a message at level <b>severity</b>, using a pretty-printed version
@@ -148,12 +167,12 @@
 
 #else /* ! defined(__GNUC__) */
 
-void _log_fn(int severity, uint32_t domain, const char *format, ...);
-void _log_debug(uint32_t domain, const char *format, ...);
-void _log_info(uint32_t domain, const char *format, ...);
-void _log_notice(uint32_t domain, const char *format, ...);
-void _log_warn(uint32_t domain, const char *format, ...);
-void _log_err(uint32_t domain, const char *format, ...);
+void _log_fn(int severity, log_domain_mask_t domain, const char *format, ...);
+void _log_debug(log_domain_mask_t domain, const char *format, ...);
+void _log_info(log_domain_mask_t domain, const char *format, ...);
+void _log_notice(log_domain_mask_t domain, const char *format, ...);
+void _log_warn(log_domain_mask_t domain, const char *format, ...);
+void _log_err(log_domain_mask_t domain, const char *format, ...);
 
 #if defined(_MSC_VER) && _MSC_VER < 1300
 /* MSVC 6 and earlier don't have __func__, or even __LINE__. */

Modified: tor/trunk/src/or/config.c
===================================================================
--- tor/trunk/src/or/config.c	2008-03-05 17:18:04 UTC (rev 13874)
+++ tor/trunk/src/or/config.c	2008-03-05 22:31:39 UTC (rev 13875)
@@ -630,8 +630,6 @@
                                  int validate_only);
 static int parse_redirect_line(smartlist_t *result,
                                config_line_t *line, char **msg);
-static int parse_log_severity_range(const char *range, int *min_out,
-                                    int *max_out);
 static int validate_data_directory(or_options_t *options);
 static int write_configuration_file(const char *fname, or_options_t *options);
 static config_line_t *get_assigned_option(config_format_t *fmt,
@@ -1052,8 +1050,10 @@
  commit:
   r = 0;
   if (logs_marked) {
+    log_severity_list_t *severity =
+      tor_malloc_zero(sizeof(log_severity_list_t));
     close_temp_logs();
-    add_callback_log(LOG_ERR, LOG_ERR, control_event_logmsg);
+    add_callback_log(severity, control_event_logmsg);
     control_adjust_event_log_severity();
   }
   SMARTLIST_FOREACH(replaced_listeners, connection_t *, conn,
@@ -3722,58 +3722,6 @@
   smartlist_free(elts);
 }
 
-/** If <b>range</b> is of the form MIN-MAX, for MIN and MAX both
- * recognized log severity levels, set *<b>min_out</b> to MIN and
- * *<b>max_out</b> to MAX and return 0.  Else, if <b>range</b> is of
- * the form MIN, act as if MIN-err had been specified.  Else, warn and
- * return -1.
- */
-static int
-parse_log_severity_range(const char *range, int *min_out, int *max_out)
-{
-  int levelMin, levelMax;
-  const char *cp;
-  cp = strchr(range, '-');
-  if (cp) {
-    if (cp == range) {
-      levelMin = LOG_DEBUG;
-    } else {
-      char *tmp_sev = tor_strndup(range, cp - range);
-      levelMin = parse_log_level(tmp_sev);
-      if (levelMin < 0) {
-        log_warn(LD_CONFIG, "Unrecognized minimum log severity '%s': must be "
-                 "one of err|warn|notice|info|debug", tmp_sev);
-        tor_free(tmp_sev);
-        return -1;
-      }
-      tor_free(tmp_sev);
-    }
-    if (!*(cp+1)) {
-      levelMax = LOG_ERR;
-    } else {
-      levelMax = parse_log_level(cp+1);
-      if (levelMax < 0) {
-        log_warn(LD_CONFIG, "Unrecognized maximum log severity '%s': must be "
-                 "one of err|warn|notice|info|debug", cp+1);
-        return -1;
-      }
-    }
-  } else {
-    levelMin = parse_log_level(range);
-    if (levelMin < 0) {
-      log_warn(LD_CONFIG, "Unrecognized log severity '%s': must be one of "
-               "err|warn|notice|info|debug", range);
-      return -1;
-    }
-    levelMax = LOG_ERR;
-  }
-
-  *min_out = levelMin;
-  *max_out = levelMax;
-
-  return 0;
-}
-
 /**
  * Initialize the logs based on the configuration file.
  */
@@ -3794,84 +3742,73 @@
   elts = smartlist_create();
 
   for (opt = options->Logs; opt; opt = opt->next) {
-    int levelMin=LOG_DEBUG, levelMax=LOG_ERR;
-    smartlist_split_string(elts, opt->value, NULL,
-                           SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 3);
-    if (smartlist_len(elts) == 0) {
-      log_warn(LD_CONFIG, "No arguments to Log option 'Log %s'", opt->value);
+    log_severity_list_t *severity;
+    const char *cfg = opt->value;
+    severity = tor_malloc_zero(sizeof(log_severity_list_t));
+    if (parse_log_severity_config(&cfg, severity) < 0) {
+      log_warn(LD_CONFIG, "Couldn't parse log levels in Log option 'Log %s'",
+               opt->value);
       ok = 0; goto cleanup;
     }
-    if (parse_log_severity_range(smartlist_get(elts,0), &levelMin,
-                                 &levelMax)) {
-      ok = 0; goto cleanup;
-    }
-    if (smartlist_len(elts) < 2) { /* only loglevels were provided */
+
+    smartlist_split_string(elts, cfg, NULL,
+                           SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 2);
+
+    if (smartlist_len(elts) == 0)
+      smartlist_add(elts, tor_strdup("stdout"));
+
+    if (smartlist_len(elts) == 1 &&
+        (!strcasecmp(smartlist_get(elts,0), "stdout") ||
+         !strcasecmp(smartlist_get(elts,0), "stderr"))) {
+      int err = smartlist_len(elts) &&
+        !strcasecmp(smartlist_get(elts,0), "stderr");
       if (!validate_only) {
         if (daemon) {
           log_warn(LD_CONFIG,
-              "Can't log to stdout with RunAsDaemon set; skipping stdout");
+                   "Can't log to %s with RunAsDaemon set; skipping stdout",
+                   err?"stderr":"stdout");
         } else {
-          add_stream_log(levelMin, levelMax, "<stdout>", stdout);
+          add_stream_log(severity, err?"<stderr>":"<stdout>",
+                         err?stderr:stdout);
+          severity=NULL;
         }
       }
       goto cleanup;
     }
-    if (!strcasecmp(smartlist_get(elts,1), "file")) {
-      if (smartlist_len(elts) != 3) {
-        log_warn(LD_CONFIG, "Bad syntax on file Log option 'Log %s'",
-                 opt->value);
-        ok = 0; goto cleanup;
+    if (smartlist_len(elts) == 1 &&
+        !strcasecmp(smartlist_get(elts,0), "syslog")) {
+#ifdef HAVE_SYSLOG_H
+      if (!validate_only) {
+        add_syslog_log(severity);
+        severity=NULL;
       }
+#else
+      log_warn(LD_CONFIG, "Syslog is not supported on this system. Sorry.");
+#endif
+      goto cleanup;
+    }
+
+    if (smartlist_len(elts) == 2 &&
+        !strcasecmp(smartlist_get(elts,0), "file")) {
       if (!validate_only) {
-        if (add_file_log(levelMin, levelMax, smartlist_get(elts, 2)) < 0) {
+        if (add_file_log(severity, smartlist_get(elts, 1)) < 0) {
           log_warn(LD_CONFIG, "Couldn't open file for 'Log %s'", opt->value);
           ok = 0;
+        } else {
+          tor_free(severity);
         }
       }
       goto cleanup;
     }
-    if (smartlist_len(elts) != 2) {
-      log_warn(LD_CONFIG, "Wrong number of arguments on Log option 'Log %s'",
-               opt->value);
-      ok = 0; goto cleanup;
-    }
-    if (!strcasecmp(smartlist_get(elts,1), "stdout")) {
-      if (daemon) {
-        log_warn(LD_CONFIG, "Can't log to stdout with RunAsDaemon set.");
-        ok = 0; goto cleanup;
-      }
-      if (!validate_only) {
-        add_stream_log(levelMin, levelMax, "<stdout>", stdout);
-      }
-    } else if (!strcasecmp(smartlist_get(elts,1), "stderr")) {
-      if (daemon) {
-        log_warn(LD_CONFIG, "Can't log to stderr with RunAsDaemon set.");
-        ok = 0; goto cleanup;
-      }
-      if (!validate_only) {
-        add_stream_log(levelMin, levelMax, "<stderr>", stderr);
-      }
-    } else if (!strcasecmp(smartlist_get(elts,1), "syslog")) {
-#ifdef HAVE_SYSLOG_H
-      if (!validate_only)
-        add_syslog_log(levelMin, levelMax);
-#else
-      log_warn(LD_CONFIG, "Syslog is not supported on this system. Sorry.");
-#endif
-    } else {
-      log_warn(LD_CONFIG, "Unrecognized log type %s",
-               (const char*)smartlist_get(elts,1));
-      if (strchr(smartlist_get(elts,1), '/') ||
-          strchr(smartlist_get(elts,1), '\\')) {
-        log_warn(LD_CONFIG, "Did you mean to say 'Log %s file %s' ?",
-                 (const char *)smartlist_get(elts,0),
-                 (const char *)smartlist_get(elts,1));
-      }
-      ok = 0; goto cleanup;
-    }
+
+    log_warn(LD_CONFIG, "Bad syntax on file Log option 'Log %s'",
+             opt->value);
+    ok = 0; goto cleanup;
+
   cleanup:
     SMARTLIST_FOREACH(elts, char*, cp, tor_free(cp));
     smartlist_clear(elts);
+    tor_free(severity);
   }
   smartlist_free(elts);
 

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2008-03-05 17:18:04 UTC (rev 13874)
+++ tor/trunk/src/or/test.c	2008-03-05 22:31:39 UTC (rev 13875)
@@ -3634,7 +3634,12 @@
     }
   }
 
-  add_stream_log(loglevel, LOG_ERR, "", stdout);
+  {
+    log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
+    for (i = loglevel; i >= LOG_ERR; --i)
+      s->masks[SEVERITY_MASK_IDX(i)] = ~0u;
+    add_stream_log(s, "", stdout);
+  }
 
   options->command = CMD_RUN_UNITTESTS;
   rep_hist_init();

Modified: tor/trunk/src/tools/tor-gencert.c
===================================================================
--- tor/trunk/src/tools/tor-gencert.c	2008-03-05 17:18:04 UTC (rev 13874)
+++ tor/trunk/src/tools/tor-gencert.c	2008-03-05 22:31:39 UTC (rev 13875)
@@ -123,6 +123,7 @@
 parse_commandline(int argc, char **argv)
 {
   int i;
+  log_severity_list_t *s;
   for (i = 1; i < argc; ++i) {
     if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
       show_help();
@@ -188,11 +189,15 @@
     }
   }
 
+  s = tor_malloc_zero(sizeof(log_severity_list_t));
+  s->masks[SEVERITY_MASK_IDX(LOG_ERR)] = ~0u;
+  s->masks[SEVERITY_MASK_IDX(LOG_WARN)] = ~0u;
   if (verbose) {
-    add_stream_log(LOG_INFO, LOG_ERR, "<stderr>", stderr);
-  } else {
-    add_stream_log(LOG_NOTICE, LOG_ERR, "<stderr>", stderr);
+    s->masks[SEVERITY_MASK_IDX(LOG_NOTICE)] = ~0u;
+    s->masks[SEVERITY_MASK_IDX(LOG_INFO)] = ~0u;
+    s->masks[SEVERITY_MASK_IDX(LOG_DEBUG)] = ~0u;
   }
+  add_stream_log(s, "<stderr>", stderr);
 
   if (!identity_key_file) {
     identity_key_file = tor_strdup("./authority_identity_key");

Modified: tor/trunk/src/tools/tor-resolve.c
===================================================================
--- tor/trunk/src/tools/tor-resolve.c	2008-03-05 17:18:04 UTC (rev 13874)
+++ tor/trunk/src/tools/tor-resolve.c	2008-03-05 22:31:39 UTC (rev 13875)
@@ -280,6 +280,7 @@
   uint32_t result = 0;
   char *result_hostname = NULL;
   char buf[INET_NTOA_BUF_LEN];
+  log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
 
   init_logging();
 
@@ -317,11 +318,15 @@
     usage();
   }
 
+  s->masks[SEVERITY_MASK_IDX(LOG_ERR)] = ~0u;
+  s->masks[SEVERITY_MASK_IDX(LOG_WARN)] = ~0u;
   if (isVerbose) {
-    add_stream_log(LOG_DEBUG, LOG_ERR, "<stderr>", stderr);
-  } else {
-    add_stream_log(LOG_WARN, LOG_ERR, "<stderr>", stderr);
+    s->masks[SEVERITY_MASK_IDX(LOG_NOTICE)] = ~0u;
+    s->masks[SEVERITY_MASK_IDX(LOG_INFO)] = ~0u;
+    s->masks[SEVERITY_MASK_IDX(LOG_DEBUG)] = ~0u;
   }
+  add_stream_log(s, "<stderr>", stderr);
+
   if (n_args == 1) {
     log_debug(LD_CONFIG, "defaulting to localhost:9050");
     sockshost = 0x7f000001u; /* localhost */



More information about the tor-commits mailing list