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/webrtc@3721 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/test/test.gyp b/test/test.gyp
index ff83ec5..e6058ea 100644
--- a/test/test.gyp
+++ b/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/test/test_suite.cc b/test/test_suite.cc
index e0208e2..2ef305b 100644
--- a/test/test_suite.cc
+++ b/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 <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 @@
 }
 
 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/test/test_suite.h b/test/test_suite.h
index 340c215..16c0357 100644
--- a/test/test_suite.h
+++ b/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 @@
   DISALLOW_COPY_AND_ASSIGN(TestSuite);
 
  private:
-  scoped_ptr<TraceCallbackImpl> trace_callback_;
+  scoped_ptr<TraceToStderr> trace_to_stderr_;
 };
 
 }  // namespace test
diff --git a/test/testsupport/trace_to_stderr.cc b/test/testsupport/trace_to_stderr.cc
new file mode 100644
index 0000000..efc6ddf
--- /dev/null
+++ b/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 <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
diff --git a/test/testsupport/trace_to_stderr.h b/test/testsupport/trace_to_stderr.h
new file mode 100644
index 0000000..40ff879
--- /dev/null
+++ b/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_