OLD | NEW |
| (Empty) |
1 // Copyright 2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Implementation of a Windows event trace controller class. | |
17 #include "omaha/base/event_trace_controller.h" | |
18 #include "omaha/base/debug.h" | |
19 | |
20 namespace omaha { | |
21 | |
22 EtwTraceController::EtwTraceController() : session_(NULL) { | |
23 } | |
24 | |
25 EtwTraceController::~EtwTraceController() { | |
26 Stop(NULL); | |
27 } | |
28 | |
29 HRESULT EtwTraceController::Start(const wchar_t* session_name, | |
30 EtwTraceProperties* prop) { | |
31 ASSERT1(NULL == session_ && session_name_.empty()); | |
32 EtwTraceProperties ignore; | |
33 if (prop == NULL) | |
34 prop = &ignore; | |
35 | |
36 HRESULT hr = Start(session_name, prop, &session_); | |
37 if (SUCCEEDED(hr)) | |
38 session_name_ = session_name; | |
39 | |
40 return hr; | |
41 } | |
42 | |
43 HRESULT EtwTraceController::StartFileSession(const wchar_t* session_name, | |
44 const wchar_t* logfile_path, bool realtime) { | |
45 ASSERT1(NULL == session_ && session_name_.empty()); | |
46 | |
47 EtwTraceProperties prop; | |
48 prop.SetLoggerFileName(logfile_path); | |
49 EVENT_TRACE_PROPERTIES& p = *prop.get(); | |
50 p.Wnode.ClientContext = 1; // QPC timer accuracy. | |
51 p.LogFileMode = EVENT_TRACE_FILE_MODE_SEQUENTIAL; // Sequential log. | |
52 if (realtime) | |
53 p.LogFileMode |= EVENT_TRACE_REAL_TIME_MODE; | |
54 | |
55 p.MaximumFileSize = 100; // 100M file size. | |
56 p.FlushTimer = 30; // 30 seconds flush lag. | |
57 return Start(session_name, &prop); | |
58 } | |
59 | |
60 HRESULT EtwTraceController::StartRealtimeSession(const wchar_t* session_name, | |
61 size_t buffer_size) { | |
62 ASSERT1(NULL == session_ && session_name_.empty()); | |
63 EtwTraceProperties prop; | |
64 EVENT_TRACE_PROPERTIES& p = *prop.get(); | |
65 p.LogFileMode = EVENT_TRACE_REAL_TIME_MODE | EVENT_TRACE_USE_PAGED_MEMORY; | |
66 p.FlushTimer = 1; // flush every second. | |
67 p.BufferSize = buffer_size; // buffer_size in kilobytes. | |
68 p.LogFileNameOffset = 0; | |
69 return Start(session_name, &prop); | |
70 } | |
71 | |
72 HRESULT EtwTraceController::EnableProvider(REFGUID provider, UCHAR level, | |
73 ULONG flags) { | |
74 ULONG error = ::EnableTrace(TRUE, flags, level, &provider, session_); | |
75 return HRESULT_FROM_WIN32(error); | |
76 } | |
77 | |
78 HRESULT EtwTraceController::DisableProvider(REFGUID provider) { | |
79 ULONG error = ::EnableTrace(FALSE, 0, 0, &provider, session_); | |
80 return HRESULT_FROM_WIN32(error); | |
81 } | |
82 | |
83 HRESULT EtwTraceController::Stop(EtwTraceProperties* properties) { | |
84 EtwTraceProperties ignore; | |
85 if (properties == NULL) | |
86 properties = &ignore; | |
87 | |
88 ULONG error = ::ControlTrace(session_, NULL, properties->get(), | |
89 EVENT_TRACE_CONTROL_STOP); | |
90 if (ERROR_SUCCESS != error) | |
91 return HRESULT_FROM_WIN32(error); | |
92 | |
93 session_ = NULL; | |
94 session_name_.clear(); | |
95 return S_OK; | |
96 } | |
97 | |
98 HRESULT EtwTraceController::Flush(EtwTraceProperties* properties) { | |
99 EtwTraceProperties ignore; | |
100 if (properties == NULL) | |
101 properties = &ignore; | |
102 | |
103 ULONG error = ::ControlTrace(session_, NULL, properties->get(), | |
104 EVENT_TRACE_CONTROL_FLUSH); | |
105 if (ERROR_SUCCESS != error) | |
106 return HRESULT_FROM_WIN32(error); | |
107 | |
108 return S_OK; | |
109 } | |
110 | |
111 HRESULT EtwTraceController::Start(const wchar_t* session_name, | |
112 EtwTraceProperties* properties, TRACEHANDLE* session_handle) { | |
113 ASSERT1(properties != NULL); | |
114 ULONG err = ::StartTrace(session_handle, session_name, properties->get()); | |
115 return HRESULT_FROM_WIN32(err); | |
116 } | |
117 | |
118 HRESULT EtwTraceController::Query(const wchar_t* session_name, | |
119 EtwTraceProperties* properties) { | |
120 ASSERT1(properties != NULL); | |
121 ULONG err = ::ControlTrace(NULL, session_name, properties->get(), | |
122 EVENT_TRACE_CONTROL_QUERY); | |
123 return HRESULT_FROM_WIN32(err); | |
124 }; | |
125 | |
126 HRESULT EtwTraceController::Update(const wchar_t* session_name, | |
127 EtwTraceProperties* properties) { | |
128 ASSERT1(properties != NULL); | |
129 ULONG err = ::ControlTrace(NULL, session_name, properties->get(), | |
130 EVENT_TRACE_CONTROL_UPDATE); | |
131 return HRESULT_FROM_WIN32(err); | |
132 } | |
133 | |
134 HRESULT EtwTraceController::Stop(const wchar_t* session_name, | |
135 EtwTraceProperties* properties) { | |
136 ASSERT1(properties != NULL); | |
137 ULONG err = ::ControlTrace(NULL, session_name, properties->get(), | |
138 EVENT_TRACE_CONTROL_STOP); | |
139 return HRESULT_FROM_WIN32(err); | |
140 } | |
141 | |
142 HRESULT EtwTraceController::Flush(const wchar_t* session_name, | |
143 EtwTraceProperties* properties) { | |
144 ASSERT1(properties != NULL); | |
145 ULONG err = ::ControlTrace(NULL, session_name, properties->get(), | |
146 EVENT_TRACE_CONTROL_FLUSH); | |
147 return HRESULT_FROM_WIN32(err); | |
148 } | |
149 | |
150 } // namespace omaha | |
OLD | NEW |