blob: 45acbfd302ae89749bd71c69d556586834f2b520 [file] [log] [blame]
andrew@webrtc.org50419b02012-11-14 19:07:54 +00001/*
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/interface/logging.h"
12
13#include "gtest/gtest.h"
14#include "webrtc/system_wrappers/interface/condition_variable_wrapper.h"
15#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
16#include "webrtc/system_wrappers/interface/scoped_ptr.h"
17#include "webrtc/system_wrappers/interface/sleep.h"
18#include "webrtc/system_wrappers/interface/trace.h"
19
20namespace webrtc {
21namespace {
22
23const size_t kBoilerplateLength = 71;
24
25class LoggingTest : public ::testing::Test, public TraceCallback {
26 public:
27 virtual void Print(const TraceLevel level,
28 const char* msg,
29 const int length) {
30 CriticalSectionScoped cs(crit_.get());
31 // We test the length here to ensure (with high likelihood) that only our
32 // traces will be tested.
33 if (level_ != kTraceNone &&
34 expected_log_.str().size() == length - kBoilerplateLength - 1) {
35 EXPECT_EQ(level_, level);
36 EXPECT_EQ(expected_log_.str(), &msg[kBoilerplateLength]);
37 level_ = kTraceNone;
38 cv_->Wake();
39 }
40 }
41
42 protected:
43 LoggingTest()
44 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
45 cv_(ConditionVariableWrapper::CreateConditionVariable()),
46 level_(kTraceNone),
47 expected_log_() {
48 }
49
50 void SetUp() {
51 Trace::CreateTrace();
52 Trace::SetTraceCallback(this);
53 // Reduce the chance that spurious traces will ruin the test.
54 Trace::SetLevelFilter(kTraceWarning | kTraceError);
55 }
56
57 void TearDown() {
58 CriticalSectionScoped cs(crit_.get());
59 Trace::SetTraceCallback(NULL);
60 Trace::ReturnTrace();
61 ASSERT_EQ(kTraceNone, level_) << "Print() was not called";
62 }
63
64 scoped_ptr<CriticalSectionWrapper> crit_;
65 scoped_ptr<ConditionVariableWrapper> cv_;
66 TraceLevel level_;
67 int length_;
68 std::ostringstream expected_log_;
69};
70
71TEST_F(LoggingTest, LogStream) {
72 {
73 CriticalSectionScoped cs(crit_.get());
74 level_ = kTraceWarning;
75 std::string msg = "Important message";
76 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 1 << "): " << msg;
77 LOG(WARNING) << msg;
78 cv_->SleepCS(*crit_.get());
79 }
80}
81
82TEST_F(LoggingTest, LogFunctionError) {
83 {
84 CriticalSectionScoped cs(crit_.get());
85 int bar = 42;
86 int baz = 99;
87 level_ = kTraceError;
88 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 2
89 << "): Foo failed: bar=" << bar << ", baz=" << baz;
90 LOG_FERR2(LS_ERROR, Foo, bar, baz);
91 cv_->SleepCS(*crit_.get());
92 }
93}
94
95} // namespace
96} // namespace webrtc