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
This commit is contained in:
andrew@webrtc.org 2013-03-25 16:23:37 +00:00
parent b4c441a785
commit b87cc85beb
5 changed files with 93 additions and 37 deletions

View File

@ -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',
],
},
{

View File

@ -8,40 +8,18 @@
* be found in the AUTHORS file in the root of the source tree.
*/
#include "test/test_suite.h"
#include <string>
#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

View File

@ -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<TraceCallbackImpl> trace_callback_;
scoped_ptr<TraceToStderr> trace_to_stderr_;
};
} // namespace test

View File

@ -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 <cassert>
#include <cstdio>
#include <string>
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

View File

@ -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_