blob: 1fd047f9b848021c2de43ae6f3e263fe71403269 [file] [log] [blame]
Vitaly Bukacbed2062015-08-17 12:54:05 -07001// 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
Vitaly Bukacbed2062015-08-17 12:54:05 -07007#include <sys/syscall.h>
Vitaly Bukacbed2062015-08-17 12:54:05 -07008#include <time.h>
Vitaly Bukacbed2062015-08-17 12:54:05 -07009#include <errno.h>
10#include <pthread.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <unistd.h>
Vitaly Bukacbed2062015-08-17 12:54:05 -070015
16#include <algorithm>
17#include <cstring>
18#include <ctime>
19#include <iomanip>
20#include <ostream>
21#include <string>
22
Vitaly Bukacbed2062015-08-17 12:54:05 -070023#include "base/posix/eintr_wrapper.h"
24#include "base/strings/string_piece.h"
25#include "base/strings/string_util.h"
26#include "base/strings/stringprintf.h"
Vitaly Buka8750b272015-08-18 18:39:08 -070027#include "base/strings/utf_string_conversion_utils.h"
Vitaly Bukacbed2062015-08-17 12:54:05 -070028
29namespace logging {
30
31namespace {
32
Vitaly Bukacbed2062015-08-17 12:54:05 -070033const char* const log_severity_names[LOG_NUM_SEVERITIES] = {
34 "INFO", "WARNING", "ERROR", "FATAL" };
35
36const char* log_severity_name(int severity) {
37 if (severity >= 0 && severity < LOG_NUM_SEVERITIES)
38 return log_severity_names[severity];
39 return "UNKNOWN";
40}
41
42int g_min_log_level = 0;
43
44LoggingDestination g_logging_destination = LOG_DEFAULT;
45
46// For LOG_ERROR and above, always print to stderr.
47const int kAlwaysPrintErrorLevel = LOG_ERROR;
48
Vitaly Bukacbed2062015-08-17 12:54:05 -070049// What should be prepended to each message?
Vitaly Bukacbed2062015-08-17 12:54:05 -070050bool g_log_timestamp = true;
Vitaly Bukacbed2062015-08-17 12:54:05 -070051
52// Should we pop up fatal debug messages in a dialog?
53bool show_error_dialogs = false;
54
55// An assert handler override specified by the client to be called instead of
56// the debug message dialog and process termination.
57LogAssertHandlerFunction log_assert_handler = nullptr;
58// A log message handler that gets notified of every log message we process.
59LogMessageHandlerFunction log_message_handler = nullptr;
60
61// Helper functions to wrap platform differences.
62
Vitaly Bukacbed2062015-08-17 12:54:05 -070063} // namespace
64
65LoggingSettings::LoggingSettings()
Vitaly Buka8750b272015-08-18 18:39:08 -070066 : logging_dest(LOG_DEFAULT) {}
Vitaly Bukacbed2062015-08-17 12:54:05 -070067
68bool BaseInitLoggingImpl(const LoggingSettings& settings) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070069 g_logging_destination = settings.logging_dest;
70
Vitaly Buka8750b272015-08-18 18:39:08 -070071 return true;
Vitaly Bukacbed2062015-08-17 12:54:05 -070072}
73
74void SetMinLogLevel(int level) {
75 g_min_log_level = std::min(LOG_FATAL, level);
76}
77
78int GetMinLogLevel() {
79 return g_min_log_level;
80}
81
82int GetVlogVerbosity() {
83 return std::max(-1, LOG_INFO - GetMinLogLevel());
84}
85
Vitaly Bukacbed2062015-08-17 12:54:05 -070086void SetLogItems(bool enable_process_id, bool enable_thread_id,
87 bool enable_timestamp, bool enable_tickcount) {
Vitaly Bukacbed2062015-08-17 12:54:05 -070088 g_log_timestamp = enable_timestamp;
Vitaly Bukacbed2062015-08-17 12:54:05 -070089}
90
91void SetShowErrorDialogs(bool enable_dialogs) {
92 show_error_dialogs = enable_dialogs;
93}
94
95void SetLogAssertHandler(LogAssertHandlerFunction handler) {
96 log_assert_handler = handler;
97}
98
99void SetLogMessageHandler(LogMessageHandlerFunction handler) {
100 log_message_handler = handler;
101}
102
103LogMessageHandlerFunction GetLogMessageHandler() {
104 return log_message_handler;
105}
106
107// Explicit instantiations for commonly used comparisons.
108template std::string* MakeCheckOpString<int, int>(
109 const int&, const int&, const char* names);
110template std::string* MakeCheckOpString<unsigned long, unsigned long>(
111 const unsigned long&, const unsigned long&, const char* names);
112template std::string* MakeCheckOpString<unsigned long, unsigned int>(
113 const unsigned long&, const unsigned int&, const char* names);
114template std::string* MakeCheckOpString<unsigned int, unsigned long>(
115 const unsigned int&, const unsigned long&, const char* names);
116template std::string* MakeCheckOpString<std::string, std::string>(
117 const std::string&, const std::string&, const char* name);
118
Vitaly Bukacbed2062015-08-17 12:54:05 -0700119LogMessage::LogMessage(const char* file, int line, LogSeverity severity)
120 : severity_(severity), file_(file), line_(line) {
121 Init(file, line);
122}
123
124LogMessage::LogMessage(const char* file, int line, std::string* result)
125 : severity_(LOG_FATAL), file_(file), line_(line) {
126 Init(file, line);
127 stream_ << "Check failed: " << *result;
128 delete result;
129}
130
131LogMessage::LogMessage(const char* file, int line, LogSeverity severity,
132 std::string* result)
133 : severity_(severity), file_(file), line_(line) {
134 Init(file, line);
135 stream_ << "Check failed: " << *result;
136 delete result;
137}
138
139LogMessage::~LogMessage() {
Vitaly Bukacbed2062015-08-17 12:54:05 -0700140 stream_ << std::endl;
141 std::string str_newline(stream_.str());
142
143 // Give any log message handler first dibs on the message.
144 if (log_message_handler &&
145 log_message_handler(severity_, file_, line_,
146 message_start_, str_newline)) {
147 // The handler took care of it, no further processing.
148 return;
149 }
150
151 if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
Vitaly Bukacbed2062015-08-17 12:54:05 -0700152 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
153 fflush(stderr);
154 } else if (severity_ >= kAlwaysPrintErrorLevel) {
155 // When we're only outputting to a log file, above a certain log level, we
156 // should still output to stderr so that we can better detect and diagnose
157 // problems with unit tests, especially on the buildbots.
158 ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));
159 fflush(stderr);
160 }
161
Vitaly Bukacbed2062015-08-17 12:54:05 -0700162 if (severity_ == LOG_FATAL) {
163 // Ensure the first characters of the string are on the stack so they
164 // are contained in minidumps for diagnostic purposes.
165 char str_stack[1024];
166 str_newline.copy(str_stack, arraysize(str_stack));
Vitaly Bukacbed2062015-08-17 12:54:05 -0700167
168 if (log_assert_handler) {
169 // Make a copy of the string for the handler out of paranoia.
170 log_assert_handler(std::string(stream_.str()));
171 } else {
Vitaly Bukacbed2062015-08-17 12:54:05 -0700172 // Crash the process to generate a dump.
Vitaly Buka8750b272015-08-18 18:39:08 -0700173 abort();
Vitaly Bukacbed2062015-08-17 12:54:05 -0700174 }
175 }
176}
177
178// writes the common header info to the stream
179void LogMessage::Init(const char* file, int line) {
180 base::StringPiece filename(file);
181 size_t last_slash_pos = filename.find_last_of("\\/");
182 if (last_slash_pos != base::StringPiece::npos)
183 filename.remove_prefix(last_slash_pos + 1);
184
185 // TODO(darin): It might be nice if the columns were fixed width.
186
187 stream_ << '[';
Vitaly Bukacbed2062015-08-17 12:54:05 -0700188 if (g_log_timestamp) {
189 time_t t = time(nullptr);
190 struct tm local_time = {0};
191#ifdef _MSC_VER
192 localtime_s(&local_time, &t);
193#else
194 localtime_r(&t, &local_time);
195#endif
196 struct tm* tm_time = &local_time;
197 stream_ << std::setfill('0')
198 << std::setw(2) << 1 + tm_time->tm_mon
199 << std::setw(2) << tm_time->tm_mday
200 << '/'
201 << std::setw(2) << tm_time->tm_hour
202 << std::setw(2) << tm_time->tm_min
203 << std::setw(2) << tm_time->tm_sec
204 << ':';
205 }
Vitaly Bukacbed2062015-08-17 12:54:05 -0700206 if (severity_ >= 0)
207 stream_ << log_severity_name(severity_);
208 else
209 stream_ << "VERBOSE" << -severity_;
210
211 stream_ << ":" << filename << "(" << line << ")] ";
212
213 message_start_ = stream_.str().length();
214}
215
Vitaly Bukacbed2062015-08-17 12:54:05 -0700216void RawLog(int level, const char* message) {
217 if (level >= g_min_log_level) {
218 size_t bytes_written = 0;
219 const size_t message_len = strlen(message);
220 int rv;
221 while (bytes_written < message_len) {
222 rv = HANDLE_EINTR(
223 write(STDERR_FILENO, message + bytes_written,
224 message_len - bytes_written));
225 if (rv < 0) {
226 // Give up, nothing we can do now.
227 break;
228 }
229 bytes_written += rv;
230 }
231
232 if (message_len > 0 && message[message_len - 1] != '\n') {
233 do {
234 rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1));
235 if (rv < 0) {
236 // Give up, nothing we can do now.
237 break;
238 }
239 } while (rv != 1);
240 }
241 }
242
243 if (level == LOG_FATAL)
Vitaly Buka8750b272015-08-18 18:39:08 -0700244 abort();
Vitaly Bukacbed2062015-08-17 12:54:05 -0700245}
246
247// This was defined at the beginning of this file.
248#undef write
249
Vitaly Buka60b8f002015-08-20 13:47:48 -0700250void LogErrorNotReached(const char* file, int line) {
Vitaly Bukacbed2062015-08-17 12:54:05 -0700251 LogMessage(file, line, LOG_ERROR).stream()
252 << "NOTREACHED() hit.";
253}
254
255} // namespace logging