blob: 19f13940cc243718d07cca97709f6533e6d4097d [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
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000013#include "testing/gtest/include/gtest/gtest.h"
andrew@webrtc.org50419b02012-11-14 19:07:54 +000014#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
andrew@webrtc.org50419b02012-11-14 19:07:54 +000023class LoggingTest : public ::testing::Test, public TraceCallback {
24 public:
andrew@webrtc.org23ec30b2012-11-15 05:33:25 +000025 virtual void Print(TraceLevel level, const char* msg, int length) {
andrew@webrtc.org50419b02012-11-14 19:07:54 +000026 CriticalSectionScoped cs(crit_.get());
27 // We test the length here to ensure (with high likelihood) that only our
28 // traces will be tested.
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +000029 if (level_ != kTraceNone && static_cast<int>(expected_log_.str().size()) ==
30 length - Trace::kBoilerplateLength - 1) {
andrew@webrtc.org50419b02012-11-14 19:07:54 +000031 EXPECT_EQ(level_, level);
andrew@webrtc.orgc1ffd332013-03-22 17:13:23 +000032 EXPECT_EQ(expected_log_.str(), &msg[Trace::kBoilerplateLength]);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000033 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 // Reduce the chance that spurious traces will ruin the test.
andrew@webrtc.org90805182013-09-05 16:40:43 +000050 Trace::set_level_filter(kTraceWarning | kTraceError);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000051 }
52
53 void TearDown() {
54 CriticalSectionScoped cs(crit_.get());
55 Trace::SetTraceCallback(NULL);
56 Trace::ReturnTrace();
57 ASSERT_EQ(kTraceNone, level_) << "Print() was not called";
58 }
59
60 scoped_ptr<CriticalSectionWrapper> crit_;
61 scoped_ptr<ConditionVariableWrapper> cv_;
62 TraceLevel level_;
63 int length_;
64 std::ostringstream expected_log_;
65};
66
67TEST_F(LoggingTest, LogStream) {
68 {
69 CriticalSectionScoped cs(crit_.get());
70 level_ = kTraceWarning;
71 std::string msg = "Important message";
72 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 1 << "): " << msg;
andrew@webrtc.org655d8f52012-11-20 07:34:45 +000073 LOG(LS_WARNING) << msg;
andrew@webrtc.orgc3e5d342012-11-23 19:30:59 +000074 cv_->SleepCS(*crit_.get(), 2000);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000075 }
76}
77
78TEST_F(LoggingTest, LogFunctionError) {
79 {
80 CriticalSectionScoped cs(crit_.get());
81 int bar = 42;
82 int baz = 99;
83 level_ = kTraceError;
84 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 2
85 << "): Foo failed: bar=" << bar << ", baz=" << baz;
86 LOG_FERR2(LS_ERROR, Foo, bar, baz);
andrew@webrtc.orgc3e5d342012-11-23 19:30:59 +000087 cv_->SleepCS(*crit_.get(), 2000);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000088 }
89}
90
91} // namespace
92} // namespace webrtc