OLD | NEW |
---|---|
(Empty) | |
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_DEBUG_BLAME_CONTEXT_H_ | |
6 #define BASE_DEBUG_BLAME_CONTEXT_H_ | |
7 | |
8 #include <inttypes.h> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "base/trace_event/trace_event.h" | |
13 #include "base/trace_event/trace_event_argument.h" | |
14 | |
15 namespace base { | |
16 namespace debug { | |
17 | |
18 class BlameContextBase { | |
19 public: | |
20 virtual void Enter() = 0; | |
21 virtual void Leave() = 0; | |
22 | |
23 protected: | |
24 BlameContextBase() {} | |
25 virtual ~BlameContextBase() {} | |
26 | |
27 DISALLOW_COPY_AND_ASSIGN(BlameContextBase); | |
28 }; | |
29 | |
30 template <const char* category, const char* name, const char* scope> | |
benjhayden
2016/02/29 19:30:37
It seems like this just requires subclasses and us
Sami
2016/02/29 20:20:31
I agree that this is a little weird but I don't se
| |
31 class BlameContext : public BlameContextBase, | |
32 trace_event::TraceLog::EnabledStateObserver { | |
33 public: | |
34 explicit BlameContext(int64_t id) | |
35 : id_(id), | |
36 parent_id_(0), | |
37 parent_scope_(nullptr), | |
38 trace_object_(category, name, TRACE_ID_WITH_SCOPE(scope, id)) { | |
39 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
40 } | |
41 | |
42 template <const char* parent_category, | |
43 const char* parent_name, | |
44 const char* parent_scope> | |
45 BlameContext(int64_t id, | |
46 const BlameContext<parent_category, parent_name, parent_scope>& | |
47 parent_context) | |
48 : id_(id), | |
49 parent_id_(parent_context.id()), | |
50 parent_scope_(parent_scope), | |
51 trace_object_(category, name, TRACE_ID_WITH_SCOPE(scope, id)) { | |
52 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
53 } | |
54 | |
55 // BlameContextBase implementation: | |
56 void Enter() override { | |
57 TRACE_EVENT_ENTER_CONTEXT(category, name, TRACE_ID_WITH_SCOPE(scope, id_)); | |
58 } | |
59 | |
60 void Leave() override { | |
61 TRACE_EVENT_LEAVE_CONTEXT(category, name, TRACE_ID_WITH_SCOPE(scope, id_)); | |
62 } | |
63 | |
64 void TakeSnapshot() { | |
65 bool is_tracing = false; | |
benjhayden
2016/02/29 19:30:37
Looks like most of this method could be moved into
Sami
2016/02/29 20:20:31
Right, we could move it there. I think I'd still w
| |
66 TRACE_EVENT_CATEGORY_GROUP_ENABLED(category, &is_tracing); | |
67 if (!is_tracing) | |
68 return; | |
69 scoped_refptr<trace_event::TracedValue> snapshot( | |
70 new trace_event::TracedValue); | |
71 AsValueInto(snapshot.get()); | |
72 scoped_refptr<trace_event::ConvertableToTraceFormat> snapshot_data( | |
73 snapshot); | |
74 trace_object_.Snapshot(snapshot_data); | |
75 } | |
76 | |
77 int64_t id() const { return id_; } | |
78 | |
79 // trace_event::TraceLog::EnabledStateObserver implementation: | |
80 void OnTraceLogEnabled() override { TakeSnapshot(); } | |
81 | |
82 void OnTraceLogDisabled() override {} | |
83 | |
84 protected: | |
85 virtual ~BlameContext() { | |
86 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
87 } | |
88 | |
89 virtual void AsValueInto(trace_event::TracedValue* state) { | |
90 if (!parent_id_) | |
91 return; | |
92 state->BeginDictionary("parent"); | |
93 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); | |
94 state->SetString("scope", parent_scope_); | |
95 state->EndDictionary(); | |
96 } | |
97 | |
98 private: | |
99 const int64_t id_; | |
100 const int64_t parent_id_; | |
101 const char* parent_scope_; | |
102 | |
103 trace_event::TraceScopedTrackableObject<trace_event::TraceID::WithScope> | |
104 trace_object_; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(BlameContext); | |
107 }; | |
108 | |
109 } // namespace debug | |
110 } // namespace base | |
111 | |
112 #endif // BASE_DEBUG_BLAME_CONTEXT_H_ | |
OLD | NEW |