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 #include "omaha/base/event_trace_provider.h" | |
16 #include <windows.h> | |
17 #include <cguid.h> | |
18 | |
19 namespace omaha { | |
20 | |
21 TRACE_GUID_REGISTRATION EtwTraceProvider::obligatory_guid_registration_ = { | |
22 &GUID_NULL, | |
23 NULL | |
24 }; | |
25 | |
26 EtwTraceProvider::EtwTraceProvider(const GUID& provider_name) | |
27 : provider_name_(provider_name), registration_handle_(NULL), | |
28 session_handle_(NULL), enable_flags_(0), enable_level_(0) { | |
29 } | |
30 | |
31 EtwTraceProvider::EtwTraceProvider() | |
32 : provider_name_(GUID_NULL), registration_handle_(NULL), | |
33 session_handle_(NULL), enable_flags_(0), enable_level_(0) { | |
34 } | |
35 | |
36 EtwTraceProvider::~EtwTraceProvider() { | |
37 Unregister(); | |
38 } | |
39 | |
40 ULONG EtwTraceProvider::EnableEvents(void* buffer) { | |
41 session_handle_ = ::GetTraceLoggerHandle(buffer); | |
42 if (NULL == session_handle_) { | |
43 return ::GetLastError(); | |
44 } | |
45 | |
46 enable_flags_ = ::GetTraceEnableFlags(session_handle_); | |
47 enable_level_ = ::GetTraceEnableLevel(session_handle_); | |
48 | |
49 // Give subclasses a chance to digest the state change. | |
50 OnEventsEnabled(); | |
51 | |
52 return ERROR_SUCCESS; | |
53 } | |
54 | |
55 ULONG EtwTraceProvider::DisableEvents() { | |
56 enable_level_ = 0; | |
57 enable_flags_ = 0; | |
58 session_handle_ = NULL; | |
59 | |
60 // Give subclasses a chance to digest the state change. | |
61 OnEventsDisabled(); | |
62 | |
63 return ERROR_SUCCESS; | |
64 } | |
65 | |
66 ULONG EtwTraceProvider::Callback(WMIDPREQUESTCODE request, void* buffer) { | |
67 switch (request) { | |
68 case WMI_ENABLE_EVENTS: | |
69 return EnableEvents(buffer); | |
70 case WMI_DISABLE_EVENTS: | |
71 return DisableEvents(); | |
72 | |
73 case WMI_GET_ALL_DATA: | |
74 case WMI_GET_SINGLE_INSTANCE: | |
75 case WMI_SET_SINGLE_INSTANCE: | |
76 case WMI_SET_SINGLE_ITEM: | |
77 case WMI_ENABLE_COLLECTION: | |
78 case WMI_DISABLE_COLLECTION: | |
79 case WMI_REGINFO: | |
80 case WMI_EXECUTE_METHOD: | |
81 default: | |
82 return ERROR_INVALID_PARAMETER; | |
83 } | |
84 // Not reached. | |
85 } | |
86 | |
87 ULONG WINAPI EtwTraceProvider::ControlCallback(WMIDPREQUESTCODE request, | |
88 void* context, ULONG *reserved, void* buffer) { | |
89 reserved; // Unused. | |
90 EtwTraceProvider *provider = reinterpret_cast<EtwTraceProvider*>(context); | |
91 | |
92 return provider->Callback(request, buffer); | |
93 } | |
94 | |
95 ULONG EtwTraceProvider::Register() { | |
96 if (provider_name_ == GUID_NULL) | |
97 return ERROR_INVALID_NAME; | |
98 | |
99 return ::RegisterTraceGuids(ControlCallback, this, &provider_name_, | |
100 1, &obligatory_guid_registration_, NULL, NULL, ®istration_handle_); | |
101 } | |
102 | |
103 ULONG EtwTraceProvider::Unregister() { | |
104 ULONG ret = ::UnregisterTraceGuids(registration_handle_); | |
105 | |
106 // Make sure we don't log anything from here on. | |
107 enable_level_ = 0; | |
108 enable_flags_ = 0; | |
109 session_handle_ = NULL; | |
110 registration_handle_ = NULL; | |
111 | |
112 return ret; | |
113 } | |
114 | |
115 ULONG EtwTraceProvider::Log(const EtwEventClass& event_class, | |
116 EtwEventType type, EtwEventLevel level, const char *message) { | |
117 if (NULL == session_handle_ || enable_level_ < level) | |
118 return ERROR_SUCCESS; // No one listening. | |
119 | |
120 EtwMofEvent<1> event(event_class, type, level); | |
121 | |
122 event.fields[0].DataPtr = reinterpret_cast<ULONG_PTR>(message); | |
123 event.fields[0].Length = message ? | |
124 static_cast<ULONG>(sizeof(message[0]) * (1 + strlen(message))) : 0; | |
125 | |
126 return ::TraceEvent(session_handle_, &event.header); | |
127 } | |
128 | |
129 ULONG EtwTraceProvider::Log(const EtwEventClass& event_class, | |
130 EtwEventType type, EtwEventLevel level, const wchar_t *message) { | |
131 if (NULL == session_handle_ || enable_level_ < level) | |
132 return ERROR_SUCCESS; // No one listening. | |
133 | |
134 EtwMofEvent<1> event(event_class, type, level); | |
135 | |
136 event.fields[0].DataPtr = reinterpret_cast<ULONG_PTR>(message); | |
137 event.fields[0].Length = message ? | |
138 static_cast<ULONG>(sizeof(message[0]) * (1 + wcslen(message))) : 0; | |
139 | |
140 return ::TraceEvent(session_handle_, &event.header); | |
141 } | |
142 | |
143 ULONG EtwTraceProvider::Log(EVENT_TRACE_HEADER* event) { | |
144 if (enable_level_ < event->Class.Level) | |
145 return ERROR_SUCCESS; | |
146 | |
147 return ::TraceEvent(session_handle_, event); | |
148 } | |
149 | |
150 } // namespace omaha | |
OLD | NEW |