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