[or-cvs] r14016: Refactor log domain mask code so that nobody outside of log. (in tor/trunk: . src/common src/or src/tools)

nickm at seul.org nickm at seul.org
Thu Mar 13 22:18:38 UTC 2008


Author: nickm
Date: 2008-03-13 18:18:38 -0400 (Thu, 13 Mar 2008)
New Revision: 14016

Modified:
   tor/trunk/
   tor/trunk/src/common/log.c
   tor/trunk/src/common/log.h
   tor/trunk/src/or/test.c
   tor/trunk/src/tools/tor-gencert.c
   tor/trunk/src/tools/tor-resolve.c
Log:
 r18804 at catbus:  nickm | 2008-03-13 18:18:31 -0400
 Refactor log domain mask code so that nobody outside of log.c has to use SEVERITY_MASK_IDX.  It is error-prone.



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

Modified: tor/trunk/src/common/log.c
===================================================================
--- tor/trunk/src/common/log.c	2008-03-13 22:18:36 UTC (rev 14015)
+++ tor/trunk/src/common/log.c	2008-03-13 22:18:38 UTC (rev 14016)
@@ -24,6 +24,7 @@
 #include <time.h>
 #endif
 #include "util.h"
+#define LOG_PRIVATE
 #include "log.h"
 #include "container.h"
 
@@ -474,16 +475,21 @@
   }
 }
 
-/** DOCDOC XXXX021 remove me. */
-static log_severity_list_t *
-new_severity_list(int loglevelMin, int loglevelMax)
+/** Adjust a log severity configuration in <b>severity_out</b> to contain
+ * every domain between <b>loglevelMin</b> and <b>loglevelMax</b>, inclusive.
+ */
+void
+set_log_severity_config(int loglevelMin, int loglevelMax,
+                        log_severity_list_t *severity_out)
 {
-  log_severity_list_t *out = tor_malloc_zero(sizeof(log_severity_list_t));
   int i;
+  tor_assert(loglevelMin >= loglevelMax);
+  tor_assert(loglevelMin >= LOG_ERR && loglevelMin <= LOG_DEBUG);
+  tor_assert(loglevelMax >= LOG_ERR && loglevelMax <= LOG_DEBUG);
+  memset(severity_out, 0, sizeof(log_severity_list_t));
   for (i = loglevelMin; i >= loglevelMax; --i) {
-    out->masks[SEVERITY_MASK_IDX(i)] = ~0u;
+    severity_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>
@@ -529,9 +535,10 @@
 void
 add_temp_log(void)
 {
+  log_severity_list_t *s = tor_malloc_zero(sizeof(log_severity_list_t));
+  set_log_severity_config(LOG_NOTICE, LOG_ERR, s);
   LOCK_LOGS();
-  add_stream_log_impl(new_severity_list(LOG_NOTICE, LOG_ERR),
-                      "<temp>", stdout);
+  add_stream_log_impl(s, "<temp>", stdout);
   logfiles->is_temporary = 1;
   UNLOCK_LOGS();
 }
@@ -567,8 +574,7 @@
   LOCK_LOGS();
   for (lf = logfiles; lf; lf = lf->next) {
     if (lf->callback == cb) {
-      tor_free(lf->severities);
-      lf->severities = new_severity_list(loglevelMin, loglevelMax);
+      set_log_severity_config(loglevelMin, loglevelMax, lf->severities);
     }
   }
   _log_global_min_severity = get_min_log_level();

Modified: tor/trunk/src/common/log.h
===================================================================
--- tor/trunk/src/common/log.h	2008-03-13 22:18:36 UTC (rev 14015)
+++ tor/trunk/src/common/log.h	2008-03-13 22:18:38 UTC (rev 14016)
@@ -104,9 +104,11 @@
   log_domain_mask_t masks[LOG_DEBUG-LOG_ERR+1];
 } log_severity_list_t;
 
+#ifdef LOG_PRIVATE
 /** 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)
+#endif
 
 /** Callback type used for add_callback_log. */
 typedef void (*log_callback)(int severity, uint32_t domain, const char *msg);
@@ -116,6 +118,8 @@
 const char *log_level_to_string(int level);
 int parse_log_severity_config(const char **cfg,
                               log_severity_list_t *severity_out);
+void set_log_severity_config(int minSeverity, int maxSeverity,
+                             log_severity_list_t *severity_out);
 void add_stream_log(log_severity_list_t *severity, const char *name,
                     FILE *stream);
 int add_file_log(log_severity_list_t *severity, const char *filename);

Modified: tor/trunk/src/or/test.c
===================================================================
--- tor/trunk/src/or/test.c	2008-03-13 22:18:36 UTC (rev 14015)
+++ tor/trunk/src/or/test.c	2008-03-13 22:18:38 UTC (rev 14016)
@@ -3636,8 +3636,7 @@
 
   {
     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;
+    set_log_severity_config(loglevel, LOG_ERR, s);
     add_stream_log(s, "", stdout);
   }
 

Modified: tor/trunk/src/tools/tor-gencert.c
===================================================================
--- tor/trunk/src/tools/tor-gencert.c	2008-03-13 22:18:36 UTC (rev 14015)
+++ tor/trunk/src/tools/tor-gencert.c	2008-03-13 22:18:38 UTC (rev 14016)
@@ -190,13 +190,10 @@
   }
 
   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) {
-    s->masks[SEVERITY_MASK_IDX(LOG_NOTICE)] = ~0u;
-    s->masks[SEVERITY_MASK_IDX(LOG_INFO)] = ~0u;
-    s->masks[SEVERITY_MASK_IDX(LOG_DEBUG)] = ~0u;
-  }
+  if (verbose)
+    set_log_severity_config(LOG_DEBUG, LOG_ERR, s);
+  else
+    set_log_severity_config(LOG_WARN, LOG_ERR, s);
   add_stream_log(s, "<stderr>", stderr);
 
   if (!identity_key_file) {

Modified: tor/trunk/src/tools/tor-resolve.c
===================================================================
--- tor/trunk/src/tools/tor-resolve.c	2008-03-13 22:18:36 UTC (rev 14015)
+++ tor/trunk/src/tools/tor-resolve.c	2008-03-13 22:18:38 UTC (rev 14016)
@@ -318,13 +318,10 @@
     usage();
   }
 
-  s->masks[SEVERITY_MASK_IDX(LOG_ERR)] = ~0u;
-  s->masks[SEVERITY_MASK_IDX(LOG_WARN)] = ~0u;
-  if (isVerbose) {
-    s->masks[SEVERITY_MASK_IDX(LOG_NOTICE)] = ~0u;
-    s->masks[SEVERITY_MASK_IDX(LOG_INFO)] = ~0u;
-    s->masks[SEVERITY_MASK_IDX(LOG_DEBUG)] = ~0u;
-  }
+  if (isVerbose)
+    set_log_severity_config(LOG_DEBUG, LOG_ERR, s);
+  else
+    set_log_severity_config(LOG_WARN, LOG_ERR, s);
   add_stream_log(s, "<stderr>", stderr);
 
   if (n_args == 1) {



More information about the tor-commits mailing list