[email protected] | 63e6680 | 2012-01-18 21:21:09 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 5 | #include "base/logging.h" |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 6 | |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 7 | // logging.h is a widely included header and its size has significant impact on |
| 8 | // build time. Try not to raise this limit unless absolutely necessary. See |
| 9 | // https://chromium.googlesource.com/chromium/src/+/HEAD/docs/wmax_tokens.md |
| 10 | #ifndef NACL_TC_REV |
Hans Wennborg | 944479f | 2020-06-25 21:39:25 | [diff] [blame] | 11 | #pragma clang max_tokens_here 350000 |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 12 | #endif // NACL_TC_REV |
| 13 | |
Hans Wennborg | 944479f | 2020-06-25 21:39:25 | [diff] [blame] | 14 | #ifdef BASE_CHECK_H_ |
| 15 | #error "logging.h should not include check.h" |
| 16 | #endif |
| 17 | |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 18 | #include <limits.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | |
Avi Drissman | 933398e | 2022-01-22 00:55:42 | [diff] [blame] | 21 | #include <tuple> |
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
David Sanders | 6e70994 | 2022-04-05 06:49:26 | [diff] [blame] | 24 | #include "base/base_export.h" |
Lukasz Anforowicz | 872567d | 2021-09-08 19:49:22 | [diff] [blame] | 25 | #include "base/debug/crash_logging.h" |
Kalvin Lee | 70699ccd | 2022-04-28 17:27:36 | [diff] [blame] | 26 | #include "base/immediate_crash.h" |
Chris Hamilton | 306740d | 2019-04-25 18:48:36 | [diff] [blame] | 27 | #include "base/pending_task.h" |
Jan Wilken Dörrie | 4a498d8c | 2021-01-20 10:19:39 | [diff] [blame] | 28 | #include "base/strings/string_piece.h" |
Chris Hamilton | 306740d | 2019-04-25 18:48:36 | [diff] [blame] | 29 | #include "base/task/common/task_annotator.h" |
Eric Seckler | f6c544f | 2020-06-02 10:49:21 | [diff] [blame] | 30 | #include "base/trace_event/base_tracing.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 31 | #include "build/build_config.h" |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 32 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 33 | #if BUILDFLAG(IS_WIN) |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 34 | #include <io.h> |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 35 | #include <windows.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 36 | typedef HANDLE FileHandle; |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 37 | // Windows warns on using write(). It prefers _write(). |
| 38 | #define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) |
| 39 | // Windows doesn't define STDERR_FILENO. Define it here. |
| 40 | #define STDERR_FILENO 2 |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 41 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 42 | #elif BUILDFLAG(IS_APPLE) |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 43 | // In MacOS 10.12 and iOS 10.0 and later ASL (Apple System Log) was deprecated |
| 44 | // in favor of OS_LOG (Unified Logging). |
| 45 | #include <AvailabilityMacros.h> |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 46 | #if BUILDFLAG(IS_IOS) |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 47 | #if !defined(__IPHONE_10_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0 |
| 48 | #define USE_ASL |
| 49 | #endif |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 50 | #else // BUILDFLAG(IS_IOS) |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 51 | #if !defined(MAC_OS_X_VERSION_10_12) || \ |
| 52 | MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_12 |
| 53 | #define USE_ASL |
| 54 | #endif |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 55 | #endif // BUILDFLAG(IS_IOS) |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 56 | |
| 57 | #if defined(USE_ASL) |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 58 | #include <asl.h> |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 59 | #else |
| 60 | #include <os/log.h> |
| 61 | #endif |
| 62 | |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 63 | #include <CoreFoundation/CoreFoundation.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 64 | #include <mach/mach.h> |
| 65 | #include <mach/mach_time.h> |
| 66 | #include <mach-o/dyld.h> |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 67 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 68 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
| 69 | #if BUILDFLAG(IS_NACL) |
thestig | 75f8735 | 2014-12-03 21:42:27 | [diff] [blame] | 70 | #include <sys/time.h> // timespec doesn't seem to be in <time.h> |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 71 | #endif |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 72 | #include <time.h> |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 73 | #endif |
| 74 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 75 | #if BUILDFLAG(IS_FUCHSIA) |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 76 | #include "base/fuchsia/scoped_fx_logger.h" |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 77 | #endif |
| 78 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 79 | #if BUILDFLAG(IS_ANDROID) |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 80 | #include <android/log.h> |
| 81 | #endif |
| 82 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 83 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 84 | #include <errno.h> |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 85 | #include <paths.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 86 | #include <stdio.h> |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 87 | #include <stdlib.h> |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 88 | #include <string.h> |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 89 | #include <sys/stat.h> |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 90 | #include "base/process/process_handle.h" |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 91 | #define MAX_PATH PATH_MAX |
| 92 | typedef FILE* FileHandle; |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 93 | #endif |
| 94 | |
[email protected] | 1f88b516 | 2011-04-01 00:02:29 | [diff] [blame] | 95 | #include <algorithm> |
| 96 | #include <cstring> |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 97 | #include <ctime> |
| 98 | #include <iomanip> |
[email protected] | 1f88b516 | 2011-04-01 00:02:29 | [diff] [blame] | 99 | #include <ostream> |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 100 | #include <string> |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 101 | #include <utility> |
[email protected] | b16ef31 | 2008-08-19 18:36:23 | [diff] [blame] | 102 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 103 | #include "base/base_switches.h" |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 104 | #include "base/callback.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 105 | #include "base/command_line.h" |
Brett Wilson | 1f07f20e | 2017-10-02 18:55:28 | [diff] [blame] | 106 | #include "base/containers/stack.h" |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 107 | #include "base/debug/activity_tracker.h" |
[email protected] | eb4c4d03 | 2012-04-03 18:45:05 | [diff] [blame] | 108 | #include "base/debug/alias.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 109 | #include "base/debug/debugger.h" |
| 110 | #include "base/debug/stack_trace.h" |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 111 | #include "base/debug/task_trace.h" |
Yannic Bonenberger | 3dcd7fe | 2019-06-08 11:01:45 | [diff] [blame] | 112 | #include "base/no_destructor.h" |
Sharon Yang | a4b908de | 2019-05-07 22:19:03 | [diff] [blame] | 113 | #include "base/path_service.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 114 | #include "base/posix/eintr_wrapper.h" |
[email protected] | eb62f726 | 2013-03-30 14:29:00 | [diff] [blame] | 115 | #include "base/strings/string_piece.h" |
Xianzhu Wang | ae8d96a3 | 2018-10-16 20:41:13 | [diff] [blame] | 116 | #include "base/strings/string_split.h" |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 117 | #include "base/strings/string_util.h" |
| 118 | #include "base/strings/stringprintf.h" |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 119 | #include "base/strings/sys_string_conversions.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 120 | #include "base/strings/utf_string_conversions.h" |
Benoit Lize | 5c98a83 | 2020-07-07 16:32:01 | [diff] [blame] | 121 | #include "base/synchronization/lock.h" |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 122 | #include "base/test/scoped_logging_settings.h" |
[email protected] | 63e6680 | 2012-01-18 21:21:09 | [diff] [blame] | 123 | #include "base/threading/platform_thread.h" |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 124 | #include "base/vlog.h" |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 125 | #include "build/chromeos_buildflags.h" |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 126 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 127 | #if BUILDFLAG(IS_WIN) |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 128 | #include "base/win/win_util.h" |
| 129 | #endif |
| 130 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 131 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
brettw | 6ee6fd6 | 2015-06-09 18:05:24 | [diff] [blame] | 132 | #include "base/posix/safe_strerror.h" |
[email protected] | 53c7ce4 | 2010-12-14 16:20:04 | [diff] [blame] | 133 | #endif |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 134 | |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 135 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 136 | #include "base/files/scoped_file.h" |
| 137 | #endif |
| 138 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 139 | namespace logging { |
| 140 | |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 141 | namespace { |
| 142 | |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame^] | 143 | #if BUILDFLAG(USE_RUNTIME_VLOG) |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 144 | VlogInfo* g_vlog_info = nullptr; |
| 145 | VlogInfo* g_vlog_info_prev = nullptr; |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame^] | 146 | #endif // BUILDFLAG(USE_RUNTIME_VLOG) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 147 | |
wez | a245bd07 | 2017-06-18 23:26:34 | [diff] [blame] | 148 | const char* const log_severity_names[] = {"INFO", "WARNING", "ERROR", "FATAL"}; |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 149 | static_assert(LOGGING_NUM_SEVERITIES == std::size(log_severity_names), |
wez | a245bd07 | 2017-06-18 23:26:34 | [diff] [blame] | 150 | "Incorrect number of log_severity_names"); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 151 | |
thestig | 75f8735 | 2014-12-03 21:42:27 | [diff] [blame] | 152 | const char* log_severity_name(int severity) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 153 | if (severity >= 0 && severity < LOGGING_NUM_SEVERITIES) |
[email protected] | 80f360a | 2014-01-23 01:36:19 | [diff] [blame] | 154 | return log_severity_names[severity]; |
| 155 | return "UNKNOWN"; |
| 156 | } |
| 157 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 158 | int g_min_log_level = 0; |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 159 | |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 160 | // Specifies the process' logging sink(s), represented as a combination of |
| 161 | // LoggingDestination values joined by bitwise OR. |
Peter Kasting | 40cfeae | 2021-06-14 12:21:48 | [diff] [blame] | 162 | uint32_t g_logging_destination = LOG_DEFAULT; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 163 | |
Georg Neis | ffe34f65 | 2021-12-27 21:42:36 | [diff] [blame] | 164 | #if BUILDFLAG(IS_CHROMEOS) |
Yuta Hijikata | 1fc8f634 | 2020-09-01 03:25:56 | [diff] [blame] | 165 | // Specifies the format of log header for chrome os. |
| 166 | LogFormat g_log_format = LogFormat::LOG_FORMAT_SYSLOG; |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 167 | #endif |
| 168 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 169 | #if BUILDFLAG(IS_FUCHSIA) |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 170 | // Retains system logging structures. |
| 171 | base::ScopedFxLogger& GetScopedFxLogger() { |
| 172 | static base::NoDestructor<base::ScopedFxLogger> logger; |
| 173 | return *logger; |
| 174 | } |
| 175 | #endif |
| 176 | |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 177 | // For LOGGING_ERROR and above, always print to stderr. |
| 178 | const int kAlwaysPrintErrorLevel = LOGGING_ERROR; |
[email protected] | a33c989 | 2008-08-25 20:10:31 | [diff] [blame] | 179 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 180 | // Which log file to use? This is initialized by InitLogging or |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 181 | // will be lazily initialized to the default value when it is |
| 182 | // first needed. |
jdoerrie | 5c4dc4e | 2019-02-01 18:02:33 | [diff] [blame] | 183 | using PathString = base::FilePath::StringType; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 184 | PathString* g_log_file_name = nullptr; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 185 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 186 | // This file is lazily opened and the handle may be nullptr |
| 187 | FileHandle g_log_file = nullptr; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 188 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 189 | // What should be prepended to each message? |
| 190 | bool g_log_process_id = false; |
| 191 | bool g_log_thread_id = false; |
| 192 | bool g_log_timestamp = true; |
| 193 | bool g_log_tickcount = false; |
James Cook | a0536c3 | 2018-08-01 20:13:31 | [diff] [blame] | 194 | const char* g_log_prefix = nullptr; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 195 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 196 | // Should we pop up fatal debug messages in a dialog? |
| 197 | bool show_error_dialogs = false; |
| 198 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 199 | // An assert handler override specified by the client to be called instead of |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 200 | // the debug message dialog and process termination. Assert handlers are stored |
| 201 | // in stack to allow overriding and restoring. |
Yannic Bonenberger | 3dcd7fe | 2019-06-08 11:01:45 | [diff] [blame] | 202 | base::stack<LogAssertHandlerFunction>& GetLogAssertHandlerStack() { |
| 203 | static base::NoDestructor<base::stack<LogAssertHandlerFunction>> instance; |
| 204 | return *instance; |
| 205 | } |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 206 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 207 | // A log message handler that gets notified of every log message we process. |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 208 | LogMessageHandlerFunction g_log_message_handler = nullptr; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 209 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 210 | uint64_t TickCount() { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 211 | #if BUILDFLAG(IS_WIN) |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 212 | return GetTickCount(); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 213 | #elif BUILDFLAG(IS_FUCHSIA) |
Sharon Yang | 52c6099 | 2019-05-16 22:41:35 | [diff] [blame] | 214 | return zx_clock_get_monotonic() / |
Wez | b050130 | 2018-03-09 05:18:45 | [diff] [blame] | 215 | static_cast<zx_time_t>(base::Time::kNanosecondsPerMicrosecond); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 216 | #elif BUILDFLAG(IS_APPLE) |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 217 | return mach_absolute_time(); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 218 | #elif BUILDFLAG(IS_NACL) |
[email protected] | 19ea84ca | 2010-11-12 08:37:08 | [diff] [blame] | 219 | // NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h |
| 220 | // So we have to use clock() for now. |
| 221 | return clock(); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 222 | #elif BUILDFLAG(IS_POSIX) |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 223 | struct timespec ts; |
| 224 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 225 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 226 | uint64_t absolute_micro = static_cast<int64_t>(ts.tv_sec) * 1000000 + |
| 227 | static_cast<int64_t>(ts.tv_nsec) / 1000; |
[email protected] | 052f1b5 | 2008-11-06 21:43:07 | [diff] [blame] | 228 | |
| 229 | return absolute_micro; |
[email protected] | f858847 | 2008-11-05 23:17:24 | [diff] [blame] | 230 | #endif |
| 231 | } |
| 232 | |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 233 | void DeleteFilePath(const PathString& log_name) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 234 | #if BUILDFLAG(IS_WIN) |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 235 | DeleteFile(log_name.c_str()); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 236 | #elif BUILDFLAG(IS_NACL) |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 237 | // Do nothing; unlink() isn't supported on NaCl. |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 238 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 239 | unlink(log_name.c_str()); |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 240 | #else |
| 241 | #error Unsupported platform |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 242 | #endif |
| 243 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 244 | |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 245 | PathString GetDefaultLogFile() { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 246 | #if BUILDFLAG(IS_WIN) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 247 | // On Windows we use the same path as the exe. |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 248 | wchar_t module_name[MAX_PATH]; |
| 249 | GetModuleFileName(nullptr, module_name, MAX_PATH); |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 250 | |
scottmg | fc5b707 | 2015-01-27 21:46:28 | [diff] [blame] | 251 | PathString log_name = module_name; |
| 252 | PathString::size_type last_backslash = log_name.rfind('\\', log_name.size()); |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 253 | if (last_backslash != PathString::npos) |
scottmg | fc5b707 | 2015-01-27 21:46:28 | [diff] [blame] | 254 | log_name.erase(last_backslash + 1); |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 255 | log_name += FILE_PATH_LITERAL("debug.log"); |
scottmg | fc5b707 | 2015-01-27 21:46:28 | [diff] [blame] | 256 | return log_name; |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 257 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 258 | // On other platforms we just use the current directory. |
[email protected] | 5f95d53 | 2010-10-01 17:16:58 | [diff] [blame] | 259 | return PathString("debug.log"); |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 260 | #endif |
| 261 | } |
| 262 | |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 263 | // We don't need locks on Windows for atomically appending to files. The OS |
| 264 | // provides this functionality. |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 265 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 266 | |
Benoit Lize | 5c98a83 | 2020-07-07 16:32:01 | [diff] [blame] | 267 | base::Lock& GetLoggingLock() { |
| 268 | static base::NoDestructor<base::Lock> lock; |
| 269 | return *lock; |
| 270 | } |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 271 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 272 | #endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 273 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 274 | // Called by logging functions to ensure that |g_log_file| is initialized |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 275 | // and can be used for writing. Returns false if the file could not be |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 276 | // initialized. |g_log_file| will be nullptr in this case. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 277 | bool InitializeLogFileHandle() { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 278 | if (g_log_file) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 279 | return true; |
| 280 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 281 | if (!g_log_file_name) { |
[email protected] | 614e9fa | 2008-08-11 22:52:59 | [diff] [blame] | 282 | // Nobody has called InitLogging to specify a debug log file, so here we |
| 283 | // initialize the log file name to a default. |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 284 | g_log_file_name = new PathString(GetDefaultLogFile()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 285 | } |
| 286 | |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 287 | if ((g_logging_destination & LOG_TO_FILE) == 0) |
| 288 | return true; |
| 289 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 290 | #if BUILDFLAG(IS_WIN) |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 291 | // The FILE_APPEND_DATA access mask ensures that the file is atomically |
| 292 | // appended to across accesses from multiple threads. |
| 293 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364399(v=vs.85).aspx |
| 294 | // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 295 | g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA, |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 296 | FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, |
| 297 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 298 | if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) { |
| 299 | // We are intentionally not using FilePath or FileUtil here to reduce the |
| 300 | // dependencies of the logging implementation. For e.g. FilePath and |
| 301 | // FileUtil depend on shell32 and user32.dll. This is not acceptable for |
| 302 | // some consumers of base logging like chrome_elf, etc. |
| 303 | // Please don't change the code below to use FilePath. |
| 304 | // try the current directory |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 305 | wchar_t system_buffer[MAX_PATH]; |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 306 | system_buffer[0] = 0; |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 307 | DWORD len = ::GetCurrentDirectory(std::size(system_buffer), system_buffer); |
| 308 | if (len == 0 || len > std::size(system_buffer)) |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 309 | return false; |
| 310 | |
| 311 | *g_log_file_name = system_buffer; |
| 312 | // Append a trailing backslash if needed. |
| 313 | if (g_log_file_name->back() != L'\\') |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 314 | *g_log_file_name += FILE_PATH_LITERAL("\\"); |
| 315 | *g_log_file_name += FILE_PATH_LITERAL("debug.log"); |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 316 | |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 317 | g_log_file = CreateFile(g_log_file_name->c_str(), FILE_APPEND_DATA, |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 318 | FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, |
| 319 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 320 | if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) { |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 321 | g_log_file = nullptr; |
[email protected] | 78c6dd6 | 2009-06-08 23:29:11 | [diff] [blame] | 322 | return false; |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 323 | } |
| 324 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 325 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 326 | g_log_file = fopen(g_log_file_name->c_str(), "a"); |
| 327 | if (g_log_file == nullptr) |
| 328 | return false; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 329 | #else |
| 330 | #error Unsupported platform |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 331 | #endif |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 332 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 336 | void CloseFile(FileHandle log) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 337 | #if BUILDFLAG(IS_WIN) |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 338 | CloseHandle(log); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 339 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 340 | fclose(log); |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 341 | #else |
| 342 | #error Unsupported platform |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 343 | #endif |
| 344 | } |
| 345 | |
| 346 | void CloseLogFileUnlocked() { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 347 | if (!g_log_file) |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 348 | return; |
| 349 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 350 | CloseFile(g_log_file); |
| 351 | g_log_file = nullptr; |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 352 | |
| 353 | // If we initialized logging via an externally-provided file descriptor, we |
| 354 | // won't have a log path set and shouldn't try to reopen the log file. |
| 355 | if (!g_log_file_name) |
| 356 | g_logging_destination &= ~LOG_TO_FILE; |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 357 | } |
| 358 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 359 | #if BUILDFLAG(IS_FUCHSIA) |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 360 | inline FuchsiaLogSeverity LogSeverityToFuchsiaLogSeverity( |
| 361 | LogSeverity severity) { |
Fabrice de Gans-Riberi | fb94dff8 | 2021-04-15 21:09:38 | [diff] [blame] | 362 | switch (severity) { |
| 363 | case LOGGING_INFO: |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 364 | return FUCHSIA_LOG_INFO; |
Fabrice de Gans-Riberi | fb94dff8 | 2021-04-15 21:09:38 | [diff] [blame] | 365 | case LOGGING_WARNING: |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 366 | return FUCHSIA_LOG_WARNING; |
Fabrice de Gans-Riberi | fb94dff8 | 2021-04-15 21:09:38 | [diff] [blame] | 367 | case LOGGING_ERROR: |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 368 | return FUCHSIA_LOG_ERROR; |
Fabrice de Gans-Riberi | fb94dff8 | 2021-04-15 21:09:38 | [diff] [blame] | 369 | case LOGGING_FATAL: |
| 370 | // Don't use FX_LOG_FATAL, otherwise fx_logger_log() will abort(). |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 371 | return FUCHSIA_LOG_ERROR; |
Fabrice de Gans-Riberi | fb94dff8 | 2021-04-15 21:09:38 | [diff] [blame] | 372 | } |
| 373 | if (severity > -3) { |
| 374 | // LOGGING_VERBOSE levels 1 and 2. |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 375 | return FUCHSIA_LOG_DEBUG; |
Fabrice de Gans-Riberi | fb94dff8 | 2021-04-15 21:09:38 | [diff] [blame] | 376 | } |
| 377 | // LOGGING_VERBOSE levels 3 and higher, or incorrect levels. |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 378 | return FUCHSIA_LOG_TRACE; |
Fabrice de Gans-Riberi | fb94dff8 | 2021-04-15 21:09:38 | [diff] [blame] | 379 | } |
Fabrice de Gans-Riberi | fb94dff8 | 2021-04-15 21:09:38 | [diff] [blame] | 380 | #endif // defined (OS_FUCHSIA) |
| 381 | |
Benoit Lize | ed67819 | 2022-01-20 10:14:54 | [diff] [blame] | 382 | void WriteToFd(int fd, const char* data, size_t length) { |
| 383 | size_t bytes_written = 0; |
| 384 | int rv; |
| 385 | while (bytes_written < length) { |
| 386 | rv = HANDLE_EINTR(write(fd, data + bytes_written, length - bytes_written)); |
| 387 | if (rv < 0) { |
| 388 | // Give up, nothing we can do now. |
| 389 | break; |
| 390 | } |
| 391 | bytes_written += rv; |
| 392 | } |
| 393 | } |
| 394 | |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 395 | } // namespace |
| 396 | |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 397 | #if defined(DCHECK_IS_CONFIGURABLE) |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 398 | // In DCHECK-enabled Chrome builds, allow the meaning of LOGGING_DCHECK to be |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 399 | // determined at run-time. We default it to INFO, to avoid it triggering |
| 400 | // crashes before the run-time has explicitly chosen the behaviour. |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 401 | BASE_EXPORT logging::LogSeverity LOGGING_DCHECK = LOGGING_INFO; |
Tomas Popela | afffa97 | 2018-11-13 20:42:05 | [diff] [blame] | 402 | #endif // defined(DCHECK_IS_CONFIGURABLE) |
Wez | 289477f | 2017-08-24 20:51:30 | [diff] [blame] | 403 | |
scottmg | 3c957a5 | 2016-12-10 20:57:59 | [diff] [blame] | 404 | // This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to have |
| 405 | // an object of the correct type on the LHS of the unused part of the ternary |
| 406 | // operator. |
| 407 | std::ostream* g_swallow_stream; |
| 408 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 409 | bool BaseInitLoggingImpl(const LoggingSettings& settings) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 410 | #if BUILDFLAG(IS_NACL) |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 411 | // Can log only to the system debug log and stderr. |
| 412 | CHECK_EQ(settings.logging_dest & ~(LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR), |
| 413 | 0u); |
[email protected] | ac07ec5 | 2013-04-22 17:32:45 | [diff] [blame] | 414 | #endif |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 415 | |
Georg Neis | ffe34f65 | 2021-12-27 21:42:36 | [diff] [blame] | 416 | #if BUILDFLAG(IS_CHROMEOS) |
Yuta Hijikata | 8a93e52 | 2020-09-25 08:59:57 | [diff] [blame] | 417 | g_log_format = settings.log_format; |
| 418 | #endif |
| 419 | |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame^] | 420 | #if BUILDFLAG(USE_RUNTIME_VLOG) |
Joshua Peraza | 236556ce | 2020-10-22 23:23:31 | [diff] [blame] | 421 | if (base::CommandLine::InitializedForCurrentProcess()) { |
| 422 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 423 | // Don't bother initializing |g_vlog_info| unless we use one of the |
| 424 | // vlog switches. |
| 425 | if (command_line->HasSwitch(switches::kV) || |
| 426 | command_line->HasSwitch(switches::kVModule)) { |
| 427 | // NOTE: If |g_vlog_info| has already been initialized, it might be in use |
| 428 | // by another thread. Don't delete the old VLogInfo, just create a second |
| 429 | // one. We keep track of both to avoid memory leak warnings. |
| 430 | CHECK(!g_vlog_info_prev); |
| 431 | g_vlog_info_prev = g_vlog_info; |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 432 | |
Joshua Peraza | 236556ce | 2020-10-22 23:23:31 | [diff] [blame] | 433 | g_vlog_info = |
| 434 | new VlogInfo(command_line->GetSwitchValueASCII(switches::kV), |
| 435 | command_line->GetSwitchValueASCII(switches::kVModule), |
| 436 | &g_min_log_level); |
| 437 | } |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 438 | } |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame^] | 439 | #endif // defined(USE_RUNTIME_VLOG) |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 440 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 441 | g_logging_destination = settings.logging_dest; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 442 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 443 | #if BUILDFLAG(IS_FUCHSIA) |
Wez | 224c0bf6 | 2019-05-24 19:26:13 | [diff] [blame] | 444 | if (g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) { |
Wez | 5d4a666c | 2021-10-04 16:41:36 | [diff] [blame] | 445 | GetScopedFxLogger() = base::ScopedFxLogger::CreateForProcess(); |
Wez | 224c0bf6 | 2019-05-24 19:26:13 | [diff] [blame] | 446 | } |
| 447 | #endif |
| 448 | |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 449 | // ignore file options unless logging to file is set. |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 450 | if ((g_logging_destination & LOG_TO_FILE) == 0) |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 451 | return true; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 452 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 453 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Benoit Lize | 5c98a83 | 2020-07-07 16:32:01 | [diff] [blame] | 454 | base::AutoLock guard(GetLoggingLock()); |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 455 | #endif |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 456 | |
| 457 | // Calling InitLogging twice or after some log call has already opened the |
| 458 | // default log file will re-initialize to the new options. |
| 459 | CloseLogFileUnlocked(); |
| 460 | |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 461 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 462 | if (settings.log_file) { |
| 463 | DCHECK(!settings.log_file_path); |
| 464 | g_log_file = settings.log_file; |
| 465 | return true; |
| 466 | } |
| 467 | #endif |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 468 | |
Xiaohan Wang | 5411974b | 2020-10-10 19:36:12 | [diff] [blame] | 469 | DCHECK(settings.log_file_path) << "LOG_TO_FILE set but no log_file_path!"; |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 470 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 471 | if (!g_log_file_name) |
| 472 | g_log_file_name = new PathString(); |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 473 | *g_log_file_name = settings.log_file_path; |
[email protected] | 5e3f7c2 | 2013-06-21 21:15:33 | [diff] [blame] | 474 | if (settings.delete_old == DELETE_OLD_LOG_FILE) |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 475 | DeleteFilePath(*g_log_file_name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 476 | |
[email protected] | c7d5da99 | 2010-10-28 00:20:21 | [diff] [blame] | 477 | return InitializeLogFileHandle(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | void SetMinLogLevel(int level) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 481 | g_min_log_level = std::min(LOGGING_FATAL, level); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | int GetMinLogLevel() { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 485 | return g_min_log_level; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 486 | } |
| 487 | |
skobes | c78c0ad7 | 2015-12-07 20:21:23 | [diff] [blame] | 488 | bool ShouldCreateLogMessage(int severity) { |
| 489 | if (severity < g_min_log_level) |
| 490 | return false; |
| 491 | |
Wez | 6c8acb8 | 2019-07-18 00:32:59 | [diff] [blame] | 492 | // Return true here unless we know ~LogMessage won't do anything. |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 493 | return g_logging_destination != LOG_NONE || g_log_message_handler || |
skobes | c78c0ad7 | 2015-12-07 20:21:23 | [diff] [blame] | 494 | severity >= kAlwaysPrintErrorLevel; |
| 495 | } |
| 496 | |
Wez | 6c8acb8 | 2019-07-18 00:32:59 | [diff] [blame] | 497 | // Returns true when LOG_TO_STDERR flag is set, or |severity| is high. |
| 498 | // If |severity| is high then true will be returned when no log destinations are |
| 499 | // set, or only LOG_TO_FILE is set, since that is useful for local development |
| 500 | // and debugging. |
| 501 | bool ShouldLogToStderr(int severity) { |
| 502 | if (g_logging_destination & LOG_TO_STDERR) |
| 503 | return true; |
Benjamin Lerman | d823d68 | 2021-10-25 11:31:01 | [diff] [blame] | 504 | #if !BUILDFLAG(IS_FUCHSIA) |
| 505 | // High-severity logs go to stderr by default, except on Fuchsia. |
Wez | 6c8acb8 | 2019-07-18 00:32:59 | [diff] [blame] | 506 | if (severity >= kAlwaysPrintErrorLevel) |
| 507 | return (g_logging_destination & ~LOG_TO_FILE) == LOG_NONE; |
Benjamin Lerman | d823d68 | 2021-10-25 11:31:01 | [diff] [blame] | 508 | #endif |
Wez | 6c8acb8 | 2019-07-18 00:32:59 | [diff] [blame] | 509 | return false; |
| 510 | } |
| 511 | |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 512 | int GetVlogVerbosity() { |
| 513 | return std::max(-1, LOG_INFO - GetMinLogLevel()); |
| 514 | } |
| 515 | |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 516 | int GetVlogLevelHelper(const char* file, size_t N) { |
| 517 | DCHECK_GT(N, 0U); |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame^] | 518 | |
| 519 | #if BUILDFLAG(USE_RUNTIME_VLOG) |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 520 | // Note: |g_vlog_info| may change on a different thread during startup |
| 521 | // (but will always be valid or nullptr). |
[email protected] | 064aa16 | 2011-12-03 00:30:08 | [diff] [blame] | 522 | VlogInfo* vlog_info = g_vlog_info; |
| 523 | return vlog_info ? |
| 524 | vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) : |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 525 | GetVlogVerbosity(); |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame^] | 526 | #else |
| 527 | return GetVlogVerbosity(); |
| 528 | #endif // BUILDFLAG(USE_RUNTIME_VLOG) |
[email protected] | 99b7c57f | 2010-09-29 19:26:36 | [diff] [blame] | 529 | } |
| 530 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 531 | void SetLogItems(bool enable_process_id, bool enable_thread_id, |
| 532 | bool enable_timestamp, bool enable_tickcount) { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 533 | g_log_process_id = enable_process_id; |
| 534 | g_log_thread_id = enable_thread_id; |
| 535 | g_log_timestamp = enable_timestamp; |
| 536 | g_log_tickcount = enable_tickcount; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 537 | } |
| 538 | |
James Cook | a0536c3 | 2018-08-01 20:13:31 | [diff] [blame] | 539 | void SetLogPrefix(const char* prefix) { |
| 540 | DCHECK(!prefix || |
| 541 | base::ContainsOnlyChars(prefix, "abcdefghijklmnopqrstuvwxyz")); |
| 542 | g_log_prefix = prefix; |
| 543 | } |
| 544 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 545 | void SetShowErrorDialogs(bool enable_dialogs) { |
| 546 | show_error_dialogs = enable_dialogs; |
| 547 | } |
| 548 | |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 549 | ScopedLogAssertHandler::ScopedLogAssertHandler( |
| 550 | LogAssertHandlerFunction handler) { |
Yannic Bonenberger | 3dcd7fe | 2019-06-08 11:01:45 | [diff] [blame] | 551 | GetLogAssertHandlerStack().push(std::move(handler)); |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | ScopedLogAssertHandler::~ScopedLogAssertHandler() { |
Yannic Bonenberger | 3dcd7fe | 2019-06-08 11:01:45 | [diff] [blame] | 555 | GetLogAssertHandlerStack().pop(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 556 | } |
| 557 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 558 | void SetLogMessageHandler(LogMessageHandlerFunction handler) { |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 559 | g_log_message_handler = handler; |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 560 | } |
| 561 | |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 562 | LogMessageHandlerFunction GetLogMessageHandler() { |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 563 | return g_log_message_handler; |
[email protected] | 64e5cc0 | 2010-11-03 19:20:27 | [diff] [blame] | 564 | } |
| 565 | |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 566 | #if !defined(NDEBUG) |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 567 | // Displays a message box to the user with the error message in it. |
| 568 | // Used for fatal messages, where we close the app simultaneously. |
[email protected] | 561513f | 2010-12-16 23:29:25 | [diff] [blame] | 569 | // This is for developers only; we don't use this in circumstances |
| 570 | // (like release builds) where users could see it, since users don't |
| 571 | // understand these messages anyway. |
[email protected] | d81baca4 | 2010-03-01 13:10:22 | [diff] [blame] | 572 | void DisplayDebugMessageInDialog(const std::string& str) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 573 | if (str.empty()) |
| 574 | return; |
| 575 | |
[email protected] | 81e0a85 | 2010-08-17 00:38:12 | [diff] [blame] | 576 | if (!show_error_dialogs) |
[email protected] | 846ed9c3 | 2010-07-29 20:33:44 | [diff] [blame] | 577 | return; |
| 578 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 579 | #if BUILDFLAG(IS_WIN) |
[email protected] | 561513f | 2010-12-16 23:29:25 | [diff] [blame] | 580 | // We intentionally don't implement a dialog on other platforms. |
| 581 | // You can just look at stderr. |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 582 | if (base::win::IsUser32AndGdi32Available()) { |
| 583 | MessageBoxW(nullptr, base::as_wcstr(base::UTF8ToUTF16(str)), L"Fatal error", |
| 584 | MB_OK | MB_ICONHAND | MB_TOPMOST); |
| 585 | } else { |
| 586 | OutputDebugStringW(base::as_wcstr(base::UTF8ToUTF16(str))); |
| 587 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 588 | #endif // BUILDFLAG(IS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 589 | } |
[email protected] | f2c0549 | 2014-06-17 12:04:23 | [diff] [blame] | 590 | #endif // !defined(NDEBUG) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 591 | |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 592 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
| 593 | : severity_(severity), file_(file), line_(line) { |
| 594 | Init(file, line); |
| 595 | } |
| 596 | |
tnagel | 4a045d3f | 2015-07-12 14:19:28 | [diff] [blame] | 597 | LogMessage::LogMessage(const char* file, int line, const char* condition) |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 598 | : severity_(LOGGING_FATAL), file_(file), line_(line) { |
tnagel | 4a045d3f | 2015-07-12 14:19:28 | [diff] [blame] | 599 | Init(file, line); |
| 600 | stream_ << "Check failed: " << condition << ". "; |
| 601 | } |
| 602 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 603 | LogMessage::~LogMessage() { |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 604 | size_t stack_start = stream_.tellp(); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 605 | #if !defined(OFFICIAL_BUILD) && !BUILDFLAG(IS_NACL) && !defined(__UCLIBC__) && \ |
| 606 | !BUILDFLAG(IS_AIX) |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 607 | if (severity_ == LOGGING_FATAL && !base::debug::BeingDebugged()) { |
brucedawson | 7c559eb | 2015-09-05 00:34:42 | [diff] [blame] | 608 | // Include a stack trace on a fatal, unless a debugger is attached. |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 609 | base::debug::StackTrace stack_trace; |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 610 | stream_ << std::endl; // Newline to separate from log message. |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 611 | stack_trace.OutputToStream(&stream_); |
| 612 | base::debug::TaskTrace task_trace; |
| 613 | if (!task_trace.empty()) |
| 614 | task_trace.OutputToStream(&stream_); |
Chris Hamilton | 306740d | 2019-04-25 18:48:36 | [diff] [blame] | 615 | |
Chris Hamilton | 88808531 | 2019-05-30 00:53:30 | [diff] [blame] | 616 | // Include the IPC context, if any. |
| 617 | // TODO(chrisha): Integrate with symbolization once those tools exist! |
Chris Hamilton | 306740d | 2019-04-25 18:48:36 | [diff] [blame] | 618 | const auto* task = base::TaskAnnotator::CurrentTaskForThread(); |
Chris Hamilton | 88808531 | 2019-05-30 00:53:30 | [diff] [blame] | 619 | if (task && task->ipc_hash) { |
| 620 | stream_ << "IPC message handler context: " |
| 621 | << base::StringPrintf("0x%08X", task->ipc_hash) << std::endl; |
Chris Hamilton | 306740d | 2019-04-25 18:48:36 | [diff] [blame] | 622 | } |
Lukasz Anforowicz | 872567d | 2021-09-08 19:49:22 | [diff] [blame] | 623 | |
| 624 | // Include the crash keys, if any. |
| 625 | base::debug::OutputCrashKeysToStream(stream_); |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 626 | } |
[email protected] | 1d8c270 | 2008-08-19 23:39:32 | [diff] [blame] | 627 | #endif |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 628 | stream_ << std::endl; |
| 629 | std::string str_newline(stream_.str()); |
Nicolò Mazzucato | 6c278d9b | 2019-08-02 16:25:44 | [diff] [blame] | 630 | TRACE_LOG_MESSAGE( |
| 631 | file_, base::StringPiece(str_newline).substr(message_start_), line_); |
[email protected] | d1ccc35a | 2010-03-24 05:03:24 | [diff] [blame] | 632 | |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 633 | // Give any log message handler first dibs on the message. |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 634 | if (g_log_message_handler && |
| 635 | g_log_message_handler(severity_, file_, line_, message_start_, |
| 636 | str_newline)) { |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 637 | // The handler took care of it, no further processing. |
[email protected] | 2b07b841 | 2009-11-25 15:26:34 | [diff] [blame] | 638 | return; |
[email protected] | 162ac0f | 2010-11-04 15:50:49 | [diff] [blame] | 639 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 640 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 641 | if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 642 | #if BUILDFLAG(IS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 643 | OutputDebugStringA(str_newline.c_str()); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 644 | #elif BUILDFLAG(IS_APPLE) |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 645 | // In LOG_TO_SYSTEM_DEBUG_LOG mode, log messages are always written to |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 646 | // stderr. If stderr is /dev/null, also log via ASL (Apple System Log) or |
| 647 | // its successor OS_LOG. If there's something weird about stderr, assume |
| 648 | // that log messages are going nowhere and log via ASL/OS_LOG too. |
| 649 | // Messages logged via ASL/OS_LOG show up in Console.app. |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 650 | // |
| 651 | // Programs started by launchd, as UI applications normally are, have had |
| 652 | // stderr connected to /dev/null since OS X 10.8. Prior to that, stderr was |
| 653 | // a pipe to launchd, which logged what it received (see log_redirect_fd in |
| 654 | // 10.7.5 launchd-392.39/launchd/src/launchd_core_logic.c). |
| 655 | // |
| 656 | // Another alternative would be to determine whether stderr is a pipe to |
| 657 | // launchd and avoid logging via ASL only in that case. See 10.7.5 |
| 658 | // CF-635.21/CFUtilities.c also_do_stderr(). This would result in logging to |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 659 | // both stderr and ASL/OS_LOG even in tests, where it's undesirable to log |
| 660 | // to the system log at all. |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 661 | // |
| 662 | // Note that the ASL client by default discards messages whose levels are |
| 663 | // below ASL_LEVEL_NOTICE. It's possible to change that with |
| 664 | // asl_set_filter(), but this is pointless because syslogd normally applies |
| 665 | // the same filter. |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 666 | const bool log_to_system = []() { |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 667 | struct stat stderr_stat; |
| 668 | if (fstat(fileno(stderr), &stderr_stat) == -1) { |
| 669 | return true; |
| 670 | } |
| 671 | if (!S_ISCHR(stderr_stat.st_mode)) { |
| 672 | return false; |
| 673 | } |
| 674 | |
| 675 | struct stat dev_null_stat; |
| 676 | if (stat(_PATH_DEVNULL, &dev_null_stat) == -1) { |
| 677 | return true; |
| 678 | } |
| 679 | |
| 680 | return !S_ISCHR(dev_null_stat.st_mode) || |
| 681 | stderr_stat.st_rdev == dev_null_stat.st_rdev; |
| 682 | }(); |
| 683 | |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 684 | if (log_to_system) { |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 685 | // Log roughly the same way that CFLog() and NSLog() would. See 10.10.5 |
| 686 | // CF-1153.18/CFUtilities.c __CFLogCString(). |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 687 | CFBundleRef main_bundle = CFBundleGetMainBundle(); |
| 688 | CFStringRef main_bundle_id_cf = |
| 689 | main_bundle ? CFBundleGetIdentifier(main_bundle) : nullptr; |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 690 | std::string main_bundle_id = |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 691 | main_bundle_id_cf ? base::SysCFStringRefToUTF8(main_bundle_id_cf) |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 692 | : std::string(""); |
| 693 | #if defined(USE_ASL) |
| 694 | // The facility is set to the main bundle ID if available. Otherwise, |
| 695 | // "com.apple.console" is used. |
| 696 | const class ASLClient { |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 697 | public: |
Bruce Dawson | 4c6f8e1 | 2017-11-16 04:35:59 | [diff] [blame] | 698 | explicit ASLClient(const std::string& facility) |
| 699 | : client_(asl_open(nullptr, facility.c_str(), ASL_OPT_NO_DELAY)) {} |
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 700 | ASLClient(const ASLClient&) = delete; |
| 701 | ASLClient& operator=(const ASLClient&) = delete; |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 702 | ~ASLClient() { asl_close(client_); } |
| 703 | |
| 704 | aslclient get() const { return client_; } |
| 705 | |
| 706 | private: |
| 707 | aslclient client_; |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 708 | } asl_client(main_bundle_id.empty() ? main_bundle_id |
| 709 | : "com.apple.console"); |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 710 | |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 711 | const class ASLMessage { |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 712 | public: |
| 713 | ASLMessage() : message_(asl_new(ASL_TYPE_MSG)) {} |
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 714 | ASLMessage(const ASLMessage&) = delete; |
| 715 | ASLMessage& operator=(const ASLMessage&) = delete; |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 716 | ~ASLMessage() { asl_free(message_); } |
| 717 | |
| 718 | aslmsg get() const { return message_; } |
| 719 | |
| 720 | private: |
| 721 | aslmsg message_; |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 722 | } asl_message; |
| 723 | |
| 724 | // By default, messages are only readable by the admin group. Explicitly |
| 725 | // make them readable by the user generating the messages. |
| 726 | char euid_string[12]; |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 727 | snprintf(euid_string, std::size(euid_string), "%d", geteuid()); |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 728 | asl_set(asl_message.get(), ASL_KEY_READ_UID, euid_string); |
| 729 | |
| 730 | // Map Chrome log severities to ASL log levels. |
| 731 | const char* const asl_level_string = [](LogSeverity severity) { |
| 732 | // ASL_LEVEL_* are ints, but ASL needs equivalent strings. This |
| 733 | // non-obvious two-step macro trick achieves what's needed. |
| 734 | // https://gcc.gnu.org/onlinedocs/cpp/Stringification.html |
| 735 | #define ASL_LEVEL_STR(level) ASL_LEVEL_STR_X(level) |
| 736 | #define ASL_LEVEL_STR_X(level) #level |
| 737 | switch (severity) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 738 | case LOGGING_INFO: |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 739 | return ASL_LEVEL_STR(ASL_LEVEL_INFO); |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 740 | case LOGGING_WARNING: |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 741 | return ASL_LEVEL_STR(ASL_LEVEL_WARNING); |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 742 | case LOGGING_ERROR: |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 743 | return ASL_LEVEL_STR(ASL_LEVEL_ERR); |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 744 | case LOGGING_FATAL: |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 745 | return ASL_LEVEL_STR(ASL_LEVEL_CRIT); |
| 746 | default: |
| 747 | return severity < 0 ? ASL_LEVEL_STR(ASL_LEVEL_DEBUG) |
| 748 | : ASL_LEVEL_STR(ASL_LEVEL_NOTICE); |
| 749 | } |
| 750 | #undef ASL_LEVEL_STR |
| 751 | #undef ASL_LEVEL_STR_X |
| 752 | }(severity_); |
| 753 | asl_set(asl_message.get(), ASL_KEY_LEVEL, asl_level_string); |
| 754 | |
| 755 | asl_set(asl_message.get(), ASL_KEY_MSG, str_newline.c_str()); |
| 756 | |
| 757 | asl_send(asl_client.get(), asl_message.get()); |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 758 | #else // !defined(USE_ASL) |
| 759 | const class OSLog { |
| 760 | public: |
| 761 | explicit OSLog(const char* subsystem) |
| 762 | : os_log_(subsystem ? os_log_create(subsystem, "chromium_logging") |
| 763 | : OS_LOG_DEFAULT) {} |
David Bienvenu | b4b441e | 2020-09-23 05:49:57 | [diff] [blame] | 764 | OSLog(const OSLog&) = delete; |
| 765 | OSLog& operator=(const OSLog&) = delete; |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 766 | ~OSLog() { |
| 767 | if (os_log_ != OS_LOG_DEFAULT) { |
| 768 | os_release(os_log_); |
| 769 | } |
| 770 | } |
| 771 | os_log_t get() const { return os_log_; } |
| 772 | |
| 773 | private: |
| 774 | os_log_t os_log_; |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 775 | } log(main_bundle_id.empty() ? nullptr : main_bundle_id.c_str()); |
| 776 | const os_log_type_t os_log_type = [](LogSeverity severity) { |
| 777 | switch (severity) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 778 | case LOGGING_INFO: |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 779 | return OS_LOG_TYPE_INFO; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 780 | case LOGGING_WARNING: |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 781 | return OS_LOG_TYPE_DEFAULT; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 782 | case LOGGING_ERROR: |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 783 | return OS_LOG_TYPE_ERROR; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 784 | case LOGGING_FATAL: |
Eric Noyau | fce10070 | 2017-10-16 09:46:34 | [diff] [blame] | 785 | return OS_LOG_TYPE_FAULT; |
| 786 | default: |
| 787 | return severity < 0 ? OS_LOG_TYPE_DEBUG : OS_LOG_TYPE_DEFAULT; |
| 788 | } |
| 789 | }(severity_); |
| 790 | os_log_with_type(log.get(), os_log_type, "%{public}s", |
| 791 | str_newline.c_str()); |
| 792 | #endif // defined(USE_ASL) |
mark | 4c7449c | 2015-11-10 19:53:42 | [diff] [blame] | 793 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 794 | #elif BUILDFLAG(IS_ANDROID) |
[email protected] | efbae7da | 2013-05-21 22:39:25 | [diff] [blame] | 795 | android_LogPriority priority = |
| 796 | (severity_ < 0) ? ANDROID_LOG_VERBOSE : ANDROID_LOG_UNKNOWN; |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 797 | switch (severity_) { |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 798 | case LOGGING_INFO: |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 799 | priority = ANDROID_LOG_INFO; |
| 800 | break; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 801 | case LOGGING_WARNING: |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 802 | priority = ANDROID_LOG_WARN; |
| 803 | break; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 804 | case LOGGING_ERROR: |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 805 | priority = ANDROID_LOG_ERROR; |
| 806 | break; |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 807 | case LOGGING_FATAL: |
[email protected] | 3132e35c | 2011-07-07 20:46:50 | [diff] [blame] | 808 | priority = ANDROID_LOG_FATAL; |
| 809 | break; |
| 810 | } |
Tomasz Śniatowski | 23dd15af | 2019-02-15 08:32:03 | [diff] [blame] | 811 | const char kAndroidLogTag[] = "chromium"; |
Xianzhu Wang | ae8d96a3 | 2018-10-16 20:41:13 | [diff] [blame] | 812 | #if DCHECK_IS_ON() |
| 813 | // Split the output by new lines to prevent the Android system from |
| 814 | // truncating the log. |
Tomasz Śniatowski | 23dd15af | 2019-02-15 08:32:03 | [diff] [blame] | 815 | std::vector<std::string> lines = base::SplitString( |
| 816 | str_newline, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
| 817 | // str_newline has an extra newline appended to it (at the top of this |
| 818 | // function), so skip the last split element to avoid needlessly |
| 819 | // logging an empty string. |
| 820 | lines.pop_back(); |
| 821 | for (const auto& line : lines) |
| 822 | __android_log_write(priority, kAndroidLogTag, line.c_str()); |
Xianzhu Wang | ae8d96a3 | 2018-10-16 20:41:13 | [diff] [blame] | 823 | #else |
| 824 | // The Android system may truncate the string if it's too long. |
Tomasz Śniatowski | 23dd15af | 2019-02-15 08:32:03 | [diff] [blame] | 825 | __android_log_write(priority, kAndroidLogTag, str_newline.c_str()); |
[email protected] | 107bc0f1 | 2008-08-26 17:48:18 | [diff] [blame] | 826 | #endif |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 827 | #elif BUILDFLAG(IS_FUCHSIA) |
Wez | 45e3ffe | 2021-09-24 02:10:02 | [diff] [blame] | 828 | // LogMessage() will silently drop the message if the logger is not valid. |
Wez | a38cc53 | 2021-12-16 22:58:20 | [diff] [blame] | 829 | // Skip the final character of |str_newline|, since LogMessage() will add |
| 830 | // a newline. |
| 831 | const auto message = base::StringPiece(str_newline).substr(message_start_); |
| 832 | GetScopedFxLogger().LogMessage(file_, line_, |
| 833 | message.substr(0, message.size() - 1), |
| 834 | LogSeverityToFuchsiaLogSeverity(severity_)); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 835 | #endif // BUILDFLAG(IS_FUCHSIA) |
Sharon Yang | 7cb919a | 2019-05-20 20:27:15 | [diff] [blame] | 836 | } |
| 837 | |
Wez | 6c8acb8 | 2019-07-18 00:32:59 | [diff] [blame] | 838 | if (ShouldLogToStderr(severity_)) { |
Benoit Lize | ed67819 | 2022-01-20 10:14:54 | [diff] [blame] | 839 | // Not using fwrite() here, as there are crashes on Windows when CRT calls |
| 840 | // malloc() internally, triggering an OOM crash. This likely means that the |
| 841 | // process is close to OOM, but at least get the proper error message out, |
| 842 | // and give the caller a chance to free() up some resources. For instance if |
| 843 | // the calling code is: |
| 844 | // |
| 845 | // allocate_something(); |
| 846 | // if (!TryToDoSomething()) { |
| 847 | // LOG(ERROR) << "Something went wrong"; |
| 848 | // free_something(); |
| 849 | // } |
| 850 | WriteToFd(STDERR_FILENO, str_newline.data(), str_newline.size()); |
[email protected] | f6abeba | 2008-08-08 13:27:28 | [diff] [blame] | 851 | } |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 852 | |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 853 | if ((g_logging_destination & LOG_TO_FILE) != 0) { |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 854 | // We can have multiple threads and/or processes, so try to prevent them |
| 855 | // from clobbering each other's writes. |
| 856 | // If the client app did not call InitLogging, and the lock has not |
| 857 | // been created do it now. We do this on demand, but if two threads try |
| 858 | // to do this at the same time, there will be a race condition to create |
| 859 | // the lock. This is why InitLogging should be called from the main |
| 860 | // thread at the beginning of execution. |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 861 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Benoit Lize | 5c98a83 | 2020-07-07 16:32:01 | [diff] [blame] | 862 | base::AutoLock guard(GetLoggingLock()); |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 863 | #endif |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 864 | if (InitializeLogFileHandle()) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 865 | #if BUILDFLAG(IS_WIN) |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 866 | DWORD num_written; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 867 | WriteFile(g_log_file, |
[email protected] | 5b84fe3 | 2010-09-14 22:24:55 | [diff] [blame] | 868 | static_cast<const void*>(str_newline.c_str()), |
| 869 | static_cast<DWORD>(str_newline.length()), |
| 870 | &num_written, |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 871 | nullptr); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 872 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Avi Drissman | 933398e | 2022-01-22 00:55:42 | [diff] [blame] | 873 | std::ignore = |
| 874 | fwrite(str_newline.data(), str_newline.size(), 1, g_log_file); |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 875 | fflush(g_log_file); |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 876 | #else |
| 877 | #error Unsupported platform |
[email protected] | cba2196 | 2010-08-31 22:35:55 | [diff] [blame] | 878 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 882 | if (severity_ == LOGGING_FATAL) { |
bcwhite | 7a30eb4 | 2016-12-02 21:23:40 | [diff] [blame] | 883 | // Write the log message to the global activity tracker, if running. |
| 884 | base::debug::GlobalActivityTracker* tracker = |
| 885 | base::debug::GlobalActivityTracker::Get(); |
| 886 | if (tracker) |
| 887 | tracker->RecordLogMessage(str_newline); |
| 888 | |
Wez | d78fee6 | 2020-09-11 18:29:14 | [diff] [blame] | 889 | char str_stack[1024]; |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 890 | base::strlcpy(str_stack, str_newline.data(), std::size(str_stack)); |
Wez | e976b73 | 2018-10-20 03:37:31 | [diff] [blame] | 891 | base::debug::Alias(&str_stack); |
[email protected] | eb4c4d03 | 2012-04-03 18:45:05 | [diff] [blame] | 892 | |
Yannic Bonenberger | 3dcd7fe | 2019-06-08 11:01:45 | [diff] [blame] | 893 | if (!GetLogAssertHandlerStack().empty()) { |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 894 | LogAssertHandlerFunction log_assert_handler = |
Yannic Bonenberger | 3dcd7fe | 2019-06-08 11:01:45 | [diff] [blame] | 895 | GetLogAssertHandlerStack().top(); |
alex-ac | cc1bde6 | 2017-04-19 08:33:55 | [diff] [blame] | 896 | |
| 897 | if (log_assert_handler) { |
| 898 | log_assert_handler.Run( |
| 899 | file_, line_, |
| 900 | base::StringPiece(str_newline.c_str() + message_start_, |
| 901 | stack_start - message_start_), |
| 902 | base::StringPiece(str_newline.c_str() + stack_start)); |
| 903 | } |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 904 | } else { |
[email protected] | 82d89ab | 2014-02-28 18:25:34 | [diff] [blame] | 905 | // Don't use the string with the newline, get a fresh version to send to |
| 906 | // the debug message process. We also don't display assertions to the |
| 907 | // user in release mode. The enduser can't do anything with this |
| 908 | // information, and displaying message boxes when the application is |
| 909 | // hosed can cause additional problems. |
[email protected] | 4d590127 | 2008-11-06 00:33:50 | [diff] [blame] | 910 | #ifndef NDEBUG |
brucedawson | 7c559eb | 2015-09-05 00:34:42 | [diff] [blame] | 911 | if (!base::debug::BeingDebugged()) { |
| 912 | // Displaying a dialog is unnecessary when debugging and can complicate |
| 913 | // debugging. |
| 914 | DisplayDebugMessageInDialog(stream_.str()); |
| 915 | } |
[email protected] | 4d590127 | 2008-11-06 00:33:50 | [diff] [blame] | 916 | #endif |
Daniel Cheng | 4f5034d | 2022-03-04 18:34:31 | [diff] [blame] | 917 | |
[email protected] | 82d89ab | 2014-02-28 18:25:34 | [diff] [blame] | 918 | // Crash the process to generate a dump. |
Torne (Richard Coles) | 54b86796a6 | 2018-07-24 14:59:52 | [diff] [blame] | 919 | IMMEDIATE_CRASH(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 924 | // writes the common header info to the stream |
| 925 | void LogMessage::Init(const char* file, int line) { |
| 926 | base::StringPiece filename(file); |
| 927 | size_t last_slash_pos = filename.find_last_of("\\/"); |
| 928 | if (last_slash_pos != base::StringPiece::npos) |
| 929 | filename.remove_prefix(last_slash_pos + 1); |
| 930 | |
Georg Neis | ffe34f65 | 2021-12-27 21:42:36 | [diff] [blame] | 931 | #if BUILDFLAG(IS_CHROMEOS) |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 932 | if (g_log_format == LogFormat::LOG_FORMAT_SYSLOG) { |
| 933 | InitWithSyslogPrefix( |
| 934 | filename, line, TickCount(), log_severity_name(severity_), g_log_prefix, |
| 935 | g_log_process_id, g_log_thread_id, g_log_timestamp, g_log_tickcount); |
| 936 | } else |
Georg Neis | ffe34f65 | 2021-12-27 21:42:36 | [diff] [blame] | 937 | #endif // BUILDFLAG(IS_CHROMEOS) |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 938 | { |
| 939 | // TODO(darin): It might be nice if the columns were fixed width. |
| 940 | stream_ << '['; |
| 941 | if (g_log_prefix) |
| 942 | stream_ << g_log_prefix << ':'; |
| 943 | if (g_log_process_id) |
| 944 | stream_ << base::GetUniqueIdForProcess() << ':'; |
| 945 | if (g_log_thread_id) |
| 946 | stream_ << base::PlatformThread::CurrentId() << ':'; |
| 947 | if (g_log_timestamp) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 948 | #if BUILDFLAG(IS_WIN) |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 949 | SYSTEMTIME local_time; |
| 950 | GetLocalTime(&local_time); |
| 951 | stream_ << std::setfill('0') |
| 952 | << std::setw(2) << local_time.wMonth |
| 953 | << std::setw(2) << local_time.wDay |
| 954 | << '/' |
| 955 | << std::setw(2) << local_time.wHour |
| 956 | << std::setw(2) << local_time.wMinute |
| 957 | << std::setw(2) << local_time.wSecond |
| 958 | << '.' |
| 959 | << std::setw(3) << local_time.wMilliseconds |
| 960 | << ':'; |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 961 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 962 | timeval tv; |
| 963 | gettimeofday(&tv, nullptr); |
| 964 | time_t t = tv.tv_sec; |
| 965 | struct tm local_time; |
| 966 | localtime_r(&t, &local_time); |
| 967 | struct tm* tm_time = &local_time; |
| 968 | stream_ << std::setfill('0') |
| 969 | << std::setw(2) << 1 + tm_time->tm_mon |
| 970 | << std::setw(2) << tm_time->tm_mday |
| 971 | << '/' |
| 972 | << std::setw(2) << tm_time->tm_hour |
| 973 | << std::setw(2) << tm_time->tm_min |
| 974 | << std::setw(2) << tm_time->tm_sec |
| 975 | << '.' |
| 976 | << std::setw(6) << tv.tv_usec |
| 977 | << ':'; |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 978 | #else |
| 979 | #error Unsupported platform |
djkurtz | 543a3be | 2016-11-30 14:17:34 | [diff] [blame] | 980 | #endif |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 981 | } |
| 982 | if (g_log_tickcount) |
| 983 | stream_ << TickCount() << ':'; |
| 984 | if (severity_ >= 0) { |
| 985 | stream_ << log_severity_name(severity_); |
| 986 | } else { |
| 987 | stream_ << "VERBOSE" << -severity_; |
| 988 | } |
| 989 | stream_ << ":" << filename << "(" << line << ")] "; |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 990 | } |
pkasting | 9cf9b94a | 2014-10-01 22:18:43 | [diff] [blame] | 991 | message_start_ = stream_.str().length(); |
[email protected] | eae9c06 | 2011-01-11 00:50:59 | [diff] [blame] | 992 | } |
| 993 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 994 | #if BUILDFLAG(IS_WIN) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 995 | // This has already been defined in the header, but defining it again as DWORD |
| 996 | // ensures that the type used in the header is equivalent to DWORD. If not, |
| 997 | // the redefinition is a compile error. |
| 998 | typedef DWORD SystemErrorCode; |
| 999 | #endif |
| 1000 | |
| 1001 | SystemErrorCode GetLastSystemErrorCode() { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1002 | #if BUILDFLAG(IS_WIN) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1003 | return ::GetLastError(); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1004 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1005 | return errno; |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1006 | #endif |
| 1007 | } |
| 1008 | |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 1009 | BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1010 | #if BUILDFLAG(IS_WIN) |
thestig | 75f8735 | 2014-12-03 21:42:27 | [diff] [blame] | 1011 | const int kErrorMessageBufferSize = 256; |
| 1012 | char msgbuf[kErrorMessageBufferSize]; |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 1013 | DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 1014 | DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf, |
Daniel Cheng | f45f4760 | 2022-02-28 22:38:32 | [diff] [blame] | 1015 | std::size(msgbuf), nullptr); |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 1016 | if (len) { |
| 1017 | // Messages returned by system end with line breaks. |
| 1018 | return base::CollapseWhitespaceASCII(msgbuf, true) + |
Bruce Dawson | 1917584 | 2017-08-02 17:00:45 | [diff] [blame] | 1019 | base::StringPrintf(" (0x%lX)", error_code); |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 1020 | } |
Bruce Dawson | 1917584 | 2017-08-02 17:00:45 | [diff] [blame] | 1021 | return base::StringPrintf("Error (0x%lX) while retrieving error. (0x%lX)", |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 1022 | GetLastError(), error_code); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1023 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Robert Sesek | d2f495f | 2017-07-25 22:03:14 | [diff] [blame] | 1024 | return base::safe_strerror(error_code) + |
| 1025 | base::StringPrintf(" (%d)", error_code); |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1026 | #endif // BUILDFLAG(IS_WIN) |
Fabrice de Gans-Riberi | 306871de | 2018-05-16 19:38:39 | [diff] [blame] | 1027 | } |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1028 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1029 | #if BUILDFLAG(IS_WIN) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1030 | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
| 1031 | int line, |
| 1032 | LogSeverity severity, |
| 1033 | SystemErrorCode err) |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 1034 | : LogMessage(file, line, severity), err_(err) {} |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1035 | |
| 1036 | Win32ErrorLogMessage::~Win32ErrorLogMessage() { |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 1037 | stream() << ": " << SystemErrorCodeToString(err_); |
[email protected] | 20909e7 | 2012-04-05 16:57:06 | [diff] [blame] | 1038 | // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a |
| 1039 | // field) and use Alias in hopes that it makes it into crash dumps. |
| 1040 | DWORD last_error = err_; |
| 1041 | base::debug::Alias(&last_error); |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1042 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1043 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1044 | ErrnoLogMessage::ErrnoLogMessage(const char* file, |
| 1045 | int line, |
| 1046 | LogSeverity severity, |
| 1047 | SystemErrorCode err) |
Hans Wennborg | 12aea3e | 2020-04-14 15:29:00 | [diff] [blame] | 1048 | : LogMessage(file, line, severity), err_(err) {} |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1049 | |
| 1050 | ErrnoLogMessage::~ErrnoLogMessage() { |
[email protected] | c914d8a | 2014-04-23 01:11:01 | [diff] [blame] | 1051 | stream() << ": " << SystemErrorCodeToString(err_); |
Robert Sesek | d2f495f | 2017-07-25 22:03:14 | [diff] [blame] | 1052 | // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a |
| 1053 | // field) and use Alias in hopes that it makes it into crash dumps. |
| 1054 | int last_error = err_; |
| 1055 | base::debug::Alias(&last_error); |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1056 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1057 | #endif // BUILDFLAG(IS_WIN) |
[email protected] | d8617a6 | 2009-10-09 23:52:20 | [diff] [blame] | 1058 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1059 | void CloseLogFile() { |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1060 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Benoit Lize | 5c98a83 | 2020-07-07 16:32:01 | [diff] [blame] | 1061 | base::AutoLock guard(GetLoggingLock()); |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 1062 | #endif |
[email protected] | 17dcf75 | 2013-07-15 21:47:09 | [diff] [blame] | 1063 | CloseLogFileUnlocked(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1064 | } |
| 1065 | |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 1066 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 1067 | FILE* DuplicateLogFILE() { |
| 1068 | if ((g_logging_destination & LOG_TO_FILE) == 0 || !InitializeLogFileHandle()) |
| 1069 | return nullptr; |
| 1070 | |
| 1071 | int log_fd = fileno(g_log_file); |
| 1072 | if (log_fd == -1) |
| 1073 | return nullptr; |
| 1074 | base::ScopedFD dup_fd(dup(log_fd)); |
| 1075 | if (dup_fd == -1) |
| 1076 | return nullptr; |
| 1077 | FILE* duplicate = fdopen(dup_fd.get(), "a"); |
| 1078 | if (!duplicate) |
| 1079 | return nullptr; |
Avi Drissman | 933398e | 2022-01-22 00:55:42 | [diff] [blame] | 1080 | std::ignore = dup_fd.release(); |
Robbie McElrath | 8bf4984 | 2019-08-20 22:22:53 | [diff] [blame] | 1081 | return duplicate; |
| 1082 | } |
| 1083 | #endif |
| 1084 | |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 1085 | // Used for testing. Declared in test/scoped_logging_settings.h. |
| 1086 | ScopedLoggingSettings::ScopedLoggingSettings() |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 1087 | : min_log_level_(g_min_log_level), |
| 1088 | logging_destination_(g_logging_destination), |
Georg Neis | ffe34f65 | 2021-12-27 21:42:36 | [diff] [blame] | 1089 | #if BUILDFLAG(IS_CHROMEOS) |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 1090 | log_format_(g_log_format), |
| 1091 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
| 1092 | enable_process_id_(g_log_process_id), |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 1093 | enable_thread_id_(g_log_thread_id), |
| 1094 | enable_timestamp_(g_log_timestamp), |
| 1095 | enable_tickcount_(g_log_tickcount), |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 1096 | log_prefix_(g_log_prefix), |
| 1097 | message_handler_(g_log_message_handler) { |
| 1098 | if (g_log_file_name) |
| 1099 | log_file_name_ = std::make_unique<PathString>(*g_log_file_name); |
| 1100 | // Duplicating |g_log_file| is complex & unnecessary for this test helpers' |
| 1101 | // use-cases, and so long as |g_log_file_name| is set, it will be re-opened |
| 1102 | // automatically anyway, when required, so just close the existing one. |
| 1103 | if (g_log_file) { |
| 1104 | CHECK(g_log_file_name) << "Un-named |log_file| is not supported."; |
| 1105 | CloseLogFileUnlocked(); |
| 1106 | } |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | ScopedLoggingSettings::~ScopedLoggingSettings() { |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 1110 | // Re-initialize logging via the normal path. This will clean up old file |
| 1111 | // name and handle state, including re-initializing the VLOG internal state. |
| 1112 | CHECK(InitLogging({ |
| 1113 | .logging_dest = logging_destination_, |
| 1114 | .log_file_path = log_file_name_ ? log_file_name_->data() : nullptr, |
Georg Neis | ffe34f65 | 2021-12-27 21:42:36 | [diff] [blame] | 1115 | #if BUILDFLAG(IS_CHROMEOS) |
Wez | 24427036 | 2021-05-04 22:50:58 | [diff] [blame] | 1116 | .log_format = log_format_ |
| 1117 | #endif |
| 1118 | })) << "~ScopedLoggingSettings() failed to restore settings."; |
| 1119 | |
| 1120 | // Restore plain data settings. |
| 1121 | SetMinLogLevel(min_log_level_); |
| 1122 | SetLogItems(enable_process_id_, enable_thread_id_, enable_timestamp_, |
| 1123 | enable_tickcount_); |
| 1124 | SetLogPrefix(log_prefix_); |
| 1125 | SetLogMessageHandler(message_handler_); |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 1126 | } |
| 1127 | |
Georg Neis | ffe34f65 | 2021-12-27 21:42:36 | [diff] [blame] | 1128 | #if BUILDFLAG(IS_CHROMEOS) |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 1129 | void ScopedLoggingSettings::SetLogFormat(LogFormat log_format) const { |
| 1130 | g_log_format = log_format; |
| 1131 | } |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 1132 | #endif // BUILDFLAG(IS_CHROMEOS_ASH) |
Yuta Hijikata | 9b7279a | 2020-08-26 16:10:54 | [diff] [blame] | 1133 | |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1134 | void RawLog(int level, const char* message) { |
erikchen | 0c9fe71 | 2016-03-11 22:07:49 | [diff] [blame] | 1135 | if (level >= g_min_log_level && message) { |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1136 | const size_t message_len = strlen(message); |
Benoit Lize | ed67819 | 2022-01-20 10:14:54 | [diff] [blame] | 1137 | WriteToFd(STDERR_FILENO, message, message_len); |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1138 | |
| 1139 | if (message_len > 0 && message[message_len - 1] != '\n') { |
Benoit Lize | ed67819 | 2022-01-20 10:14:54 | [diff] [blame] | 1140 | int rv; |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1141 | do { |
| 1142 | rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1)); |
| 1143 | if (rv < 0) { |
| 1144 | // Give up, nothing we can do now. |
| 1145 | break; |
| 1146 | } |
| 1147 | } while (rv != 1); |
| 1148 | } |
| 1149 | } |
| 1150 | |
Lei Zhang | 93dd4257 | 2020-10-23 18:45:53 | [diff] [blame] | 1151 | if (level == LOGGING_FATAL) |
Daniel Cheng | 4f5034d | 2022-03-04 18:34:31 | [diff] [blame] | 1152 | IMMEDIATE_CRASH(); |
[email protected] | e36ddc8 | 2009-12-08 04:22:50 | [diff] [blame] | 1153 | } |
| 1154 | |
[email protected] | 34a90773 | 2012-01-20 06:33:27 | [diff] [blame] | 1155 | // This was defined at the beginning of this file. |
| 1156 | #undef write |
| 1157 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 1158 | #if BUILDFLAG(IS_WIN) |
ananta | 61762fb | 2015-09-18 01:00:09 | [diff] [blame] | 1159 | bool IsLoggingToFileEnabled() { |
| 1160 | return g_logging_destination & LOG_TO_FILE; |
| 1161 | } |
| 1162 | |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 1163 | std::wstring GetLogFileFullPath() { |
thestig | 3e4787d | 2015-05-19 19:31:52 | [diff] [blame] | 1164 | if (g_log_file_name) |
| 1165 | return *g_log_file_name; |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 1166 | return std::wstring(); |
[email protected] | f01b88a | 2013-02-27 22:04:00 | [diff] [blame] | 1167 | } |
| 1168 | #endif |
| 1169 | |
Xiyuan Xia | a0559da | 2022-05-05 19:42:45 | [diff] [blame^] | 1170 | #if !BUILDFLAG(USE_RUNTIME_VLOG) |
| 1171 | int GetDisableAllVLogLevel() { |
| 1172 | return -1; |
| 1173 | } |
| 1174 | #endif // !BUILDFLAG(USE_RUNTIME_VLOG) |
| 1175 | |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 1176 | } // namespace logging |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1177 | |
[email protected] | 81411c6 | 2014-07-08 23:03:06 | [diff] [blame] | 1178 | std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
Jan Wilken Dörrie | 4a498d8c | 2021-01-20 10:19:39 | [diff] [blame] | 1179 | return out << (wstr ? base::WStringPiece(wstr) : base::WStringPiece()); |
| 1180 | } |
| 1181 | |
| 1182 | std::ostream& std::operator<<(std::ostream& out, const std::wstring& wstr) { |
| 1183 | return out << base::WStringPiece(wstr); |
| 1184 | } |
| 1185 | |
| 1186 | std::ostream& std::operator<<(std::ostream& out, const char16_t* str16) { |
Jan Wilken Dörrie | 43d5378f | 2021-03-10 12:04:46 | [diff] [blame] | 1187 | return out << (str16 ? base::StringPiece16(str16) : base::StringPiece16()); |
Jan Wilken Dörrie | 4a498d8c | 2021-01-20 10:19:39 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | std::ostream& std::operator<<(std::ostream& out, const std::u16string& str16) { |
Jan Wilken Dörrie | 43d5378f | 2021-03-10 12:04:46 | [diff] [blame] | 1191 | return out << base::StringPiece16(str16); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 1192 | } |