blob: 2da24b26f443ffeb912b0d6b987e1f278757a04a [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
Henrik Kjellander98f53512015-10-28 18:17:40 +010011#include "webrtc/system_wrappers/include/logging.h"
andrew@webrtc.org50419b02012-11-14 19:07:54 +000012
pbos@webrtc.orgacaf3a12013-05-27 15:07:45 +000013#include "testing/gtest/include/gtest/gtest.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000014#include "webrtc/base/scoped_ptr.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010015#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"
andrew@webrtc.org50419b02012-11-14 19:07:54 +000019
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);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000049 }
50
51 void TearDown() {
andrew@webrtc.org50419b02012-11-14 19:07:54 +000052 Trace::SetTraceCallback(NULL);
53 Trace::ReturnTrace();
andresp@webrtc.org285e9bc2014-07-07 20:27:33 +000054 CriticalSectionScoped cs(crit_.get());
andrew@webrtc.org50419b02012-11-14 19:07:54 +000055 ASSERT_EQ(kTraceNone, level_) << "Print() was not called";
56 }
57
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000058 rtc::scoped_ptr<CriticalSectionWrapper> crit_;
59 rtc::scoped_ptr<ConditionVariableWrapper> cv_;
andresp@webrtc.org285e9bc2014-07-07 20:27:33 +000060 TraceLevel level_ GUARDED_BY(crit_);
61 std::ostringstream expected_log_ GUARDED_BY(crit_);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000062};
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;
andrew@webrtc.org655d8f52012-11-20 07:34:45 +000070 LOG(LS_WARNING) << msg;
andrew@webrtc.orgc3e5d342012-11-23 19:30:59 +000071 cv_->SleepCS(*crit_.get(), 2000);
andrew@webrtc.org50419b02012-11-14 19:07:54 +000072 }
73}
74
andrew@webrtc.org50419b02012-11-14 19:07:54 +000075} // namespace
76} // namespace webrtc