Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | // |
michael@0 | 5 | #include "base/win/event_trace_provider.h" |
michael@0 | 6 | #include <windows.h> |
michael@0 | 7 | #include <cguid.h> |
michael@0 | 8 | |
michael@0 | 9 | namespace base { |
michael@0 | 10 | namespace win { |
michael@0 | 11 | |
michael@0 | 12 | TRACE_GUID_REGISTRATION EtwTraceProvider::obligatory_guid_registration_ = { |
michael@0 | 13 | &GUID_NULL, |
michael@0 | 14 | NULL |
michael@0 | 15 | }; |
michael@0 | 16 | |
michael@0 | 17 | EtwTraceProvider::EtwTraceProvider(const GUID& provider_name) |
michael@0 | 18 | : provider_name_(provider_name), registration_handle_(NULL), |
michael@0 | 19 | session_handle_(NULL), enable_flags_(0), enable_level_(0) { |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | EtwTraceProvider::EtwTraceProvider() |
michael@0 | 23 | : provider_name_(GUID_NULL), registration_handle_(NULL), |
michael@0 | 24 | session_handle_(NULL), enable_flags_(0), enable_level_(0) { |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | EtwTraceProvider::~EtwTraceProvider() { |
michael@0 | 28 | Unregister(); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | ULONG EtwTraceProvider::EnableEvents(void* buffer) { |
michael@0 | 32 | session_handle_ = ::GetTraceLoggerHandle(buffer); |
michael@0 | 33 | if (NULL == session_handle_) { |
michael@0 | 34 | return ::GetLastError(); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | enable_flags_ = ::GetTraceEnableFlags(session_handle_); |
michael@0 | 38 | enable_level_ = ::GetTraceEnableLevel(session_handle_); |
michael@0 | 39 | |
michael@0 | 40 | // Give subclasses a chance to digest the state change. |
michael@0 | 41 | OnEventsEnabled(); |
michael@0 | 42 | |
michael@0 | 43 | return ERROR_SUCCESS; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | ULONG EtwTraceProvider::DisableEvents() { |
michael@0 | 47 | // Give subclasses a chance to digest the state change. |
michael@0 | 48 | OnEventsDisabled(); |
michael@0 | 49 | |
michael@0 | 50 | enable_level_ = 0; |
michael@0 | 51 | enable_flags_ = 0; |
michael@0 | 52 | session_handle_ = NULL; |
michael@0 | 53 | |
michael@0 | 54 | PostEventsDisabled(); |
michael@0 | 55 | |
michael@0 | 56 | return ERROR_SUCCESS; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | ULONG EtwTraceProvider::Callback(WMIDPREQUESTCODE request, void* buffer) { |
michael@0 | 60 | switch (request) { |
michael@0 | 61 | case WMI_ENABLE_EVENTS: |
michael@0 | 62 | return EnableEvents(buffer); |
michael@0 | 63 | case WMI_DISABLE_EVENTS: |
michael@0 | 64 | return DisableEvents(); |
michael@0 | 65 | default: |
michael@0 | 66 | return ERROR_INVALID_PARAMETER; |
michael@0 | 67 | } |
michael@0 | 68 | // Not reached. |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | ULONG WINAPI EtwTraceProvider::ControlCallback(WMIDPREQUESTCODE request, |
michael@0 | 72 | void* context, ULONG *reserved, void* buffer) { |
michael@0 | 73 | EtwTraceProvider *provider = reinterpret_cast<EtwTraceProvider*>(context); |
michael@0 | 74 | |
michael@0 | 75 | return provider->Callback(request, buffer); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | ULONG EtwTraceProvider::Register() { |
michael@0 | 79 | if (provider_name_ == GUID_NULL) |
michael@0 | 80 | return ERROR_INVALID_NAME; |
michael@0 | 81 | |
michael@0 | 82 | return ::RegisterTraceGuids(ControlCallback, this, &provider_name_, |
michael@0 | 83 | 1, &obligatory_guid_registration_, NULL, NULL, ®istration_handle_); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | ULONG EtwTraceProvider::Unregister() { |
michael@0 | 87 | // If a session is active, notify subclasses that it's going away. |
michael@0 | 88 | if (session_handle_ != NULL) |
michael@0 | 89 | DisableEvents(); |
michael@0 | 90 | |
michael@0 | 91 | ULONG ret = ::UnregisterTraceGuids(registration_handle_); |
michael@0 | 92 | |
michael@0 | 93 | registration_handle_ = NULL; |
michael@0 | 94 | |
michael@0 | 95 | return ret; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | ULONG EtwTraceProvider::Log(const EtwEventClass& event_class, |
michael@0 | 99 | EtwEventType type, EtwEventLevel level, const char *message) { |
michael@0 | 100 | if (NULL == session_handle_ || enable_level_ < level) |
michael@0 | 101 | return ERROR_SUCCESS; // No one listening. |
michael@0 | 102 | |
michael@0 | 103 | EtwMofEvent<1> event(event_class, type, level); |
michael@0 | 104 | |
michael@0 | 105 | event.fields[0].DataPtr = reinterpret_cast<ULONG64>(message); |
michael@0 | 106 | event.fields[0].Length = message ? |
michael@0 | 107 | static_cast<ULONG>(sizeof(message[0]) * (1 + strlen(message))) : 0; |
michael@0 | 108 | |
michael@0 | 109 | return ::TraceEvent(session_handle_, &event.header); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | ULONG EtwTraceProvider::Log(const EtwEventClass& event_class, |
michael@0 | 113 | EtwEventType type, EtwEventLevel level, const wchar_t *message) { |
michael@0 | 114 | if (NULL == session_handle_ || enable_level_ < level) |
michael@0 | 115 | return ERROR_SUCCESS; // No one listening. |
michael@0 | 116 | |
michael@0 | 117 | EtwMofEvent<1> event(event_class, type, level); |
michael@0 | 118 | |
michael@0 | 119 | event.fields[0].DataPtr = reinterpret_cast<ULONG64>(message); |
michael@0 | 120 | event.fields[0].Length = message ? |
michael@0 | 121 | static_cast<ULONG>(sizeof(message[0]) * (1 + wcslen(message))) : 0; |
michael@0 | 122 | |
michael@0 | 123 | return ::TraceEvent(session_handle_, &event.header); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | ULONG EtwTraceProvider::Log(EVENT_TRACE_HEADER* event) { |
michael@0 | 127 | if (enable_level_ < event->Class.Level) |
michael@0 | 128 | return ERROR_SUCCESS; |
michael@0 | 129 | |
michael@0 | 130 | return ::TraceEvent(session_handle_, event); |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | } // namespace win |
michael@0 | 134 | } // namespace base |