blob: 2556d24fe5a447e6781f2f8a2d0c17fc317700a1 [file] [log] [blame]
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/event_tracer.h"
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000012
Yves Gerey50b0baf2019-09-06 10:03:54 +020013#include "rtc_base/critical_section.h"
14#include "rtc_base/thread_annotations.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/trace_event.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "test/gtest.h"
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000017
18namespace {
19
20class TestStatistics {
21 public:
Yves Gerey50b0baf2019-09-06 10:03:54 +020022 void Reset() {
23 rtc::CritScope cs(&crit_);
24 events_logged_ = 0;
25 }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000026
Yves Gerey50b0baf2019-09-06 10:03:54 +020027 void Increment() {
28 rtc::CritScope cs(&crit_);
29 ++events_logged_;
30 }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000031
Yves Gerey50b0baf2019-09-06 10:03:54 +020032 int Count() const {
33 rtc::CritScope cs(&crit_);
34 return events_logged_;
35 }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000036
37 static TestStatistics* Get() {
Yves Gerey50b0baf2019-09-06 10:03:54 +020038 // google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
39 static auto& test_stats = *new TestStatistics();
40 return &test_stats;
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000041 }
42
43 private:
Yves Gerey50b0baf2019-09-06 10:03:54 +020044 rtc::CriticalSection crit_;
45 int events_logged_ RTC_GUARDED_BY(crit_) = 0;
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000046};
47
Yves Gerey50b0baf2019-09-06 10:03:54 +020048const unsigned char* GetCategoryEnabledHandler(const char* /*name*/) {
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000049 return reinterpret_cast<const unsigned char*>("test");
50}
51
Yves Gerey50b0baf2019-09-06 10:03:54 +020052void TraceEventHandler(char /*phase*/,
53 const unsigned char* /*category_enabled*/,
54 const char* /*name*/,
55 unsigned long long /*id*/,
56 int /*num_args*/,
57 const char** /*arg_names*/,
58 const unsigned char* /*arg_types*/,
59 const unsigned long long* /*arg_values*/,
60 unsigned char /*flags*/) {
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000061 TestStatistics::Get()->Increment();
62}
63
64} // namespace
65
66namespace webrtc {
67
68TEST(EventTracerTest, EventTracerDisabled) {
Yves Gerey665174f2018-06-19 15:03:05 +020069 { TRACE_EVENT0("test", "EventTracerDisabled"); }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000070 EXPECT_FALSE(TestStatistics::Get()->Count());
71 TestStatistics::Get()->Reset();
72}
73
74TEST(EventTracerTest, ScopedTraceEvent) {
Yves Gerey50b0baf2019-09-06 10:03:54 +020075 SetupEventTracer(&GetCategoryEnabledHandler, &TraceEventHandler);
Yves Gerey665174f2018-06-19 15:03:05 +020076 { TRACE_EVENT0("test", "ScopedTraceEvent"); }
tommi@webrtc.org7c64ed22015-03-17 14:25:37 +000077 EXPECT_EQ(2, TestStatistics::Get()->Count());
78 TestStatistics::Get()->Reset();
79}
80
81} // namespace webrtc