1/*
2 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/system_wrappers/include/logging.h"
12
13#include "testing/gtest/include/gtest/gtest.h"
14#include "webrtc/base/scoped_ptr.h"
15#include "webrtc/system_wrappers/include/condition_variable_wrapper.h"
16#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
17#include "webrtc/system_wrappers/include/sleep.h"
18#include "webrtc/system_wrappers/include/trace.h"
19
20namespace webrtc {
21namespace {
22
23class LoggingTest : public ::testing::Test, public TraceCallback {
24 public:
25  virtual void Print(TraceLevel level, const char* msg, int length) {
26    CriticalSectionScoped cs(crit_.get());
27    // We test the length here to ensure (with high likelihood) that only our
28    // traces will be tested.
29    if (level_ != kTraceNone && static_cast<int>(expected_log_.str().size()) ==
30        length - Trace::kBoilerplateLength - 1) {
31      EXPECT_EQ(level_, level);
32      EXPECT_EQ(expected_log_.str(), &msg[Trace::kBoilerplateLength]);
33      level_ = kTraceNone;
34      cv_->Wake();
35    }
36  }
37
38 protected:
39  LoggingTest()
40    : crit_(CriticalSectionWrapper::CreateCriticalSection()),
41      cv_(ConditionVariableWrapper::CreateConditionVariable()),
42      level_(kTraceNone),
43      expected_log_() {
44  }
45
46  void SetUp() {
47    Trace::CreateTrace();
48    Trace::SetTraceCallback(this);
49  }
50
51  void TearDown() {
52    Trace::SetTraceCallback(NULL);
53    Trace::ReturnTrace();
54    CriticalSectionScoped cs(crit_.get());
55    ASSERT_EQ(kTraceNone, level_) << "Print() was not called";
56  }
57
58  rtc::scoped_ptr<CriticalSectionWrapper> crit_;
59  rtc::scoped_ptr<ConditionVariableWrapper> cv_;
60  TraceLevel level_ GUARDED_BY(crit_);
61  std::ostringstream expected_log_ GUARDED_BY(crit_);
62};
63
64TEST_F(LoggingTest, LogStream) {
65  {
66    CriticalSectionScoped cs(crit_.get());
67    level_ = kTraceWarning;
68    std::string msg = "Important message";
69    expected_log_ << "(logging_unittest.cc:" << __LINE__ + 1 << "): " << msg;
70    LOG(LS_WARNING) << msg;
71    cv_->SleepCS(*crit_.get(), 2000);
72  }
73}
74
75}  // namespace
76}  // namespace webrtc
77