From b87cc85beb52e6b78dd6add415f3a5c53432d25a Mon Sep 17 00:00:00 2001 From: "andrew@webrtc.org" Date: Mon, 25 Mar 2013 16:23:37 +0000 Subject: [PATCH] Refactor unittest trace printouts to a separate class. This allows other tests/tools which don't depend on TestSuite to reuse the functionality. BUG= Review URL: https://webrtc-codereview.appspot.com/1245004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3721 4adac7df-926f-26a2-2b94-8c16560cd09d --- webrtc/test/test.gyp | 2 + webrtc/test/test_suite.cc | 37 +++-------------- webrtc/test/test_suite.h | 12 +++--- webrtc/test/testsupport/trace_to_stderr.cc | 47 ++++++++++++++++++++++ webrtc/test/testsupport/trace_to_stderr.h | 32 +++++++++++++++ 5 files changed, 93 insertions(+), 37 deletions(-) create mode 100644 webrtc/test/testsupport/trace_to_stderr.cc create mode 100644 webrtc/test/testsupport/trace_to_stderr.h diff --git a/webrtc/test/test.gyp b/webrtc/test/test.gyp index ff83ec53c..e6058ea32 100644 --- a/webrtc/test/test.gyp +++ b/webrtc/test/test.gyp @@ -47,6 +47,8 @@ 'testsupport/packet_reader.h', 'testsupport/perf_test.cc', 'testsupport/perf_test.h', + 'testsupport/trace_to_stderr.cc', + 'testsupport/trace_to_stderr.h', ], }, { diff --git a/webrtc/test/test_suite.cc b/webrtc/test/test_suite.cc index e0208e218..2ef305bfa 100644 --- a/webrtc/test/test_suite.cc +++ b/webrtc/test/test_suite.cc @@ -8,40 +8,18 @@ * be found in the AUTHORS file in the root of the source tree. */ -#include "test/test_suite.h" - -#include +#include "webrtc/test/test_suite.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" -#include "test/testsupport/fileutils.h" -#include "webrtc/system_wrappers/interface/trace.h" +#include "webrtc/test/testsupport/fileutils.h" +#include "webrtc/test/testsupport/trace_to_stderr.h" namespace webrtc { namespace test { -const int kLevelFilter = kTraceError | kTraceWarning | kTraceTerseInfo; - -class TraceCallbackImpl : public TraceCallback { - public: - TraceCallbackImpl() { } - virtual ~TraceCallbackImpl() { } - - virtual void Print(TraceLevel level, const char* msg_array, int length) { - if (level & kLevelFilter) { - ASSERT_GT(length, Trace::kBoilerplateLength); - std::string msg = msg_array; - std::string msg_time = msg.substr(Trace::kTimestampPosition, - Trace::kTimestampLength); - std::string msg_log = msg.substr(Trace::kBoilerplateLength); - fprintf(stderr, "%s %s\n", msg_time.c_str(), msg_log.c_str()); - fflush(stderr); - } - } -}; - TestSuite::TestSuite(int argc, char** argv) - : trace_callback_(new TraceCallbackImpl) { + : trace_to_stderr_(NULL) { SetExecutablePath(argv[0]); testing::InitGoogleMock(&argc, argv); // Runs InitGoogleTest() internally. } @@ -57,14 +35,11 @@ int TestSuite::Run() { } void TestSuite::Initialize() { - Trace::CreateTrace(); - Trace::SetTraceCallback(trace_callback_.get()); - Trace::SetLevelFilter(kLevelFilter); + // Create TraceToStderr here so the behavior can be overridden. + trace_to_stderr_.reset(new TraceToStderr); } void TestSuite::Shutdown() { - Trace::SetTraceCallback(NULL); - Trace::ReturnTrace(); } } // namespace test diff --git a/webrtc/test/test_suite.h b/webrtc/test/test_suite.h index 340c2151f..16c035795 100644 --- a/webrtc/test/test_suite.h +++ b/webrtc/test/test_suite.h @@ -8,8 +8,8 @@ * be found in the AUTHORS file in the root of the source tree. */ -#ifndef TEST_TEST_SUITE_H_ -#define TEST_TEST_SUITE_H_ +#ifndef WEBRTC_TEST_TEST_SUITE_H_ +#define WEBRTC_TEST_TEST_SUITE_H_ // Derived from Chromium's src/base/test/test_suite.h. @@ -17,13 +17,13 @@ // instantiate this class in your main function and call its Run method to run // any gtest based tests that are linked into your executable. -#include "system_wrappers/interface/constructor_magic.h" -#include "system_wrappers/interface/scoped_ptr.h" +#include "webrtc/system_wrappers/interface/constructor_magic.h" +#include "webrtc/system_wrappers/interface/scoped_ptr.h" namespace webrtc { namespace test { -class TraceCallbackImpl; +class TraceToStderr; class TestSuite { public: @@ -41,7 +41,7 @@ class TestSuite { DISALLOW_COPY_AND_ASSIGN(TestSuite); private: - scoped_ptr trace_callback_; + scoped_ptr trace_to_stderr_; }; } // namespace test diff --git a/webrtc/test/testsupport/trace_to_stderr.cc b/webrtc/test/testsupport/trace_to_stderr.cc new file mode 100644 index 000000000..efc6ddf59 --- /dev/null +++ b/webrtc/test/testsupport/trace_to_stderr.cc @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#include "webrtc/test/testsupport/trace_to_stderr.h" + +#include +#include + +#include + +namespace webrtc { +namespace test { + +static const int kLevelFilter = kTraceError | kTraceWarning | kTraceTerseInfo; + +TraceToStderr::TraceToStderr() { + Trace::CreateTrace(); + Trace::SetTraceCallback(this); + Trace::SetLevelFilter(kLevelFilter); +} + +TraceToStderr::~TraceToStderr() { + Trace::SetTraceCallback(NULL); + Trace::ReturnTrace(); +} + +void TraceToStderr::Print(TraceLevel level, const char* msg_array, int length) { + if (level & kLevelFilter) { + assert(length > Trace::kBoilerplateLength); + std::string msg = msg_array; + std::string msg_time = msg.substr(Trace::kTimestampPosition, + Trace::kTimestampLength); + std::string msg_log = msg.substr(Trace::kBoilerplateLength); + fprintf(stderr, "%s %s\n", msg_time.c_str(), msg_log.c_str()); + fflush(stderr); + } +} + +} // namespace test +} // namespace webrtc diff --git a/webrtc/test/testsupport/trace_to_stderr.h b/webrtc/test/testsupport/trace_to_stderr.h new file mode 100644 index 000000000..40ff8795c --- /dev/null +++ b/webrtc/test/testsupport/trace_to_stderr.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef WEBRTC_TEST_TEST_SUPPORT_TRACE_TO_STDERR_H_ +#define WEBRTC_TEST_TEST_SUPPORT_TRACE_TO_STDERR_H_ + +#include "webrtc/system_wrappers/interface/trace.h" + +namespace webrtc { +namespace test { + +// Upon constructing an instance of this class, all traces will be redirected +// to stderr. At destruction, redirection is halted. +class TraceToStderr : public TraceCallback { + public: + TraceToStderr(); + virtual ~TraceToStderr(); + + virtual void Print(TraceLevel level, const char* msg_array, int length); +}; + +} // namespace test +} // namespace webrtc + +#endif // WEBRTC_TEST_TEST_SUPPORT_TRACE_TO_STDERR_H_