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