summaryrefslogtreecommitdiffstats
path: root/base/event_trace_provider_win.cc
diff options
context:
space:
mode:
authorsiggi@chromium.org <siggi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-25 15:26:34 +0000
committersiggi@chromium.org <siggi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-25 15:26:34 +0000
commit2b07b84191a266cb8c9dd86b6957df4947b136e0 (patch)
treebbe263aa264dc1cb12ad0d84d265b4580b93cb25 /base/event_trace_provider_win.cc
parent458d689228b12028f02d632c091ad1f637bbc15b (diff)
downloadchromium_src-2b07b84191a266cb8c9dd86b6957df4947b136e0.zip
chromium_src-2b07b84191a266cb8c9dd86b6957df4947b136e0.tar.gz
chromium_src-2b07b84191a266cb8c9dd86b6957df4947b136e0.tar.bz2
Integrate the base logging with Event Tracing for Windows. This allows ETW to control the log level and log feature flags from outside the process, and all log messages can be transported into ETW trace sessions.As is this provides an event trace provider class thatmanages the log level on control callbacks and shunts the formatted log messages to ETW when warranted.The provider observes one feature flag, which when turned on causes it to capture and log a stack trace at the log site, which can be helpful when diagnosing errors from logs.This CL also initializes ETW logging for chrome.dll, but only if the environment variable "CHROME_ETW_LOGGING" is set. The ETW machinery may create a thread in every process registering a trace provider, and the environment variable makes this overhead optional.
TEST=Unittests in this change BUG=none Review URL: http://codereview.chromium.org/413006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33066 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/event_trace_provider_win.cc')
-rw-r--r--base/event_trace_provider_win.cc126
1 files changed, 126 insertions, 0 deletions
diff --git a/base/event_trace_provider_win.cc b/base/event_trace_provider_win.cc
new file mode 100644
index 0000000..69fcd57
--- /dev/null
+++ b/base/event_trace_provider_win.cc
@@ -0,0 +1,126 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+//
+#include "base/event_trace_provider_win.h"
+#include <windows.h>
+#include <cguid.h>
+
+TRACE_GUID_REGISTRATION EtwTraceProvider::obligatory_guid_registration_ = {
+ &GUID_NULL,
+ NULL
+};
+
+EtwTraceProvider::EtwTraceProvider(const GUID& provider_name)
+ : provider_name_(provider_name), registration_handle_(NULL),
+ session_handle_(NULL), enable_flags_(0), enable_level_(0) {
+}
+
+EtwTraceProvider::EtwTraceProvider()
+ : provider_name_(GUID_NULL), registration_handle_(NULL),
+ session_handle_(NULL), enable_flags_(0), enable_level_(0) {
+}
+
+EtwTraceProvider::~EtwTraceProvider() {
+ Unregister();
+}
+
+ULONG EtwTraceProvider::EnableEvents(void* buffer) {
+ session_handle_ = ::GetTraceLoggerHandle(buffer);
+ if (NULL == session_handle_) {
+ return ::GetLastError();
+ }
+
+ enable_flags_ = ::GetTraceEnableFlags(session_handle_);
+ enable_level_ = ::GetTraceEnableLevel(session_handle_);
+
+ // Give subclasses a chance to digest the state change.
+ OnEventsEnabled();
+
+ return ERROR_SUCCESS;
+}
+
+ULONG EtwTraceProvider::DisableEvents() {
+ enable_level_ = 0;
+ enable_flags_ = 0;
+ session_handle_ = NULL;
+
+ // Give subclasses a chance to digest the state change.
+ OnEventsDisabled();
+
+ return ERROR_SUCCESS;
+}
+
+ULONG EtwTraceProvider::Callback(WMIDPREQUESTCODE request, void* buffer) {
+ switch (request) {
+ case WMI_ENABLE_EVENTS:
+ return EnableEvents(buffer);
+ case WMI_DISABLE_EVENTS:
+ return DisableEvents();
+ default:
+ return ERROR_INVALID_PARAMETER;
+ }
+ // Not reached.
+}
+
+ULONG WINAPI EtwTraceProvider::ControlCallback(WMIDPREQUESTCODE request,
+ void* context, ULONG *reserved, void* buffer) {
+ EtwTraceProvider *provider = reinterpret_cast<EtwTraceProvider*>(context);
+
+ return provider->Callback(request, buffer);
+}
+
+ULONG EtwTraceProvider::Register() {
+ if (provider_name_ == GUID_NULL)
+ return ERROR_INVALID_NAME;
+
+ return ::RegisterTraceGuids(ControlCallback, this, &provider_name_,
+ 1, &obligatory_guid_registration_, NULL, NULL, &registration_handle_);
+}
+
+ULONG EtwTraceProvider::Unregister() {
+ ULONG ret = ::UnregisterTraceGuids(registration_handle_);
+
+ // Make sure we don't log anything from here on.
+ enable_level_ = 0;
+ enable_flags_ = 0;
+ session_handle_ = NULL;
+ registration_handle_ = NULL;
+
+ return ret;
+}
+
+ULONG EtwTraceProvider::Log(const EtwEventClass& event_class,
+ EtwEventType type, EtwEventLevel level, const char *message) {
+ if (NULL == session_handle_ || enable_level_ < level)
+ return ERROR_SUCCESS; // No one listening.
+
+ EtwMofEvent<1> event(event_class, type, level);
+
+ event.fields[0].DataPtr = reinterpret_cast<ULONG64>(message);
+ event.fields[0].Length = message ?
+ sizeof(message[0]) * (1 + strlen(message)) : 0;
+
+ return ::TraceEvent(session_handle_, &event.header);
+}
+
+ULONG EtwTraceProvider::Log(const EtwEventClass& event_class,
+ EtwEventType type, EtwEventLevel level, const wchar_t *message) {
+ if (NULL == session_handle_ || enable_level_ < level)
+ return ERROR_SUCCESS; // No one listening.
+
+ EtwMofEvent<1> event(event_class, type, level);
+
+ event.fields[0].DataPtr = reinterpret_cast<ULONG64>(message);
+ event.fields[0].Length = message ?
+ sizeof(message[0]) * (1 + wcslen(message)) : 0;
+
+ return ::TraceEvent(session_handle_, &event.header);
+}
+
+ULONG EtwTraceProvider::Log(EVENT_TRACE_HEADER* event) {
+ if (enable_level_ < event->Class.Level)
+ return ERROR_SUCCESS;
+
+ return ::TraceEvent(session_handle_, event);
+}