blob: d1eab32e4b7a6e7ad9a3354e79ad2f2595dc5cf8 [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() {
andrew@webrtc.org50419b02012-11-14 19:07:54 +000054 Trace::SetTraceCallback(NULL);
55 Trace::ReturnTrace();
andresp@webrtc.org285e9bc2014-07-07 20:27:33 +000056 CriticalSectionScoped cs(crit_.get());
andrew@webrtc.org50419b02012-11-14 19:07:54 +000057 ASSERT_EQ(kTraceNone, level_) << "Print() was not called";
58 }
59
60 scoped_ptr<CriticalSectionWrapper> crit_;
61 scoped_ptr<ConditionVariableWrapper> cv_;
andresp@webrtc.org285e9bc2014-07-07 20:27:33 +000062 TraceLevel level_ GUARDED_BY(crit_);
63 std::ostringstream expected_log_ GUARDED_BY(crit_);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000064};
65
66TEST_F(LoggingTest, LogStream) {
67 {
68 CriticalSectionScoped cs(crit_.get());
69 level_ = kTraceWarning;
70 std::string msg = "Important message";
71 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 1 << "): " << msg;
andrew@webrtc.org655d8f52012-11-20 07:34:45 +000072 LOG(LS_WARNING) << msg;
andrew@webrtc.orgc3e5d342012-11-23 19:30:59 +000073 cv_->SleepCS(*crit_.get(), 2000);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000074 }
75}
76
77TEST_F(LoggingTest, LogFunctionError) {
78 {
79 CriticalSectionScoped cs(crit_.get());
80 int bar = 42;
81 int baz = 99;
82 level_ = kTraceError;
83 expected_log_ << "(logging_unittest.cc:" << __LINE__ + 2
84 << "): Foo failed: bar=" << bar << ", baz=" << baz;
85 LOG_FERR2(LS_ERROR, Foo, bar, baz);
andrew@webrtc.orgc3e5d342012-11-23 19:30:59 +000086 cv_->SleepCS(*crit_.get(), 2000);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000087 }
88}
89
90} // namespace
91} // namespace webrtc