Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
danakj | 51d26a40 | 2024-04-25 14:23:56 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 10 | #include "base/logging_win.h" |
Takuto Ikuta | d0bcf212 | 2024-04-17 15:37:04 | [diff] [blame] | 11 | |
| 12 | #include <initguid.h> |
| 13 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/singleton.h" |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 15 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 16 | namespace logging { |
| 17 | |
[email protected] | ba50d192 | 2010-11-06 15:39:40 | [diff] [blame] | 18 | using base::win::EtwEventLevel; |
| 19 | using base::win::EtwMofEvent; |
| 20 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 21 | DEFINE_GUID(kLogEventId, |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 22 | 0x7fe69228, |
| 23 | 0x633e, |
| 24 | 0x4f06, |
| 25 | 0x80, |
| 26 | 0xc1, |
| 27 | 0x52, |
| 28 | 0x7f, |
| 29 | 0xea, |
| 30 | 0x23, |
| 31 | 0xe3, |
| 32 | 0xa7); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 33 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 34 | LogEventProvider::LogEventProvider() : old_log_level_(LOG_NONE) {} |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 35 | |
[email protected] | 864b558 | 2010-12-04 23:00:10 | [diff] [blame] | 36 | LogEventProvider* LogEventProvider::GetInstance() { |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 37 | return base::Singleton<LogEventProvider, base::StaticMemorySingletonTraits< |
| 38 | LogEventProvider>>::get(); |
[email protected] | 864b558 | 2010-12-04 23:00:10 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 41 | bool LogEventProvider::LogMessage(logging::LogSeverity severity, |
Collin Baker | 0162bae | 2025-04-03 18:01:40 | [diff] [blame] | 42 | const char* file, |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 43 | int line, |
| 44 | size_t message_start, |
| 45 | const std::string& message) { |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 46 | EtwEventLevel level = TRACE_LEVEL_NONE; |
| 47 | |
| 48 | // Convert the log severity to the most appropriate ETW trace level. |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 49 | if (severity >= 0) { |
| 50 | switch (severity) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 51 | case LOGGING_INFO: |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 52 | level = TRACE_LEVEL_INFORMATION; |
| 53 | break; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 54 | case LOGGING_WARNING: |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 55 | level = TRACE_LEVEL_WARNING; |
| 56 | break; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 57 | case LOGGING_ERROR: |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 58 | level = TRACE_LEVEL_ERROR; |
| 59 | break; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 60 | case LOGGING_FATAL: |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 61 | level = TRACE_LEVEL_FATAL; |
| 62 | break; |
| 63 | } |
| 64 | } else { // severity < 0 is VLOG verbosity levels. |
pkasting | 9cf9b94a | 2014-10-01 22:18:43 | [diff] [blame] | 65 | level = static_cast<EtwEventLevel>(TRACE_LEVEL_INFORMATION - severity); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 66 | } |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 67 | |
| 68 | // Bail if we're not logging, not at that level, |
| 69 | // or if we're post-atexit handling. |
[email protected] | 864b558 | 2010-12-04 23:00:10 | [diff] [blame] | 70 | LogEventProvider* provider = LogEventProvider::GetInstance(); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 71 | if (provider == NULL || level > provider->enable_level()) { |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 72 | return false; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 73 | } |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 74 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 75 | // And now log the event. |
| 76 | if (provider->enable_flags() & ENABLE_LOG_MESSAGE_ONLY) { |
| 77 | EtwMofEvent<1> event(kLogEventId, LOG_MESSAGE, level); |
| 78 | event.SetField(0, message.length() + 1 - message_start, |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 79 | message.c_str() + message_start); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 80 | |
| 81 | provider->Log(event.get()); |
| 82 | } else { |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 83 | const size_t kMaxBacktraceDepth = 32; |
| 84 | void* backtrace[kMaxBacktraceDepth]; |
| 85 | DWORD depth = 0; |
| 86 | |
| 87 | // Capture a stack trace if one is requested. |
| 88 | // requested per our enable flags. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 89 | if (provider->enable_flags() & ENABLE_STACK_TRACE_CAPTURE) { |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 90 | depth = CaptureStackBackTrace(2, kMaxBacktraceDepth, backtrace, NULL); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 91 | } |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 92 | |
| 93 | EtwMofEvent<5> event(kLogEventId, LOG_MESSAGE_FULL, level); |
Collin Baker | 0162bae | 2025-04-03 18:01:40 | [diff] [blame] | 94 | if (file == NULL) { |
| 95 | file = ""; |
| 96 | } |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 97 | |
| 98 | // Add the stack trace. |
| 99 | event.SetField(0, sizeof(depth), &depth); |
| 100 | event.SetField(1, sizeof(backtrace[0]) * depth, &backtrace); |
| 101 | // The line. |
| 102 | event.SetField(2, sizeof(line), &line); |
| 103 | // The file. |
Collin Baker | 0162bae | 2025-04-03 18:01:40 | [diff] [blame] | 104 | event.SetField(3, strlen(file) + 1, file); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 105 | // And finally the message. |
| 106 | event.SetField(4, message.length() + 1 - message_start, |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 107 | message.c_str() + message_start); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 108 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 109 | provider->Log(event.get()); |
| 110 | } |
| 111 | |
| 112 | // Don't increase verbosity in other log destinations. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 113 | if (severity < provider->old_log_level_) { |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 114 | return true; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 115 | } |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 116 | |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | void LogEventProvider::Initialize(const GUID& provider_name) { |
[email protected] | 864b558 | 2010-12-04 23:00:10 | [diff] [blame] | 121 | LogEventProvider* provider = LogEventProvider::GetInstance(); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 122 | |
| 123 | provider->set_provider_name(provider_name); |
| 124 | provider->Register(); |
| 125 | |
| 126 | // Register our message handler with logging. |
| 127 | SetLogMessageHandler(LogMessage); |
| 128 | } |
| 129 | |
| 130 | void LogEventProvider::Uninitialize() { |
[email protected] | 864b558 | 2010-12-04 23:00:10 | [diff] [blame] | 131 | LogEventProvider::GetInstance()->Unregister(); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void LogEventProvider::OnEventsEnabled() { |
| 135 | // Grab the old log level so we can restore it later. |
| 136 | old_log_level_ = GetMinLogLevel(); |
| 137 | |
| 138 | // Convert the new trace level to a logging severity |
| 139 | // and enable logging at that level. |
| 140 | EtwEventLevel level = enable_level(); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 141 | if (level == TRACE_LEVEL_NONE || level == TRACE_LEVEL_FATAL) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 142 | SetMinLogLevel(LOGGING_FATAL); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 143 | } else if (level == TRACE_LEVEL_ERROR) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 144 | SetMinLogLevel(LOGGING_ERROR); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 145 | } else if (level == TRACE_LEVEL_WARNING) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 146 | SetMinLogLevel(LOGGING_WARNING); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 147 | } else if (level == TRACE_LEVEL_INFORMATION) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 148 | SetMinLogLevel(LOGGING_INFO); |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 149 | } else if (level >= TRACE_LEVEL_VERBOSE) { |
| 150 | // Above INFO, we enable verbose levels with negative severities. |
| 151 | SetMinLogLevel(TRACE_LEVEL_INFORMATION - level); |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | void LogEventProvider::OnEventsDisabled() { |
| 156 | // Restore the old log level. |
| 157 | SetMinLogLevel(old_log_level_); |
| 158 | } |
| 159 | |
| 160 | } // namespace logging |