Add ICU message format support
[chromium-blink-merge.git] / base / trace_event / trace_event_win.cc
blobfdbd35a0e2feda383072f9c3e78a1b5668b919e1
1 // Copyright (c) 2012 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.
5 #include "base/trace_event/trace_event_win.h"
7 #include "base/logging.h"
8 #include "base/memory/singleton.h"
9 #include <initguid.h> // NOLINT
11 namespace base {
12 namespace trace_event {
14 using base::win::EtwEventType;
15 using base::win::EtwMofEvent;
17 // {3DADA31D-19EF-4dc1-B345-037927193422}
18 const GUID kChromeTraceProviderName = {
19 0x3dada31d,
20 0x19ef,
21 0x4dc1,
22 {0xb3, 0x45, 0x3, 0x79, 0x27, 0x19, 0x34, 0x22}};
24 // {B967AE67-BB22-49d7-9406-55D91EE1D560}
25 const GUID kTraceEventClass32 = {
26 0xb967ae67,
27 0xbb22,
28 0x49d7,
29 {0x94, 0x6, 0x55, 0xd9, 0x1e, 0xe1, 0xd5, 0x60}};
31 // {97BE602D-2930-4ac3-8046-B6763B631DFE}
32 const GUID kTraceEventClass64 = {
33 0x97be602d,
34 0x2930,
35 0x4ac3,
36 {0x80, 0x46, 0xb6, 0x76, 0x3b, 0x63, 0x1d, 0xfe}};
38 TraceEventETWProvider::TraceEventETWProvider() :
39 EtwTraceProvider(kChromeTraceProviderName) {
40 Register();
43 TraceEventETWProvider* TraceEventETWProvider::GetInstance() {
44 return Singleton<TraceEventETWProvider,
45 StaticMemorySingletonTraits<TraceEventETWProvider> >::get();
48 bool TraceEventETWProvider::StartTracing() {
49 return true;
52 void TraceEventETWProvider::TraceEvent(const char* name,
53 size_t name_len,
54 char type,
55 const void* id,
56 const char* extra,
57 size_t extra_len) {
58 // Make sure we don't touch NULL.
59 if (name == NULL)
60 name = "";
61 if (extra == NULL)
62 extra = "";
64 EtwEventType etw_type = 0;
65 switch (type) {
66 case TRACE_EVENT_PHASE_BEGIN:
67 etw_type = kTraceEventTypeBegin;
68 break;
69 case TRACE_EVENT_PHASE_END:
70 etw_type = kTraceEventTypeEnd;
71 break;
73 case TRACE_EVENT_PHASE_INSTANT:
74 etw_type = kTraceEventTypeInstant;
75 break;
77 default:
78 NOTREACHED() << "Unknown event type";
79 etw_type = kTraceEventTypeInstant;
80 break;
83 EtwMofEvent<5> event(kTraceEventClass32,
84 etw_type,
85 TRACE_LEVEL_INFORMATION);
86 event.SetField(0, name_len + 1, name);
87 event.SetField(1, sizeof(id), &id);
88 event.SetField(2, extra_len + 1, extra);
90 // These variables are declared here so that they are not out of scope when
91 // the event is logged.
92 DWORD depth;
93 void* backtrace[32];
95 // See whether we're to capture a backtrace.
96 if (enable_flags() & CAPTURE_STACK_TRACE) {
97 depth = CaptureStackBackTrace(0,
98 arraysize(backtrace),
99 backtrace,
100 NULL);
101 event.SetField(3, sizeof(depth), &depth);
102 event.SetField(4, sizeof(backtrace[0]) * depth, backtrace);
105 // Trace the event.
106 Log(event.get());
109 void TraceEventETWProvider::Trace(const char* name,
110 size_t name_len,
111 char type,
112 const void* id,
113 const char* extra,
114 size_t extra_len) {
115 TraceEventETWProvider* provider = TraceEventETWProvider::GetInstance();
116 if (provider && provider->IsTracing()) {
117 // Compute the name & extra lengths if not supplied already.
118 if (name_len == kUseStrlen)
119 name_len = (name == NULL) ? 0 : strlen(name);
120 if (extra_len == kUseStrlen)
121 extra_len = (extra == NULL) ? 0 : strlen(extra);
123 provider->TraceEvent(name, name_len, type, id, extra, extra_len);
127 void TraceEventETWProvider::Resurrect() {
128 StaticMemorySingletonTraits<TraceEventETWProvider>::Resurrect();
131 } // namespace trace_event
132 } // namespace base