Let MetricsService know about some Android Activities
[chromium-blink-merge.git] / base / logging_win.cc
bloba71466578847f355bab8ddca045e0dacaef68497
1 // Copyright (c) 2011 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.
5 #include "base/logging_win.h"
6 #include "base/memory/singleton.h"
7 #include <initguid.h> // NOLINT
9 namespace logging {
11 using base::win::EtwEventLevel;
12 using base::win::EtwMofEvent;
14 DEFINE_GUID(kLogEventId,
15 0x7fe69228, 0x633e, 0x4f06, 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7);
17 LogEventProvider::LogEventProvider() : old_log_level_(LOG_NONE) {
20 LogEventProvider* LogEventProvider::GetInstance() {
21 return Singleton<LogEventProvider,
22 StaticMemorySingletonTraits<LogEventProvider> >::get();
25 bool LogEventProvider::LogMessage(logging::LogSeverity severity,
26 const char* file, int line, size_t message_start,
27 const std::string& message) {
28 EtwEventLevel level = TRACE_LEVEL_NONE;
30 // Convert the log severity to the most appropriate ETW trace level.
31 if (severity >= 0) {
32 switch (severity) {
33 case LOG_INFO:
34 level = TRACE_LEVEL_INFORMATION;
35 break;
36 case LOG_WARNING:
37 level = TRACE_LEVEL_WARNING;
38 break;
39 case LOG_ERROR:
40 case LOG_ERROR_REPORT:
41 level = TRACE_LEVEL_ERROR;
42 break;
43 case LOG_FATAL:
44 level = TRACE_LEVEL_FATAL;
45 break;
47 } else { // severity < 0 is VLOG verbosity levels.
48 level = TRACE_LEVEL_INFORMATION - severity;
51 // Bail if we're not logging, not at that level,
52 // or if we're post-atexit handling.
53 LogEventProvider* provider = LogEventProvider::GetInstance();
54 if (provider == NULL || level > provider->enable_level())
55 return false;
57 // And now log the event.
58 if (provider->enable_flags() & ENABLE_LOG_MESSAGE_ONLY) {
59 EtwMofEvent<1> event(kLogEventId, LOG_MESSAGE, level);
60 event.SetField(0, message.length() + 1 - message_start,
61 message.c_str() + message_start);
63 provider->Log(event.get());
64 } else {
65 const size_t kMaxBacktraceDepth = 32;
66 void* backtrace[kMaxBacktraceDepth];
67 DWORD depth = 0;
69 // Capture a stack trace if one is requested.
70 // requested per our enable flags.
71 if (provider->enable_flags() & ENABLE_STACK_TRACE_CAPTURE)
72 depth = CaptureStackBackTrace(2, kMaxBacktraceDepth, backtrace, NULL);
74 EtwMofEvent<5> event(kLogEventId, LOG_MESSAGE_FULL, level);
75 if (file == NULL)
76 file = "";
78 // Add the stack trace.
79 event.SetField(0, sizeof(depth), &depth);
80 event.SetField(1, sizeof(backtrace[0]) * depth, &backtrace);
81 // The line.
82 event.SetField(2, sizeof(line), &line);
83 // The file.
84 event.SetField(3, strlen(file) + 1, file);
85 // And finally the message.
86 event.SetField(4, message.length() + 1 - message_start,
87 message.c_str() + message_start);
89 provider->Log(event.get());
92 // Don't increase verbosity in other log destinations.
93 if (severity < provider->old_log_level_)
94 return true;
96 return false;
99 void LogEventProvider::Initialize(const GUID& provider_name) {
100 LogEventProvider* provider = LogEventProvider::GetInstance();
102 provider->set_provider_name(provider_name);
103 provider->Register();
105 // Register our message handler with logging.
106 SetLogMessageHandler(LogMessage);
109 void LogEventProvider::Uninitialize() {
110 LogEventProvider::GetInstance()->Unregister();
113 void LogEventProvider::OnEventsEnabled() {
114 // Grab the old log level so we can restore it later.
115 old_log_level_ = GetMinLogLevel();
117 // Convert the new trace level to a logging severity
118 // and enable logging at that level.
119 EtwEventLevel level = enable_level();
120 if (level == TRACE_LEVEL_NONE || level == TRACE_LEVEL_FATAL) {
121 SetMinLogLevel(LOG_FATAL);
122 } else if (level == TRACE_LEVEL_ERROR) {
123 SetMinLogLevel(LOG_ERROR);
124 } else if (level == TRACE_LEVEL_WARNING) {
125 SetMinLogLevel(LOG_WARNING);
126 } else if (level == TRACE_LEVEL_INFORMATION) {
127 SetMinLogLevel(LOG_INFO);
128 } else if (level >= TRACE_LEVEL_VERBOSE) {
129 // Above INFO, we enable verbose levels with negative severities.
130 SetMinLogLevel(TRACE_LEVEL_INFORMATION - level);
134 void LogEventProvider::OnEventsDisabled() {
135 // Restore the old log level.
136 SetMinLogLevel(old_log_level_);
139 } // namespace logging