Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/logging.h" |
| 6 | |
| 7 | #include <limits.h> |
| 8 | #include <stdint.h> |
| 9 | |
Scott Graham | ce047c9 | 2018-06-19 15:56:56 -0700 | [diff] [blame] | 10 | #include <thread> |
| 11 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 12 | #include "base/macros.h" |
Scott Graham | 76a8dc7 | 2018-06-18 13:37:29 -0700 | [diff] [blame] | 13 | #include "util/build_config.h" |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 14 | |
| 15 | #if defined(OS_WIN) |
| 16 | #include <io.h> |
| 17 | #include <windows.h> |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 18 | // Windows warns on using write(). It prefers _write(). |
| 19 | #define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) |
| 20 | // Windows doesn't define STDERR_FILENO. Define it here. |
| 21 | #define STDERR_FILENO 2 |
| 22 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 23 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
Scott Graham | ce047c9 | 2018-06-19 15:56:56 -0700 | [diff] [blame] | 24 | #include <sys/time.h> |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 25 | #include <time.h> |
| 26 | #endif |
| 27 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 28 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 29 | #include <errno.h> |
| 30 | #include <paths.h> |
| 31 | #include <pthread.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <unistd.h> |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #include <algorithm> |
| 40 | #include <cstring> |
| 41 | #include <ctime> |
| 42 | #include <iomanip> |
| 43 | #include <ostream> |
| 44 | #include <string> |
| 45 | #include <utility> |
| 46 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 47 | #include "base/callback.h" |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 48 | #include "base/containers/stack.h" |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 49 | #include "base/posix/eintr_wrapper.h" |
| 50 | #include "base/strings/string_piece.h" |
| 51 | #include "base/strings/string_util.h" |
| 52 | #include "base/strings/stringprintf.h" |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 53 | #include "base/strings/utf_string_conversions.h" |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 54 | |
| 55 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 56 | #include "base/posix/safe_strerror.h" |
| 57 | #endif |
| 58 | |
| 59 | namespace logging { |
| 60 | |
| 61 | namespace { |
| 62 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 63 | const char* const log_severity_names[] = {"INFO", "WARNING", "ERROR", "FATAL"}; |
| 64 | static_assert(LOG_NUM_SEVERITIES == arraysize(log_severity_names), |
| 65 | "Incorrect number of log_severity_names"); |
| 66 | |
| 67 | const char* log_severity_name(int severity) { |
| 68 | if (severity >= 0 && severity < LOG_NUM_SEVERITIES) |
| 69 | return log_severity_names[severity]; |
| 70 | return "UNKNOWN"; |
| 71 | } |
| 72 | |
| 73 | int g_min_log_level = 0; |
| 74 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 75 | // For LOG_ERROR and above, always print to stderr. |
| 76 | const int kAlwaysPrintErrorLevel = LOG_ERROR; |
| 77 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 78 | } // namespace |
| 79 | |
| 80 | #if DCHECK_IS_CONFIGURABLE |
| 81 | // In DCHECK-enabled Chrome builds, allow the meaning of LOG_DCHECK to be |
| 82 | // determined at run-time. We default it to INFO, to avoid it triggering |
| 83 | // crashes before the run-time has explicitly chosen the behaviour. |
Scott Graham | 4459807 | 2018-06-14 22:01:37 -0700 | [diff] [blame] | 84 | logging::LogSeverity LOG_DCHECK = LOG_INFO; |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 85 | #endif // DCHECK_IS_CONFIGURABLE |
| 86 | |
| 87 | // This is never instantiated, it's just used for EAT_STREAM_PARAMETERS to have |
| 88 | // an object of the correct type on the LHS of the unused part of the ternary |
| 89 | // operator. |
| 90 | std::ostream* g_swallow_stream; |
| 91 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 92 | void SetMinLogLevel(int level) { |
| 93 | g_min_log_level = std::min(LOG_FATAL, level); |
| 94 | } |
| 95 | |
| 96 | int GetMinLogLevel() { |
| 97 | return g_min_log_level; |
| 98 | } |
| 99 | |
| 100 | bool ShouldCreateLogMessage(int severity) { |
| 101 | if (severity < g_min_log_level) |
| 102 | return false; |
| 103 | |
| 104 | // Return true here unless we know ~LogMessage won't do anything. Note that |
| 105 | // ~LogMessage writes to stderr if severity_ >= kAlwaysPrintErrorLevel, even |
| 106 | // when g_logging_destination is LOG_NONE. |
Scott Graham | 76a8dc7 | 2018-06-18 13:37:29 -0700 | [diff] [blame] | 107 | return severity >= kAlwaysPrintErrorLevel; |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Explicit instantiations for commonly used comparisons. |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 111 | template std::string* MakeCheckOpString<int, int>(const int&, |
| 112 | const int&, |
| 113 | const char* names); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 114 | template std::string* MakeCheckOpString<unsigned long, unsigned long>( |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 115 | const unsigned long&, |
| 116 | const unsigned long&, |
| 117 | const char* names); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 118 | template std::string* MakeCheckOpString<unsigned long, unsigned int>( |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 119 | const unsigned long&, |
| 120 | const unsigned int&, |
| 121 | const char* names); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 122 | template std::string* MakeCheckOpString<unsigned int, unsigned long>( |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 123 | const unsigned int&, |
| 124 | const unsigned long&, |
| 125 | const char* names); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 126 | template std::string* MakeCheckOpString<std::string, std::string>( |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 127 | const std::string&, |
| 128 | const std::string&, |
| 129 | const char* name); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 130 | |
| 131 | void MakeCheckOpValueString(std::ostream* os, std::nullptr_t p) { |
| 132 | (*os) << "nullptr"; |
| 133 | } |
| 134 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 135 | #if defined(OS_WIN) |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 136 | LogMessage::SaveLastError::SaveLastError() : last_error_(::GetLastError()) {} |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 137 | |
| 138 | LogMessage::SaveLastError::~SaveLastError() { |
| 139 | ::SetLastError(last_error_); |
| 140 | } |
| 141 | #endif // defined(OS_WIN) |
| 142 | |
| 143 | LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
| 144 | : severity_(severity), file_(file), line_(line) { |
| 145 | Init(file, line); |
| 146 | } |
| 147 | |
| 148 | LogMessage::LogMessage(const char* file, int line, const char* condition) |
| 149 | : severity_(LOG_FATAL), file_(file), line_(line) { |
| 150 | Init(file, line); |
| 151 | stream_ << "Check failed: " << condition << ". "; |
| 152 | } |
| 153 | |
| 154 | LogMessage::LogMessage(const char* file, int line, std::string* result) |
| 155 | : severity_(LOG_FATAL), file_(file), line_(line) { |
| 156 | Init(file, line); |
| 157 | stream_ << "Check failed: " << *result; |
| 158 | delete result; |
| 159 | } |
| 160 | |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 161 | LogMessage::LogMessage(const char* file, |
| 162 | int line, |
| 163 | LogSeverity severity, |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 164 | std::string* result) |
| 165 | : severity_(severity), file_(file), line_(line) { |
| 166 | Init(file, line); |
| 167 | stream_ << "Check failed: " << *result; |
| 168 | delete result; |
| 169 | } |
| 170 | |
| 171 | LogMessage::~LogMessage() { |
Scott Graham | 7e8fdb3 | 2018-06-14 11:22:06 -0700 | [diff] [blame] | 172 | if (severity_ == LOG_FATAL) { |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 173 | stream_ << std::endl; // Newline to separate from log message. |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 174 | } |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 175 | stream_ << std::endl; |
| 176 | std::string str_newline(stream_.str()); |
| 177 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 178 | #if defined(OS_WIN) |
Scott Graham | 76a8dc7 | 2018-06-18 13:37:29 -0700 | [diff] [blame] | 179 | OutputDebugStringA(str_newline.c_str()); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 180 | #endif |
Scott Graham | 76a8dc7 | 2018-06-18 13:37:29 -0700 | [diff] [blame] | 181 | ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr)); |
| 182 | fflush(stderr); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 183 | |
| 184 | if (severity_ == LOG_FATAL) { |
Scott Graham | 894986a | 2018-06-14 14:15:50 -0700 | [diff] [blame] | 185 | abort(); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | // writes the common header info to the stream |
| 190 | void LogMessage::Init(const char* file, int line) { |
| 191 | base::StringPiece filename(file); |
| 192 | size_t last_slash_pos = filename.find_last_of("\\/"); |
| 193 | if (last_slash_pos != base::StringPiece::npos) |
| 194 | filename.remove_prefix(last_slash_pos + 1); |
| 195 | |
| 196 | // TODO(darin): It might be nice if the columns were fixed width. |
| 197 | |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 198 | stream_ << '['; |
Scott Graham | ce047c9 | 2018-06-19 15:56:56 -0700 | [diff] [blame] | 199 | stream_ << std::this_thread::get_id() << ':'; |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 200 | #if defined(OS_WIN) |
Scott Graham | 76a8dc7 | 2018-06-18 13:37:29 -0700 | [diff] [blame] | 201 | SYSTEMTIME local_time; |
| 202 | GetLocalTime(&local_time); |
| 203 | stream_ << std::setfill('0') << std::setw(2) << local_time.wMonth |
| 204 | << std::setw(2) << local_time.wDay << '/' << std::setw(2) |
| 205 | << local_time.wHour << std::setw(2) << local_time.wMinute |
| 206 | << std::setw(2) << local_time.wSecond << '.' << std::setw(3) |
| 207 | << local_time.wMilliseconds << ':'; |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 208 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
Scott Graham | 76a8dc7 | 2018-06-18 13:37:29 -0700 | [diff] [blame] | 209 | timeval tv; |
| 210 | gettimeofday(&tv, nullptr); |
| 211 | time_t t = tv.tv_sec; |
| 212 | struct tm local_time; |
| 213 | localtime_r(&t, &local_time); |
| 214 | struct tm* tm_time = &local_time; |
| 215 | stream_ << std::setfill('0') << std::setw(2) << 1 + tm_time->tm_mon |
| 216 | << std::setw(2) << tm_time->tm_mday << '/' << std::setw(2) |
| 217 | << tm_time->tm_hour << std::setw(2) << tm_time->tm_min << std::setw(2) |
| 218 | << tm_time->tm_sec << '.' << std::setw(6) << tv.tv_usec << ':'; |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 219 | #else |
| 220 | #error Unsupported platform |
| 221 | #endif |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 222 | if (severity_ >= 0) |
| 223 | stream_ << log_severity_name(severity_); |
| 224 | else |
| 225 | stream_ << "VERBOSE" << -severity_; |
| 226 | |
| 227 | stream_ << ":" << filename << "(" << line << ")] "; |
| 228 | |
| 229 | message_start_ = stream_.str().length(); |
| 230 | } |
| 231 | |
| 232 | #if defined(OS_WIN) |
| 233 | // This has already been defined in the header, but defining it again as DWORD |
| 234 | // ensures that the type used in the header is equivalent to DWORD. If not, |
| 235 | // the redefinition is a compile error. |
| 236 | typedef DWORD SystemErrorCode; |
| 237 | #endif |
| 238 | |
| 239 | SystemErrorCode GetLastSystemErrorCode() { |
| 240 | #if defined(OS_WIN) |
| 241 | return ::GetLastError(); |
| 242 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 243 | return errno; |
| 244 | #endif |
| 245 | } |
| 246 | |
Scott Graham | 4459807 | 2018-06-14 22:01:37 -0700 | [diff] [blame] | 247 | std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 248 | #if defined(OS_WIN) |
| 249 | const int kErrorMessageBufferSize = 256; |
| 250 | char msgbuf[kErrorMessageBufferSize]; |
| 251 | DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; |
| 252 | DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf, |
| 253 | arraysize(msgbuf), nullptr); |
| 254 | if (len) { |
| 255 | // Messages returned by system end with line breaks. |
| 256 | return base::CollapseWhitespaceASCII(msgbuf, true) + |
| 257 | base::StringPrintf(" (0x%lX)", error_code); |
| 258 | } |
| 259 | return base::StringPrintf("Error (0x%lX) while retrieving error. (0x%lX)", |
| 260 | GetLastError(), error_code); |
| 261 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 262 | return base::safe_strerror(error_code) + |
| 263 | base::StringPrintf(" (%d)", error_code); |
| 264 | #endif // defined(OS_WIN) |
| 265 | } |
| 266 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 267 | #if defined(OS_WIN) |
| 268 | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
| 269 | int line, |
| 270 | LogSeverity severity, |
| 271 | SystemErrorCode err) |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 272 | : err_(err), log_message_(file, line, severity) {} |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 273 | |
| 274 | Win32ErrorLogMessage::~Win32ErrorLogMessage() { |
| 275 | stream() << ": " << SystemErrorCodeToString(err_); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 276 | } |
| 277 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
| 278 | ErrnoLogMessage::ErrnoLogMessage(const char* file, |
| 279 | int line, |
| 280 | LogSeverity severity, |
| 281 | SystemErrorCode err) |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 282 | : err_(err), log_message_(file, line, severity) {} |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 283 | |
| 284 | ErrnoLogMessage::~ErrnoLogMessage() { |
| 285 | stream() << ": " << SystemErrorCodeToString(err_); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 286 | } |
| 287 | #endif // defined(OS_WIN) |
| 288 | |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 289 | void RawLog(int level, const char* message) { |
| 290 | if (level >= g_min_log_level && message) { |
| 291 | size_t bytes_written = 0; |
| 292 | const size_t message_len = strlen(message); |
| 293 | int rv; |
| 294 | while (bytes_written < message_len) { |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 295 | rv = HANDLE_EINTR(write(STDERR_FILENO, message + bytes_written, |
| 296 | message_len - bytes_written)); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 297 | if (rv < 0) { |
| 298 | // Give up, nothing we can do now. |
| 299 | break; |
| 300 | } |
| 301 | bytes_written += rv; |
| 302 | } |
| 303 | |
| 304 | if (message_len > 0 && message[message_len - 1] != '\n') { |
| 305 | do { |
| 306 | rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1)); |
| 307 | if (rv < 0) { |
| 308 | // Give up, nothing we can do now. |
| 309 | break; |
| 310 | } |
| 311 | } while (rv != 1); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if (level == LOG_FATAL) |
Scott Graham | 7e8fdb3 | 2018-06-14 11:22:06 -0700 | [diff] [blame] | 316 | abort(); |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // This was defined at the beginning of this file. |
| 320 | #undef write |
| 321 | |
Scott Graham | 4459807 | 2018-06-14 22:01:37 -0700 | [diff] [blame] | 322 | void LogErrorNotReached(const char* file, int line) { |
Scott Graham | 98cd3ca | 2018-06-14 22:26:55 -0700 | [diff] [blame] | 323 | LogMessage(file, line, LOG_ERROR).stream() << "NOTREACHED() hit."; |
Scott Graham | 6696211 | 2018-06-08 12:42:08 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | } // namespace logging |
| 327 | |
| 328 | std::ostream& std::operator<<(std::ostream& out, const wchar_t* wstr) { |
| 329 | return out << (wstr ? base::WideToUTF8(wstr) : std::string()); |
| 330 | } |