OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 // Declaration of a Windows event trace consumer base class. | |
6 #ifndef BASE_EVENT_TRACE_CONSUMER_WIN_H_ | |
7 #define BASE_EVENT_TRACE_CONSUMER_WIN_H_ | |
8 #pragma once | |
9 | |
10 #include <windows.h> | |
11 #include <wmistr.h> | |
12 #include <evntrace.h> | |
13 #include <vector> | |
14 #include "base/basictypes.h" | |
15 | |
16 // This class is a base class that makes it easier to consume events | |
17 // from realtime or file sessions. Concrete consumers need to sublass | |
18 // a specialization of this class and override the ProcessEvent and/or | |
19 // the ProcessBuffer methods to implement the event consumption logic. | |
20 // Usage might look like: | |
21 // class MyConsumer: public EtwTraceConsumerBase<MyConsumer, 1> { | |
22 // protected: | |
23 // static VOID WINAPI ProcessEvent(PEVENT_TRACE event); | |
24 // }; | |
25 // | |
26 // MyConsumer consumer; | |
27 // consumer.OpenFileSession(file_path); | |
28 // consumer.Consume(); | |
29 template <class ImplClass> | |
30 class EtwTraceConsumerBase { | |
31 public: | |
32 // Constructs a closed consumer. | |
33 EtwTraceConsumerBase() { | |
34 } | |
35 | |
36 ~EtwTraceConsumerBase() { | |
37 Close(); | |
38 } | |
39 | |
40 // Opens the named realtime session, which must be existent. | |
41 // Note: You can use OpenRealtimeSession or OpenFileSession | |
42 // to open as many as MAXIMUM_WAIT_OBJECTS (63) sessions at | |
43 // any one time, though only one of them may be a realtime | |
44 // session. | |
45 HRESULT OpenRealtimeSession(const wchar_t* session_name); | |
46 | |
47 // Opens the event trace log in "file_name", which must be a full or | |
48 // relative path to an existing event trace log file. | |
49 // Note: You can use OpenRealtimeSession or OpenFileSession | |
50 // to open as many as kNumSessions at any one time. | |
51 HRESULT OpenFileSession(const wchar_t* file_name); | |
52 | |
53 // Consume all open sessions from beginning to end. | |
54 HRESULT Consume(); | |
55 | |
56 // Close all open sessions. | |
57 HRESULT Close(); | |
58 | |
59 protected: | |
60 // Override in subclasses to handle events. | |
61 static void ProcessEvent(EVENT_TRACE* event) { | |
62 } | |
63 // Override in subclasses to handle buffers. | |
64 static bool ProcessBuffer(EVENT_TRACE_LOGFILE* buffer) { | |
65 return true; // keep going | |
66 } | |
67 | |
68 protected: | |
69 // Currently open sessions. | |
70 std::vector<TRACEHANDLE> trace_handles_; | |
71 | |
72 private: | |
73 // These delegate to ImplClass callbacks with saner signatures. | |
74 static void WINAPI ProcessEventCallback(EVENT_TRACE* event) { | |
75 ImplClass::ProcessEvent(event); | |
76 } | |
77 static ULONG WINAPI ProcessBufferCallback(PEVENT_TRACE_LOGFILE buffer) { | |
78 return ImplClass::ProcessBuffer(buffer); | |
79 } | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(EtwTraceConsumerBase); | |
82 }; | |
83 | |
84 template <class ImplClass> inline | |
85 HRESULT EtwTraceConsumerBase<ImplClass>::OpenRealtimeSession( | |
86 const wchar_t* session_name) { | |
87 EVENT_TRACE_LOGFILE logfile = {}; | |
88 logfile.LoggerName = const_cast<wchar_t*>(session_name); | |
89 logfile.LogFileMode = EVENT_TRACE_REAL_TIME_MODE; | |
90 logfile.BufferCallback = &ProcessBufferCallback; | |
91 logfile.EventCallback = &ProcessEventCallback; | |
92 logfile.Context = this; | |
93 TRACEHANDLE trace_handle = ::OpenTrace(&logfile); | |
94 if (reinterpret_cast<TRACEHANDLE>(INVALID_HANDLE_VALUE) == trace_handle) | |
95 return HRESULT_FROM_WIN32(::GetLastError()); | |
96 | |
97 trace_handles_.push_back(trace_handle); | |
98 return S_OK; | |
99 } | |
100 | |
101 template <class ImplClass> inline | |
102 HRESULT EtwTraceConsumerBase<ImplClass>::OpenFileSession( | |
103 const wchar_t* file_name) { | |
104 EVENT_TRACE_LOGFILE logfile = {}; | |
105 logfile.LogFileName = const_cast<wchar_t*>(file_name); | |
106 logfile.BufferCallback = &ProcessBufferCallback; | |
107 logfile.EventCallback = &ProcessEventCallback; | |
108 logfile.Context = this; | |
109 TRACEHANDLE trace_handle = ::OpenTrace(&logfile); | |
110 if (reinterpret_cast<TRACEHANDLE>(INVALID_HANDLE_VALUE) == trace_handle) | |
111 return HRESULT_FROM_WIN32(::GetLastError()); | |
112 | |
113 trace_handles_.push_back(trace_handle); | |
114 return S_OK; | |
115 } | |
116 | |
117 template <class ImplClass> inline | |
118 HRESULT EtwTraceConsumerBase<ImplClass>::Consume() { | |
119 ULONG err = ::ProcessTrace(&trace_handles_[0], | |
120 trace_handles_.size(), | |
121 NULL, | |
122 NULL); | |
123 return HRESULT_FROM_WIN32(err); | |
124 } | |
125 | |
126 template <class ImplClass> inline | |
127 HRESULT EtwTraceConsumerBase<ImplClass>::Close() { | |
128 HRESULT hr = S_OK; | |
129 for (size_t i = 0; i < trace_handles_.size(); ++i) { | |
130 if (NULL != trace_handles_[i]) { | |
131 ULONG ret = ::CloseTrace(trace_handles_[i]); | |
132 trace_handles_[i] = NULL; | |
133 | |
134 if (FAILED(HRESULT_FROM_WIN32(ret))) | |
135 hr = HRESULT_FROM_WIN32(ret); | |
136 } | |
137 | |
138 trace_handles_.clear(); | |
139 } | |
140 | |
141 return hr; | |
142 } | |
143 | |
144 #endif // BASE_EVENT_TRACE_CONSUMER_WIN_H_ | |
OLD | NEW |