Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(475)

Unified Diff: base/logging.cc

Issue 16519003: Define a LoggingSettings struct to use for InitLogging() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« base/logging.h ('K') | « base/logging.h ('k') | base/test/test_suite.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/logging.cc
diff --git a/base/logging.cc b/base/logging.cc
index 49623bf555abfa0d7f09874f9b821bfa1e3fbd1b..534a897c11256e34e357a8f125de367f18916fd9 100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -80,20 +80,12 @@ namespace {
VlogInfo* g_vlog_info = NULL;
VlogInfo* g_vlog_info_prev = NULL;
-const char* const log_severity_names[LOG_NUM_SEVERITIES] = {
+const char* const kLogSeverityNames[LOG_NUM_SEVERITIES] = {
"INFO", "WARNING", "ERROR", "ERROR_REPORT", "FATAL" };
-int min_log_level = 0;
+int g_min_log_level = 0;
brettw 2013/06/19 18:10:13 This stuff is in an anonymous namespace in a .cc f
akalin 2013/06/19 19:59:18 Done. There were already some existing g_ variable
-// The default set here for logging_destination will only be used if
-// InitLogging is not called. On Windows, use a file next to the exe;
-// on POSIX platforms, where it may not even be possible to locate the
-// executable on disk, use stderr.
-#if defined(OS_WIN)
-LoggingDestination logging_destination = LOG_ONLY_TO_FILE;
-#elif defined(OS_POSIX)
-LoggingDestination logging_destination = LOG_ONLY_TO_SYSTEM_DEBUG_LOG;
-#endif
+LoggingDestination g_logging_destination = LOG_DEFAULT;
// For LOG_ERROR and above, always print to stderr.
const int kAlwaysPrintErrorLevel = LOG_ERROR;
@@ -106,28 +98,28 @@ typedef std::wstring PathString;
#else
typedef std::string PathString;
#endif
-PathString* log_file_name = NULL;
+PathString* g_log_file_name = NULL;
// this file is lazily opened and the handle may be NULL
-FileHandle log_file = NULL;
+FileHandle g_log_file = NULL;
// what should be prepended to each message?
-bool log_process_id = false;
-bool log_thread_id = false;
-bool log_timestamp = true;
-bool log_tickcount = false;
+bool g_log_process_id = false;
+bool g_log_thread_id = false;
+bool g_log_timestamp = true;
+bool g_log_tickcount = false;
// Should we pop up fatal debug messages in a dialog?
-bool show_error_dialogs = false;
+bool g_show_error_dialogs = false;
// An assert handler override specified by the client to be called instead of
// the debug message dialog and process termination.
-LogAssertHandlerFunction log_assert_handler = NULL;
+LogAssertHandlerFunction g_log_assert_handler = NULL;
// An report handler override specified by the client to be called instead of
// the debug message dialog.
-LogReportHandlerFunction log_report_handler = NULL;
+LogReportHandlerFunction g_log_report_handler = NULL;
// A log message handler that gets notified of every log message we process.
-LogMessageHandlerFunction log_message_handler = NULL;
+LogMessageHandlerFunction g_log_message_handler = NULL;
// Helper functions to wrap platform differences.
@@ -184,13 +176,13 @@ PathString GetDefaultLogFile() {
wchar_t module_name[MAX_PATH];
GetModuleFileName(NULL, module_name, MAX_PATH);
- PathString log_file = module_name;
+ PathString g_log_file = module_name;
PathString::size_type last_backslash =
- log_file.rfind('\\', log_file.size());
+ g_log_file.rfind('\\', g_log_file.size());
if (last_backslash != PathString::npos)
- log_file.erase(last_backslash + 1);
- log_file += L"debug.log";
- return log_file;
+ g_log_file.erase(last_backslash + 1);
+ g_log_file += L"debug.log";
+ return g_log_file;
#elif defined(OS_POSIX)
// On other platforms we just use the current directory.
return PathString("debug.log");
@@ -314,35 +306,34 @@ pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER;
// and can be used for writing. Returns false if the file could not be
// initialized. debug_file will be NULL in this case.
bool InitializeLogFileHandle() {
- if (log_file)
+ if (g_log_file)
return true;
- if (!log_file_name) {
+ if (!g_log_file_name) {
// Nobody has called InitLogging to specify a debug log file, so here we
// initialize the log file name to a default.
- log_file_name = new PathString(GetDefaultLogFile());
+ g_log_file_name = new PathString(GetDefaultLogFile());
}
- if (logging_destination == LOG_ONLY_TO_FILE ||
- logging_destination == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) {
+ if ((g_logging_destination & LOG_TO_FILE) != 0) {
#if defined(OS_WIN)
- log_file = CreateFile(log_file_name->c_str(), GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
- OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) {
- // try the current directory
- log_file = CreateFile(L".\\debug.log", GENERIC_WRITE,
+ g_log_file = CreateFile(g_log_file_name->c_str(), GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
- if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) {
- log_file = NULL;
+ if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == NULL) {
+ // try the current directory
+ g_log_file = CreateFile(L".\\debug.log", GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
+ OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+ if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == NULL) {
+ g_log_file = NULL;
return false;
}
}
- SetFilePointer(log_file, 0, 0, FILE_END);
+ SetFilePointer(g_log_file, 0, 0, FILE_END);
#elif defined(OS_POSIX)
- log_file = fopen(log_file_name->c_str(), "a");
- if (log_file == NULL)
+ g_log_file = fopen(g_log_file_name->c_str(), "a");
+ if (g_log_file == NULL)
return false;
#endif
}
@@ -352,17 +343,19 @@ bool InitializeLogFileHandle() {
} // namespace
+LoggingSettings::LoggingSettings()
+ : logging_dest(LOG_DEFAULT),
+ log_file(NULL),
+ lock_log(LOCK_LOG_FILE),
+ delete_old(APPEND_TO_OLD_LOG_FILE),
+ dcheck_state(DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS) {}
-bool BaseInitLoggingImpl(const PathChar* new_log_file,
- LoggingDestination logging_dest,
- LogLockingState lock_log,
- OldFileDeletionState delete_old,
- DcheckState dcheck_state) {
+bool BaseInitLoggingImpl(const LoggingSettings& settings) {
#if defined(OS_NACL)
- CHECK(logging_dest == LOG_NONE ||
- logging_dest == LOG_ONLY_TO_SYSTEM_DEBUG_LOG);
+ // Can log only to the system debug log.
+ CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
#endif
- g_dcheck_state = dcheck_state;
+ g_dcheck_state = settings.dcheck_state;
CommandLine* command_line = CommandLine::ForCurrentProcess();
// Don't bother initializing g_vlog_info unless we use one of the
// vlog switches.
@@ -377,42 +370,41 @@ bool BaseInitLoggingImpl(const PathChar* new_log_file,
g_vlog_info =
new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
command_line->GetSwitchValueASCII(switches::kVModule),
- &min_log_level);
+ &g_min_log_level);
}
- LoggingLock::Init(lock_log, new_log_file);
+ LoggingLock::Init(settings.lock_log, settings.log_file);
LoggingLock logging_lock;
- if (log_file) {
+ if (g_log_file) {
// calling InitLogging twice or after some log call has already opened the
// default log file will re-initialize to the new options
- CloseFile(log_file);
- log_file = NULL;
+ CloseFile(g_log_file);
+ g_log_file = NULL;
}
- logging_destination = logging_dest;
+ g_logging_destination = settings.logging_dest;
- // ignore file options if logging is disabled or only to system
- if (logging_destination == LOG_NONE ||
- logging_destination == LOG_ONLY_TO_SYSTEM_DEBUG_LOG)
+ // ignore file options unless logging to file is set.
+ if ((g_logging_destination & LOG_TO_FILE) == 0)
return true;
- if (!log_file_name)
- log_file_name = new PathString();
- *log_file_name = new_log_file;
- if (delete_old == DELETE_OLD_LOG_FILE)
- DeleteFilePath(*log_file_name);
+ if (!g_log_file_name)
+ g_log_file_name = new PathString();
+ *g_log_file_name = settings.log_file;
+ if (settings.delete_old == DELETE_OLD_LOG_FILE)
+ DeleteFilePath(*g_log_file_name);
return InitializeLogFileHandle();
}
void SetMinLogLevel(int level) {
- min_log_level = std::min(LOG_ERROR_REPORT, level);
+ g_min_log_level = std::min(LOG_ERROR_REPORT, level);
}
int GetMinLogLevel() {
- return min_log_level;
+ return g_min_log_level;
}
int GetVlogVerbosity() {
@@ -431,30 +423,30 @@ int GetVlogLevelHelper(const char* file, size_t N) {
void SetLogItems(bool enable_process_id, bool enable_thread_id,
bool enable_timestamp, bool enable_tickcount) {
- log_process_id = enable_process_id;
- log_thread_id = enable_thread_id;
- log_timestamp = enable_timestamp;
- log_tickcount = enable_tickcount;
+ g_log_process_id = enable_process_id;
+ g_log_thread_id = enable_thread_id;
+ g_log_timestamp = enable_timestamp;
+ g_log_tickcount = enable_tickcount;
}
void SetShowErrorDialogs(bool enable_dialogs) {
- show_error_dialogs = enable_dialogs;
+ g_show_error_dialogs = enable_dialogs;
}
void SetLogAssertHandler(LogAssertHandlerFunction handler) {
- log_assert_handler = handler;
+ g_log_assert_handler = handler;
}
void SetLogReportHandler(LogReportHandlerFunction handler) {
- log_report_handler = handler;
+ g_log_report_handler = handler;
}
void SetLogMessageHandler(LogMessageHandlerFunction handler) {
- log_message_handler = handler;
+ g_log_message_handler = handler;
}
LogMessageHandlerFunction GetLogMessageHandler() {
- return log_message_handler;
+ return g_log_message_handler;
}
// MSVC doesn't like complex extern templates and DLLs.
@@ -481,7 +473,7 @@ void DisplayDebugMessageInDialog(const std::string& str) {
if (str.empty())
return;
- if (!show_error_dialogs)
+ if (!g_show_error_dialogs)
return;
#if defined(OS_WIN)
@@ -578,14 +570,14 @@ LogMessage::~LogMessage() {
std::string str_newline(stream_.str());
// Give any log message handler first dibs on the message.
- if (log_message_handler && log_message_handler(severity_, file_, line_,
- message_start_, str_newline)) {
+ if (g_log_message_handler &&
+ g_log_message_handler(severity_, file_, line_,
+ message_start_, str_newline)) {
// The handler took care of it, no further processing.
return;
}
- if (logging_destination == LOG_ONLY_TO_SYSTEM_DEBUG_LOG ||
- logging_destination == LOG_TO_BOTH_FILE_AND_SYSTEM_DEBUG_LOG) {
+ if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
#if defined(OS_WIN)
OutputDebugStringA(str_newline.c_str());
#elif defined(OS_ANDROID)
@@ -627,21 +619,20 @@ LogMessage::~LogMessage() {
// thread at the beginning of execution.
LoggingLock::Init(LOCK_LOG_FILE, NULL);
// write to log file
- if (logging_destination != LOG_NONE &&
- logging_destination != LOG_ONLY_TO_SYSTEM_DEBUG_LOG) {
+ if ((g_logging_destination & LOG_TO_FILE) != 0) {
LoggingLock logging_lock;
if (InitializeLogFileHandle()) {
#if defined(OS_WIN)
- SetFilePointer(log_file, 0, 0, SEEK_END);
+ SetFilePointer(g_log_file, 0, 0, SEEK_END);
DWORD num_written;
- WriteFile(log_file,
+ WriteFile(g_log_file,
static_cast<const void*>(str_newline.c_str()),
static_cast<DWORD>(str_newline.length()),
&num_written,
NULL);
#else
- fprintf(log_file, "%s", str_newline.c_str());
- fflush(log_file);
+ fprintf(g_log_file, "%s", str_newline.c_str());
+ fflush(g_log_file);
#endif
}
}
@@ -657,9 +648,9 @@ LogMessage::~LogMessage() {
if (base::debug::BeingDebugged()) {
base::debug::BreakDebugger();
} else {
- if (log_assert_handler) {
+ if (g_log_assert_handler) {
// make a copy of the string for the handler out of paranoia
- log_assert_handler(std::string(stream_.str()));
+ g_log_assert_handler(std::string(stream_.str()));
} else {
// Don't use the string with the newline, get a fresh version to send to
// the debug message process. We also don't display assertions to the
@@ -675,8 +666,8 @@ LogMessage::~LogMessage() {
}
} else if (severity_ == LOG_ERROR_REPORT) {
// We are here only if the user runs with --enable-dcheck in release mode.
- if (log_report_handler) {
- log_report_handler(std::string(stream_.str()));
+ if (g_log_report_handler) {
+ g_log_report_handler(std::string(stream_.str()));
} else {
DisplayDebugMessageInDialog(stream_.str());
}
@@ -693,11 +684,11 @@ void LogMessage::Init(const char* file, int line) {
// TODO(darin): It might be nice if the columns were fixed width.
stream_ << '[';
- if (log_process_id)
+ if (g_log_process_id)
stream_ << CurrentProcessId() << ':';
- if (log_thread_id)
+ if (g_log_thread_id)
stream_ << base::PlatformThread::CurrentId() << ':';
- if (log_timestamp) {
+ if (g_log_timestamp) {
time_t t = time(NULL);
struct tm local_time = {0};
#if _MSC_VER >= 1400
@@ -715,10 +706,10 @@ void LogMessage::Init(const char* file, int line) {
<< std::setw(2) << tm_time->tm_sec
<< ':';
}
- if (log_tickcount)
+ if (g_log_tickcount)
stream_ << TickCount() << ':';
if (severity_ >= 0)
- stream_ << log_severity_names[severity_];
+ stream_ << kLogSeverityNames[severity_];
else
stream_ << "VERBOSE" << -severity_;
@@ -822,15 +813,15 @@ ErrnoLogMessage::~ErrnoLogMessage() {
void CloseLogFile() {
LoggingLock logging_lock;
- if (!log_file)
+ if (!g_log_file)
return;
- CloseFile(log_file);
- log_file = NULL;
+ CloseFile(g_log_file);
+ g_log_file = NULL;
}
void RawLog(int level, const char* message) {
- if (level >= min_log_level) {
+ if (level >= g_min_log_level) {
size_t bytes_written = 0;
const size_t message_len = strlen(message);
int rv;
@@ -865,8 +856,8 @@ void RawLog(int level, const char* message) {
#if defined(OS_WIN)
std::wstring GetLogFileFullPath() {
- if (log_file_name)
- return *log_file_name;
+ if (g_log_file_name)
+ return *g_log_file_name;
return std::wstring();
}
#endif
« base/logging.h ('K') | « base/logging.h ('k') | base/test/test_suite.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698