Browse Source

setup_capture_of_logs: no longer suppress log messages

Previously setup_capture_of_logs would prevent log messages from
going to the console entirely.  That's a problem, since sometimes
log messages are bugs!  Now setup_capture_of_logs() acts sensibly.

If you really do need to keep a message from going to the console
entirely, there is setup_full_capture_of_logs().  But only use that
if you're prepared to make sure that there are no extraneous
messages generated at all.
Nick Mathewson 7 years ago
parent
commit
f74916a98f
4 changed files with 30 additions and 1 deletions
  1. 5 0
      src/common/log.c
  2. 5 0
      src/common/torlog.h
  3. 15 1
      src/test/log_test_helpers.c
  4. 5 0
      src/test/log_test_helpers.h

+ 5 - 0
src/common/log.c

@@ -534,6 +534,11 @@ tor_log(int severity, log_domain_mask_t domain, const char *format, ...)
   if (severity > log_global_min_severity_)
     return;
   va_start(ap,format);
+#ifdef TOR_UNIT_TESTS
+  if (domain & LD_NO_MOCK)
+    logv__real(severity, domain, NULL, NULL, format, ap);
+  else
+#endif
   logv(severity, domain, NULL, NULL, format, ap);
   va_end(ap);
 }

+ 5 - 0
src/common/torlog.h

@@ -109,6 +109,11 @@
  * would. Used as a flag, not a log domain. */
 #define LD_NOFUNCNAME (1u<<30)
 
+#ifdef TOR_UNIT_TESTS
+/** This log message should not be intercepted by mock_saving_logv */
+#define LD_NO_MOCK (1u<<29)
+#endif
+
 /** Mask of zero or more log domains, OR'd together. */
 typedef uint32_t log_domain_mask_t;
 

+ 15 - 1
src/test/log_test_helpers.c

@@ -6,6 +6,16 @@
 
 static smartlist_t *saved_logs = NULL;
 
+static int echo_to_real_logs = 1;
+
+int
+setup_full_capture_of_logs(int new_level)
+{
+  int result = setup_capture_of_logs(new_level);
+  echo_to_real_logs = 0;
+  return result;
+}
+
 int
 setup_capture_of_logs(int new_level)
 {
@@ -14,6 +24,7 @@ setup_capture_of_logs(int new_level)
   mock_clean_saved_logs();
   saved_logs = smartlist_new();
   MOCK(logv, mock_saving_logv);
+  echo_to_real_logs = 1;
   return previous_log;
 }
 
@@ -108,7 +119,6 @@ mock_saving_logv(int severity, log_domain_mask_t domain,
                  const char *funcname, const char *suffix,
                  const char *format, va_list ap)
 {
-  (void)domain;
   char *buf = tor_malloc_zero(10240);
   int n;
   n = tor_vsnprintf(buf,10240,format,ap);
@@ -127,5 +137,9 @@ mock_saving_logv(int severity, log_domain_mask_t domain,
   if (!saved_logs)
     saved_logs = smartlist_new();
   smartlist_add(saved_logs, e);
+
+  if (echo_to_real_logs) {
+    tor_log(severity, domain|LD_NO_MOCK, "%s", e->generated_msg);
+  }
 }
 

+ 5 - 0
src/test/log_test_helpers.h

@@ -22,6 +22,7 @@ void mock_saving_logv(int severity, log_domain_mask_t domain,
 void mock_clean_saved_logs(void);
 const smartlist_t *mock_saved_logs(void);
 int setup_capture_of_logs(int new_level);
+int setup_full_capture_of_logs(int new_level);
 void teardown_capture_of_logs(int prev);
 
 int mock_saved_log_has_message(const char *msg);
@@ -33,6 +34,10 @@ int mock_saved_log_has_entry(void);
   tt_assert_msg(mock_saved_log_has_message(str), \
                 "expected log to contain " # str);
 
+#define expect_log_msg_containing(str) \
+  tt_assert_msg(mock_saved_log_has_message_containing(str), \
+                "expected log to contain " # str);
+
 #define expect_no_log_msg(str) \
   tt_assert_msg(!mock_saved_log_has_message(str), \
                 "expected log to not contain " # str);