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