tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "rtc_base/event_tracer.h" |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 11 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 12 | #include <inttypes.h> |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | #include <stdio.h> |
| 15 | #include <string.h> |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 16 | #include <string> |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 19 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/critical_section.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/event.h" |
| 23 | #include "rtc_base/logging.h" |
| 24 | #include "rtc_base/platform_thread.h" |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 25 | #include "rtc_base/platform_thread_types.h" |
| 26 | #include "rtc_base/thread_annotations.h" |
| 27 | #include "rtc_base/thread_checker.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 28 | #include "rtc_base/time_utils.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 29 | #include "rtc_base/trace_event.h" |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 30 | |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 31 | // This is a guesstimate that should be enough in most cases. |
| 32 | static const size_t kEventLoggerArgsStrBufferInitialSize = 256; |
| 33 | static const size_t kTraceArgBufferLength = 32; |
| 34 | |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 35 | namespace webrtc { |
| 36 | |
| 37 | namespace { |
| 38 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 39 | GetCategoryEnabledPtr g_get_category_enabled_ptr = nullptr; |
| 40 | AddTraceEventPtr g_add_trace_event_ptr = nullptr; |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 41 | |
| 42 | } // namespace |
| 43 | |
| 44 | void SetupEventTracer(GetCategoryEnabledPtr get_category_enabled_ptr, |
| 45 | AddTraceEventPtr add_trace_event_ptr) { |
| 46 | g_get_category_enabled_ptr = get_category_enabled_ptr; |
| 47 | g_add_trace_event_ptr = add_trace_event_ptr; |
| 48 | } |
| 49 | |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 50 | const unsigned char* EventTracer::GetCategoryEnabled(const char* name) { |
| 51 | if (g_get_category_enabled_ptr) |
| 52 | return g_get_category_enabled_ptr(name); |
| 53 | |
| 54 | // A string with null terminator means category is disabled. |
| 55 | return reinterpret_cast<const unsigned char*>("\0"); |
| 56 | } |
| 57 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 58 | // Arguments to this function (phase, etc.) are as defined in |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 59 | // webrtc/rtc_base/trace_event.h. |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 60 | void EventTracer::AddTraceEvent(char phase, |
| 61 | const unsigned char* category_enabled, |
| 62 | const char* name, |
| 63 | unsigned long long id, |
| 64 | int num_args, |
| 65 | const char** arg_names, |
| 66 | const unsigned char* arg_types, |
| 67 | const unsigned long long* arg_values, |
| 68 | unsigned char flags) { |
| 69 | if (g_add_trace_event_ptr) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 70 | g_add_trace_event_ptr(phase, category_enabled, name, id, num_args, |
| 71 | arg_names, arg_types, arg_values, flags); |
tommi@webrtc.org | 7c64ed2 | 2015-03-17 14:25:37 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | } // namespace webrtc |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 76 | |
| 77 | namespace rtc { |
| 78 | namespace tracing { |
| 79 | namespace { |
| 80 | |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 81 | static void EventTracingThreadFunc(void* params); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 82 | |
| 83 | // Atomic-int fast path for avoiding logging when disabled. |
| 84 | static volatile int g_event_logging_active = 0; |
| 85 | |
| 86 | // TODO(pbos): Log metadata for all threads, etc. |
| 87 | class EventLogger final { |
| 88 | public: |
| 89 | EventLogger() |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 90 | : logging_thread_(EventTracingThreadFunc, |
| 91 | this, |
| 92 | "EventTracingThread", |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 93 | kLowPriority) {} |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 94 | ~EventLogger() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); } |
| 95 | |
| 96 | void AddTraceEvent(const char* name, |
| 97 | const unsigned char* category_enabled, |
| 98 | char phase, |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 99 | int num_args, |
| 100 | const char** arg_names, |
| 101 | const unsigned char* arg_types, |
| 102 | const unsigned long long* arg_values, |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 103 | uint64_t timestamp, |
| 104 | int pid, |
| 105 | rtc::PlatformThreadId thread_id) { |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 106 | std::vector<TraceArg> args(num_args); |
| 107 | for (int i = 0; i < num_args; ++i) { |
| 108 | TraceArg& arg = args[i]; |
| 109 | arg.name = arg_names[i]; |
| 110 | arg.type = arg_types[i]; |
| 111 | arg.value.as_uint = arg_values[i]; |
| 112 | |
| 113 | // Value is a pointer to a temporary string, so we have to make a copy. |
| 114 | if (arg.type == TRACE_VALUE_TYPE_COPY_STRING) { |
| 115 | // Space for the string and for the terminating null character. |
| 116 | size_t str_length = strlen(arg.value.as_string) + 1; |
| 117 | char* str_copy = new char[str_length]; |
| 118 | memcpy(str_copy, arg.value.as_string, str_length); |
| 119 | arg.value.as_string = str_copy; |
| 120 | } |
| 121 | } |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 122 | rtc::CritScope lock(&crit_); |
| 123 | trace_events_.push_back( |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 124 | {name, category_enabled, phase, args, timestamp, 1, thread_id}); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 127 | // The TraceEvent format is documented here: |
| 128 | // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 129 | void Log() { |
| 130 | RTC_DCHECK(output_file_); |
| 131 | static const int kLoggingIntervalMs = 100; |
| 132 | fprintf(output_file_, "{ \"traceEvents\": [\n"); |
| 133 | bool has_logged_event = false; |
| 134 | while (true) { |
| 135 | bool shutting_down = shutdown_event_.Wait(kLoggingIntervalMs); |
| 136 | std::vector<TraceEvent> events; |
| 137 | { |
| 138 | rtc::CritScope lock(&crit_); |
| 139 | trace_events_.swap(events); |
| 140 | } |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 141 | std::string args_str; |
| 142 | args_str.reserve(kEventLoggerArgsStrBufferInitialSize); |
| 143 | for (TraceEvent& e : events) { |
| 144 | args_str.clear(); |
| 145 | if (!e.args.empty()) { |
| 146 | args_str += ", \"args\": {"; |
| 147 | bool is_first_argument = true; |
| 148 | for (TraceArg& arg : e.args) { |
| 149 | if (!is_first_argument) |
| 150 | args_str += ","; |
| 151 | is_first_argument = false; |
| 152 | args_str += " \""; |
| 153 | args_str += arg.name; |
| 154 | args_str += "\": "; |
| 155 | args_str += TraceArgValueAsString(arg); |
| 156 | |
| 157 | // Delete our copy of the string. |
| 158 | if (arg.type == TRACE_VALUE_TYPE_COPY_STRING) { |
| 159 | delete[] arg.value.as_string; |
| 160 | arg.value.as_string = nullptr; |
| 161 | } |
| 162 | } |
| 163 | args_str += " }"; |
| 164 | } |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 165 | fprintf(output_file_, |
| 166 | "%s{ \"name\": \"%s\"" |
| 167 | ", \"cat\": \"%s\"" |
| 168 | ", \"ph\": \"%c\"" |
| 169 | ", \"ts\": %" PRIu64 |
| 170 | ", \"pid\": %d" |
Peter Boström | 43e4e23 | 2015-12-11 20:29:35 +0100 | [diff] [blame] | 171 | #if defined(WEBRTC_WIN) |
| 172 | ", \"tid\": %lu" |
| 173 | #else |
| 174 | ", \"tid\": %d" |
| 175 | #endif // defined(WEBRTC_WIN) |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 176 | "%s" |
Peter Boström | 43e4e23 | 2015-12-11 20:29:35 +0100 | [diff] [blame] | 177 | "}\n", |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 178 | has_logged_event ? "," : " ", e.name, e.category_enabled, |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 179 | e.phase, e.timestamp, e.pid, e.tid, args_str.c_str()); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 180 | has_logged_event = true; |
| 181 | } |
| 182 | if (shutting_down) |
| 183 | break; |
| 184 | } |
| 185 | fprintf(output_file_, "]}\n"); |
| 186 | if (output_file_owned_) |
| 187 | fclose(output_file_); |
| 188 | output_file_ = nullptr; |
| 189 | } |
| 190 | |
| 191 | void Start(FILE* file, bool owned) { |
Peter Boström | dda8a83 | 2016-02-29 14:54:04 +0100 | [diff] [blame] | 192 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 193 | RTC_DCHECK(file); |
| 194 | RTC_DCHECK(!output_file_); |
| 195 | output_file_ = file; |
| 196 | output_file_owned_ = owned; |
| 197 | { |
| 198 | rtc::CritScope lock(&crit_); |
| 199 | // Since the atomic fast-path for adding events to the queue can be |
| 200 | // bypassed while the logging thread is shutting down there may be some |
| 201 | // stale events in the queue, hence the vector needs to be cleared to not |
| 202 | // log events from a previous logging session (which may be days old). |
| 203 | trace_events_.clear(); |
| 204 | } |
| 205 | // Enable event logging (fast-path). This should be disabled since starting |
| 206 | // shouldn't be done twice. |
| 207 | RTC_CHECK_EQ(0, |
| 208 | rtc::AtomicOps::CompareAndSwap(&g_event_logging_active, 0, 1)); |
| 209 | |
| 210 | // Finally start, everything should be set up now. |
| 211 | logging_thread_.Start(); |
Peter Boström | dda8a83 | 2016-02-29 14:54:04 +0100 | [diff] [blame] | 212 | TRACE_EVENT_INSTANT0("webrtc", "EventLogger::Start"); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void Stop() { |
Peter Boström | dda8a83 | 2016-02-29 14:54:04 +0100 | [diff] [blame] | 216 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 217 | TRACE_EVENT_INSTANT0("webrtc", "EventLogger::Stop"); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 218 | // Try to stop. Abort if we're not currently logging. |
| 219 | if (rtc::AtomicOps::CompareAndSwap(&g_event_logging_active, 1, 0) == 0) |
| 220 | return; |
| 221 | |
| 222 | // Wake up logging thread to finish writing. |
| 223 | shutdown_event_.Set(); |
| 224 | // Join the logging thread. |
| 225 | logging_thread_.Stop(); |
| 226 | } |
| 227 | |
| 228 | private: |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 229 | struct TraceArg { |
| 230 | const char* name; |
| 231 | unsigned char type; |
kjellander | e96c45b | 2017-06-30 10:45:21 -0700 | [diff] [blame] | 232 | // Copied from webrtc/rtc_base/trace_event.h TraceValueUnion. |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 233 | union TraceArgValue { |
| 234 | bool as_bool; |
| 235 | unsigned long long as_uint; |
| 236 | long long as_int; |
| 237 | double as_double; |
| 238 | const void* as_pointer; |
| 239 | const char* as_string; |
| 240 | } value; |
| 241 | |
| 242 | // Assert that the size of the union is equal to the size of the as_uint |
| 243 | // field since we are assigning to arbitrary types using it. |
| 244 | static_assert(sizeof(TraceArgValue) == sizeof(unsigned long long), |
| 245 | "Size of TraceArg value union is not equal to the size of " |
| 246 | "the uint field of that union."); |
| 247 | }; |
| 248 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 249 | struct TraceEvent { |
| 250 | const char* name; |
| 251 | const unsigned char* category_enabled; |
| 252 | char phase; |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 253 | std::vector<TraceArg> args; |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 254 | uint64_t timestamp; |
| 255 | int pid; |
| 256 | rtc::PlatformThreadId tid; |
| 257 | }; |
| 258 | |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 259 | static std::string TraceArgValueAsString(TraceArg arg) { |
| 260 | std::string output; |
| 261 | |
| 262 | if (arg.type == TRACE_VALUE_TYPE_STRING || |
| 263 | arg.type == TRACE_VALUE_TYPE_COPY_STRING) { |
| 264 | // Space for every character to be an espaced character + two for |
| 265 | // quatation marks. |
| 266 | output.reserve(strlen(arg.value.as_string) * 2 + 2); |
| 267 | output += '\"'; |
| 268 | const char* c = arg.value.as_string; |
| 269 | do { |
| 270 | if (*c == '"' || *c == '\\') { |
| 271 | output += '\\'; |
| 272 | output += *c; |
| 273 | } else { |
| 274 | output += *c; |
| 275 | } |
| 276 | } while (*++c); |
| 277 | output += '\"'; |
| 278 | } else { |
| 279 | output.resize(kTraceArgBufferLength); |
| 280 | size_t print_length = 0; |
| 281 | switch (arg.type) { |
| 282 | case TRACE_VALUE_TYPE_BOOL: |
| 283 | if (arg.value.as_bool) { |
| 284 | strcpy(&output[0], "true"); |
| 285 | print_length = 4; |
| 286 | } else { |
| 287 | strcpy(&output[0], "false"); |
| 288 | print_length = 5; |
| 289 | } |
| 290 | break; |
| 291 | case TRACE_VALUE_TYPE_UINT: |
Niels Möller | aba0633 | 2018-10-16 15:14:15 +0200 | [diff] [blame] | 292 | print_length = snprintf(&output[0], kTraceArgBufferLength, "%llu", |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 293 | arg.value.as_uint); |
| 294 | break; |
| 295 | case TRACE_VALUE_TYPE_INT: |
Niels Möller | aba0633 | 2018-10-16 15:14:15 +0200 | [diff] [blame] | 296 | print_length = snprintf(&output[0], kTraceArgBufferLength, "%lld", |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 297 | arg.value.as_int); |
| 298 | break; |
| 299 | case TRACE_VALUE_TYPE_DOUBLE: |
Niels Möller | aba0633 | 2018-10-16 15:14:15 +0200 | [diff] [blame] | 300 | print_length = snprintf(&output[0], kTraceArgBufferLength, "%f", |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 301 | arg.value.as_double); |
| 302 | break; |
| 303 | case TRACE_VALUE_TYPE_POINTER: |
Niels Möller | aba0633 | 2018-10-16 15:14:15 +0200 | [diff] [blame] | 304 | print_length = snprintf(&output[0], kTraceArgBufferLength, "\"%p\"", |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 305 | arg.value.as_pointer); |
| 306 | break; |
| 307 | } |
| 308 | size_t output_length = print_length < kTraceArgBufferLength |
| 309 | ? print_length |
| 310 | : kTraceArgBufferLength - 1; |
| 311 | // This will hopefully be very close to nop. On most implementations, it |
| 312 | // just writes null byte and sets the length field of the string. |
| 313 | output.resize(output_length); |
| 314 | } |
| 315 | |
| 316 | return output; |
| 317 | } |
| 318 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 319 | rtc::CriticalSection crit_; |
danilchap | 3c6abd2 | 2017-09-06 05:46:29 -0700 | [diff] [blame] | 320 | std::vector<TraceEvent> trace_events_ RTC_GUARDED_BY(crit_); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 321 | rtc::PlatformThread logging_thread_; |
| 322 | rtc::Event shutdown_event_; |
| 323 | rtc::ThreadChecker thread_checker_; |
| 324 | FILE* output_file_ = nullptr; |
| 325 | bool output_file_owned_ = false; |
| 326 | }; |
| 327 | |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 328 | static void EventTracingThreadFunc(void* params) { |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 329 | static_cast<EventLogger*>(params)->Log(); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static EventLogger* volatile g_event_logger = nullptr; |
| 333 | static const char* const kDisabledTracePrefix = TRACE_DISABLED_BY_DEFAULT(""); |
| 334 | const unsigned char* InternalGetCategoryEnabled(const char* name) { |
| 335 | const char* prefix_ptr = &kDisabledTracePrefix[0]; |
| 336 | const char* name_ptr = name; |
| 337 | // Check whether name contains the default-disabled prefix. |
| 338 | while (*prefix_ptr == *name_ptr && *prefix_ptr != '\0') { |
| 339 | ++prefix_ptr; |
| 340 | ++name_ptr; |
| 341 | } |
| 342 | return reinterpret_cast<const unsigned char*>(*prefix_ptr == '\0' ? "" |
| 343 | : name); |
| 344 | } |
| 345 | |
| 346 | void InternalAddTraceEvent(char phase, |
| 347 | const unsigned char* category_enabled, |
| 348 | const char* name, |
| 349 | unsigned long long id, |
| 350 | int num_args, |
| 351 | const char** arg_names, |
| 352 | const unsigned char* arg_types, |
| 353 | const unsigned long long* arg_values, |
| 354 | unsigned char flags) { |
| 355 | // Fast path for when event tracing is inactive. |
| 356 | if (rtc::AtomicOps::AcquireLoad(&g_event_logging_active) == 0) |
| 357 | return; |
| 358 | |
sakal | f4433ef | 2016-08-08 04:10:13 -0700 | [diff] [blame] | 359 | g_event_logger->AddTraceEvent(name, category_enabled, phase, num_args, |
| 360 | arg_names, arg_types, arg_values, |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 361 | rtc::TimeMicros(), 1, rtc::CurrentThreadId()); |
| 362 | } |
| 363 | |
| 364 | } // namespace |
| 365 | |
| 366 | void SetupInternalTracer() { |
| 367 | RTC_CHECK(rtc::AtomicOps::CompareAndSwapPtr( |
| 368 | &g_event_logger, static_cast<EventLogger*>(nullptr), |
| 369 | new EventLogger()) == nullptr); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 370 | webrtc::SetupEventTracer(InternalGetCategoryEnabled, InternalAddTraceEvent); |
| 371 | } |
| 372 | |
| 373 | void StartInternalCaptureToFile(FILE* file) { |
jtteh | 7480da4 | 2017-07-07 11:02:15 -0700 | [diff] [blame] | 374 | if (g_event_logger) { |
| 375 | g_event_logger->Start(file, false); |
| 376 | } |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | bool StartInternalCapture(const char* filename) { |
jtteh | 7480da4 | 2017-07-07 11:02:15 -0700 | [diff] [blame] | 380 | if (!g_event_logger) |
| 381 | return false; |
| 382 | |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 383 | FILE* file = fopen(filename, "w"); |
| 384 | if (!file) { |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 385 | RTC_LOG(LS_ERROR) << "Failed to open trace file '" << filename |
| 386 | << "' for writing."; |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 387 | return false; |
| 388 | } |
| 389 | g_event_logger->Start(file, true); |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | void StopInternalCapture() { |
jtteh | 7480da4 | 2017-07-07 11:02:15 -0700 | [diff] [blame] | 394 | if (g_event_logger) { |
| 395 | g_event_logger->Stop(); |
| 396 | } |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void ShutdownInternalTracer() { |
| 400 | StopInternalCapture(); |
Peter Boström | 455a252 | 2015-12-18 17:00:25 +0100 | [diff] [blame] | 401 | EventLogger* old_logger = rtc::AtomicOps::AcquireLoadPtr(&g_event_logger); |
Peter Boström | 6f28cf0 | 2015-12-07 23:17:15 +0100 | [diff] [blame] | 402 | RTC_DCHECK(old_logger); |
| 403 | RTC_CHECK(rtc::AtomicOps::CompareAndSwapPtr( |
| 404 | &g_event_logger, old_logger, |
| 405 | static_cast<EventLogger*>(nullptr)) == old_logger); |
| 406 | delete old_logger; |
| 407 | webrtc::SetupEventTracer(nullptr, nullptr); |
| 408 | } |
| 409 | |
| 410 | } // namespace tracing |
| 411 | } // namespace rtc |