[Mac] Use the app's version as the app shim bundle version.
[chromium-blink-merge.git] / base / logging_win.cc
blob53cc37c4ea32c6ccf09083dd7210388ce8143688
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 level = TRACE_LEVEL_ERROR;
41 break;
42 case LOG_FATAL:
43 level = TRACE_LEVEL_FATAL;
44 break;
46 } else { // severity < 0 is VLOG verbosity levels.
47 level = static_cast<EtwEventLevel>(TRACE_LEVEL_INFORMATION - severity);
50 // Bail if we're not logging, not at that level,
51 // or if we're post-atexit handling.
52 LogEventProvider* provider = LogEventProvider::GetInstance();
53 if (provider == NULL || level > provider->enable_level())
54 return false;
56 // And now log the event.
57 if (provider->enable_flags() & ENABLE_LOG_MESSAGE_ONLY) {
58 EtwMofEvent<1> event(kLogEventId, LOG_MESSAGE, level);
59 event.SetField(0, message.length() + 1 - message_start,
60 message.c_str() + message_start);
62 provider->Log(event.get());
63 } else {
64 const size_t kMaxBacktraceDepth = 32;
65 void* backtrace[kMaxBacktraceDepth];
66 DWORD depth = 0;
68 // Capture a stack trace if one is requested.
69 // requested per our enable flags.
70 if (provider->enable_flags() & ENABLE_STACK_TRACE_CAPTURE)
71 depth = CaptureStackBackTrace(2, kMaxBacktraceDepth, backtrace, NULL);
73 EtwMofEvent<5> event(kLogEventId, LOG_MESSAGE_FULL, level);
74 if (file == NULL)
75 file = "";
77 // Add the stack trace.
78 event.SetField(0, sizeof(depth), &depth);
79 event.SetField(1, sizeof(backtrace[0]) * depth, &backtrace);
80 // The line.
81 event.SetField(2, sizeof(line), &line);
82 // The file.
83 event.SetField(3, strlen(file) + 1, file);
84 // And finally the message.
85 event.SetField(4, message.length() + 1 - message_start,
86 message.c_str() + message_start);
88 provider->Log(event.get());
91 // Don't increase verbosity in other log destinations.
92 if (severity < provider->old_log_level_)
93 return true;
95 return false;
98 void LogEventProvider::Initialize(const GUID& provider_name) {
99 LogEventProvider* provider = LogEventProvider::GetInstance();
101 provider->set_provider_name(provider_name);
102 provider->Register();
104 // Register our message handler with logging.
105 SetLogMessageHandler(LogMessage);
108 void LogEventProvider::Uninitialize() {
109 LogEventProvider::GetInstance()->Unregister();
112 void LogEventProvider::OnEventsEnabled() {
113 // Grab the old log level so we can restore it later.
114 old_log_level_ = GetMinLogLevel();
116 // Convert the new trace level to a logging severity
117 // and enable logging at that level.
118 EtwEventLevel level = enable_level();
119 if (level == TRACE_LEVEL_NONE || level == TRACE_LEVEL_FATAL) {
120 SetMinLogLevel(LOG_FATAL);
121 } else if (level == TRACE_LEVEL_ERROR) {
122 SetMinLogLevel(LOG_ERROR);
123 } else if (level == TRACE_LEVEL_WARNING) {
124 SetMinLogLevel(LOG_WARNING);
125 } else if (level == TRACE_LEVEL_INFORMATION) {
126 SetMinLogLevel(LOG_INFO);
127 } else if (level >= TRACE_LEVEL_VERBOSE) {
128 // Above INFO, we enable verbose levels with negative severities.
129 SetMinLogLevel(TRACE_LEVEL_INFORMATION - level);
133 void LogEventProvider::OnEventsDisabled() {
134 // Restore the old log level.
135 SetMinLogLevel(old_log_level_);
138 } // namespace logging