commit f74916a98f97c7e80e46400e921d54466285d1fb Author: Nick Mathewson nickm@torproject.org Date: Wed Aug 31 12:51:22 2016 -0400
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. --- src/common/log.c | 5 +++++ src/common/torlog.h | 5 +++++ src/test/log_test_helpers.c | 16 +++++++++++++++- src/test/log_test_helpers.h | 5 +++++ 4 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/src/common/log.c b/src/common/log.c index cb62a37..71b6790 100644 --- a/src/common/log.c +++ b/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); } diff --git a/src/common/torlog.h b/src/common/torlog.h index 80f37e0..6732a42 100644 --- a/src/common/torlog.h +++ b/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;
diff --git a/src/test/log_test_helpers.c b/src/test/log_test_helpers.c index 1828689..8b67c39 100644 --- a/src/test/log_test_helpers.c +++ b/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); + } }
diff --git a/src/test/log_test_helpers.h b/src/test/log_test_helpers.h index d767453..2cb76b2 100644 --- a/src/test/log_test_helpers.h +++ b/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);