blob: 8d8a580f78eff31f38f55d451d6083fbbe026c4d [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:
andrew@webrtc.org23ec30b2012-11-15 05:33:25 +000027 virtual void Print(TraceLevel level, const char* msg, int length) {
andrew@webrtc.org50419b02012-11-14 19:07:54 +000028 CriticalSectionScoped cs(crit_.get());
29 // We test the length here to ensure (with high likelihood) that only our
30 // traces will be tested.
31 if (level_ != kTraceNone &&
32 expected_log_.str().size() == length - kBoilerplateLength - 1) {
33 EXPECT_EQ(level_, level);
34 EXPECT_EQ(expected_log_.str(), &msg[kBoilerplateLength]);
35 level_ = kTraceNone;
36 cv_->Wake();
37 }
38 }
39
40 protected:
41 LoggingTest()
42 : crit_(CriticalSectionWrapper::CreateCriticalSection()),
43 cv_(ConditionVariableWrapper::CreateConditionVariable()),
44 level_(kTraceNone),
45 expected_log_() {
46 }
47
48 void SetUp() {
49 Trace::CreateTrace();
50 Trace::SetTraceCallback(this);
51 // Reduce the chance that spurious traces will ruin the test.
52 Trace::SetLevelFilter(kTraceWarning | kTraceError);
53 }
54
55 void TearDown() {
56 CriticalSectionScoped cs(crit_.get());
57 Trace::SetTraceCallback(NULL);
58 Trace::ReturnTrace();
59 ASSERT_EQ(kTraceNone, level_) << "Print() was not called";
60 }
61
62 scoped_ptr<CriticalSectionWrapper> crit_;
63 scoped_ptr<ConditionVariableWrapper> cv_;
64 TraceLevel level_;
65 int length_;
66 std::ostringstream expected_log_;
67};
68
69TEST_F(LoggingTest, LogStream) {
70 {
71 CriticalSectionScoped cs(crit_.get());
72 level_ = kTraceWarning;
73 std::string msg = "Important message";
74 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 1 << "): " << msg;
andrew@webrtc.org655d8f52012-11-20 07:34:45 +000075 LOG(LS_WARNING) << msg;
andrew@webrtc.orgc3e5d342012-11-23 19:30:59 +000076 cv_->SleepCS(*crit_.get(), 2000);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000077 }
78}
79
80TEST_F(LoggingTest, LogFunctionError) {
81 {
82 CriticalSectionScoped cs(crit_.get());
83 int bar = 42;
84 int baz = 99;
85 level_ = kTraceError;
86 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 2
87 << "): Foo failed: bar=" << bar << ", baz=" << baz;
88 LOG_FERR2(LS_ERROR, Foo, bar, baz);
andrew@webrtc.orgc3e5d342012-11-23 19:30:59 +000089 cv_->SleepCS(*crit_.get(), 2000);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000090 }
91}
92
93} // namespace
94} // namespace webrtc