blob: 695b03f93a09cb7cd6e7ee34c73a31618303792a [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"
tommiaff4b702016-01-18 10:20:17 -080014#include "webrtc/base/arraysize.h"
15#include "webrtc/base/event.h"
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000016#include "webrtc/base/scoped_ptr.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010017#include "webrtc/system_wrappers/include/trace.h"
andrew@webrtc.org50419b02012-11-14 19:07:54 +000018
19namespace webrtc {
20namespace {
tommiaff4b702016-01-18 10:20:17 -080021const char kTestLogString[] = "Incredibly important test message!(?)";
22const int kTestLevel = kTraceWarning;
andrew@webrtc.org50419b02012-11-14 19:07:54 +000023
tommiaff4b702016-01-18 10:20:17 -080024class LoggingTestCallback : public TraceCallback {
andrew@webrtc.org50419b02012-11-14 19:07:54 +000025 public:
tommiaff4b702016-01-18 10:20:17 -080026 LoggingTestCallback(rtc::Event* event) : event_(event) {}
27
28 private:
29 void Print(TraceLevel level, const char* msg, int length) override {
30 if (static_cast<size_t>(length) < arraysize(kTestLogString) ||
31 level != kTestLevel) {
32 return;
andrew@webrtc.org50419b02012-11-14 19:07:54 +000033 }
tommiaff4b702016-01-18 10:20:17 -080034
35 std::string msg_str(msg, length);
36 if (msg_str.find(kTestLogString) != std::string::npos)
37 event_->Set();
andrew@webrtc.org50419b02012-11-14 19:07:54 +000038 }
39
tommiaff4b702016-01-18 10:20:17 -080040 rtc::Event* const event_;
andrew@webrtc.org50419b02012-11-14 19:07:54 +000041};
42
andrew@webrtc.org50419b02012-11-14 19:07:54 +000043} // namespace
tommiaff4b702016-01-18 10:20:17 -080044
45TEST(LoggingTest, LogStream) {
46 Trace::CreateTrace();
47
48 rtc::Event event(false, false);
49 LoggingTestCallback callback(&event);
50 Trace::SetTraceCallback(&callback);
51
52 LOG(LS_WARNING) << kTestLogString;
53 EXPECT_TRUE(event.Wait(2000));
54
55 Trace::SetTraceCallback(nullptr);
56 Trace::ReturnTrace();
57}
andrew@webrtc.org50419b02012-11-14 19:07:54 +000058} // namespace webrtc