Andrew Top | 0d1858f | 2019-05-15 22:01:47 -0700 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_AUTO_OPEN_CLOSE_EVENT_H_ |
| 6 | #define BASE_AUTO_OPEN_CLOSE_EVENT_H_ |
| 7 | |
| 8 | #include "base/macros.h" |
| 9 | #include "base/memory/weak_ptr.h" |
| 10 | #include "base/threading/thread_checker.h" |
| 11 | #include "base/time/time.h" |
| 12 | #include "base/trace_event/trace_event.h" |
| 13 | |
| 14 | namespace base { |
| 15 | namespace trace_event { |
| 16 | |
| 17 | // Class for tracing events that support "auto-opening" and "auto-closing". |
| 18 | // "auto-opening" = if the trace event is started (call Begin() before |
| 19 | // tracing is started,the trace event will be opened, with the start time |
| 20 | // being the time that the trace event was actually started. |
| 21 | // "auto-closing" = if the trace event is started but not ended by the time |
| 22 | // tracing ends, then the trace event will be automatically closed at the |
| 23 | // end of tracing. |
| 24 | class BASE_EXPORT AutoOpenCloseEvent |
| 25 | : public TraceLog::AsyncEnabledStateObserver { |
| 26 | public: |
| 27 | enum Type { |
| 28 | ASYNC |
| 29 | }; |
| 30 | |
| 31 | // As in the rest of the tracing macros, the const char* arguments here |
| 32 | // must be pointers to indefinitely lived strings (e.g. hard-coded string |
| 33 | // literals are okay, but not strings created by c_str()) |
| 34 | AutoOpenCloseEvent(Type type, const char* category, const char* event_name); |
| 35 | ~AutoOpenCloseEvent() override; |
| 36 | |
| 37 | void Begin(); |
| 38 | void End(); |
| 39 | |
| 40 | // AsyncEnabledStateObserver implementation |
| 41 | void OnTraceLogEnabled() override; |
| 42 | void OnTraceLogDisabled() override; |
| 43 | |
| 44 | private: |
| 45 | const char* const category_; |
| 46 | const char* const event_name_; |
| 47 | base::TimeTicks start_time_; |
| 48 | base::ThreadChecker thread_checker_; |
| 49 | WeakPtrFactory<AutoOpenCloseEvent> weak_factory_; |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(AutoOpenCloseEvent); |
| 52 | }; |
| 53 | |
| 54 | } // namespace trace_event |
| 55 | } // namespace base |
| 56 | |
| 57 | #endif // BASE_AUTO_OPEN_CLOSE_EVENT_H_ |