Commit 3e4787d6 authored by thestig's avatar thestig Committed by Commit bot

Cleanup base/logging.cc a bit.

- NULL -> nullptr
- prepend g_ to global variables
- fix misc nits.

Review URL: https://codereview.chromium.org/1134153003

Cr-Commit-Position: refs/heads/master@{#330582}
parent eb8ea8d2
...@@ -70,8 +70,8 @@ namespace logging { ...@@ -70,8 +70,8 @@ namespace logging {
namespace { namespace {
VlogInfo* g_vlog_info = NULL; VlogInfo* g_vlog_info = nullptr;
VlogInfo* g_vlog_info_prev = NULL; VlogInfo* g_vlog_info_prev = nullptr;
const char* const log_severity_names[LOG_NUM_SEVERITIES] = { const char* const log_severity_names[LOG_NUM_SEVERITIES] = {
"INFO", "WARNING", "ERROR", "FATAL" }; "INFO", "WARNING", "ERROR", "FATAL" };
...@@ -82,9 +82,9 @@ const char* log_severity_name(int severity) { ...@@ -82,9 +82,9 @@ const char* log_severity_name(int severity) {
return "UNKNOWN"; return "UNKNOWN";
} }
int min_log_level = 0; int g_min_log_level = 0;
LoggingDestination logging_destination = LOG_DEFAULT; LoggingDestination g_logging_destination = LOG_DEFAULT;
// For LOG_ERROR and above, always print to stderr. // For LOG_ERROR and above, always print to stderr.
const int kAlwaysPrintErrorLevel = LOG_ERROR; const int kAlwaysPrintErrorLevel = LOG_ERROR;
...@@ -97,25 +97,25 @@ typedef std::wstring PathString; ...@@ -97,25 +97,25 @@ typedef std::wstring PathString;
#else #else
typedef std::string PathString; typedef std::string PathString;
#endif #endif
PathString* log_file_name = NULL; PathString* g_log_file_name = nullptr;
// this file is lazily opened and the handle may be NULL // This file is lazily opened and the handle may be nullptr
FileHandle log_file = NULL; FileHandle g_log_file = nullptr;
// what should be prepended to each message? // What should be prepended to each message?
bool log_process_id = false; bool g_log_process_id = false;
bool log_thread_id = false; bool g_log_thread_id = false;
bool log_timestamp = true; bool g_log_timestamp = true;
bool log_tickcount = false; bool g_log_tickcount = false;
// Should we pop up fatal debug messages in a dialog? // Should we pop up fatal debug messages in a dialog?
bool show_error_dialogs = false; bool show_error_dialogs = false;
// An assert handler override specified by the client to be called instead of // An assert handler override specified by the client to be called instead of
// the debug message dialog and process termination. // the debug message dialog and process termination.
LogAssertHandlerFunction log_assert_handler = NULL; LogAssertHandlerFunction log_assert_handler = nullptr;
// A log message handler that gets notified of every log message we process. // A log message handler that gets notified of every log message we process.
LogMessageHandlerFunction log_message_handler = NULL; LogMessageHandlerFunction log_message_handler = nullptr;
// Helper functions to wrap platform differences. // Helper functions to wrap platform differences.
...@@ -162,7 +162,7 @@ PathString GetDefaultLogFile() { ...@@ -162,7 +162,7 @@ PathString GetDefaultLogFile() {
#if defined(OS_WIN) #if defined(OS_WIN)
// On Windows we use the same path as the exe. // On Windows we use the same path as the exe.
wchar_t module_name[MAX_PATH]; wchar_t module_name[MAX_PATH];
GetModuleFileName(NULL, module_name, MAX_PATH); GetModuleFileName(nullptr, module_name, MAX_PATH);
PathString log_name = module_name; PathString log_name = module_name;
PathString::size_type last_backslash = log_name.rfind('\\', log_name.size()); PathString::size_type last_backslash = log_name.rfind('\\', log_name.size());
...@@ -208,9 +208,9 @@ class LoggingLock { ...@@ -208,9 +208,9 @@ class LoggingLock {
std::replace(safe_name.begin(), safe_name.end(), '\\', '/'); std::replace(safe_name.begin(), safe_name.end(), '\\', '/');
std::wstring t(L"Global\\"); std::wstring t(L"Global\\");
t.append(safe_name); t.append(safe_name);
log_mutex = ::CreateMutex(NULL, FALSE, t.c_str()); log_mutex = ::CreateMutex(nullptr, FALSE, t.c_str());
if (log_mutex == NULL) { if (log_mutex == nullptr) {
#if DEBUG #if DEBUG
// Keep the error code for debugging // Keep the error code for debugging
int error = GetLastError(); // NOLINT int error = GetLastError(); // NOLINT
...@@ -278,49 +278,49 @@ class LoggingLock { ...@@ -278,49 +278,49 @@ class LoggingLock {
// static // static
bool LoggingLock::initialized = false; bool LoggingLock::initialized = false;
// static // static
base::internal::LockImpl* LoggingLock::log_lock = NULL; base::internal::LockImpl* LoggingLock::log_lock = nullptr;
// static // static
LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE; LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE;
#if defined(OS_WIN) #if defined(OS_WIN)
// static // static
MutexHandle LoggingLock::log_mutex = NULL; MutexHandle LoggingLock::log_mutex = nullptr;
#elif defined(OS_POSIX) #elif defined(OS_POSIX)
pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif #endif
// Called by logging functions to ensure that debug_file is initialized // Called by logging functions to ensure that |g_log_file| is initialized
// and can be used for writing. Returns false if the file could not be // and can be used for writing. Returns false if the file could not be
// initialized. debug_file will be NULL in this case. // initialized. |g_log_file| will be nullptr in this case.
bool InitializeLogFileHandle() { bool InitializeLogFileHandle() {
if (log_file) if (g_log_file)
return true; return true;
if (!log_file_name) { if (!g_log_file_name) {
// Nobody has called InitLogging to specify a debug log file, so here we // Nobody has called InitLogging to specify a debug log file, so here we
// initialize the log file name to a default. // 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_TO_FILE) != 0) { if ((g_logging_destination & LOG_TO_FILE) != 0) {
#if defined(OS_WIN) #if defined(OS_WIN)
log_file = CreateFile(log_file_name->c_str(), GENERIC_WRITE, g_log_file = CreateFile(g_log_file_name->c_str(), GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
// try the current directory // try the current directory
log_file = CreateFile(L".\\debug.log", GENERIC_WRITE, g_log_file = CreateFile(L".\\debug.log", GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { if (g_log_file == INVALID_HANDLE_VALUE || g_log_file == nullptr) {
log_file = NULL; g_log_file = nullptr;
return false; return false;
} }
} }
SetFilePointer(log_file, 0, 0, FILE_END); SetFilePointer(g_log_file, 0, 0, FILE_END);
#elif defined(OS_POSIX) #elif defined(OS_POSIX)
log_file = fopen(log_file_name->c_str(), "a"); g_log_file = fopen(g_log_file_name->c_str(), "a");
if (log_file == NULL) if (g_log_file == nullptr)
return false; return false;
#endif #endif
} }
...@@ -337,18 +337,18 @@ void CloseFile(FileHandle log) { ...@@ -337,18 +337,18 @@ void CloseFile(FileHandle log) {
} }
void CloseLogFileUnlocked() { void CloseLogFileUnlocked() {
if (!log_file) if (!g_log_file)
return; return;
CloseFile(log_file); CloseFile(g_log_file);
log_file = NULL; g_log_file = nullptr;
} }
} // namespace } // namespace
LoggingSettings::LoggingSettings() LoggingSettings::LoggingSettings()
: logging_dest(LOG_DEFAULT), : logging_dest(LOG_DEFAULT),
log_file(NULL), log_file(nullptr),
lock_log(LOCK_LOG_FILE), lock_log(LOCK_LOG_FILE),
delete_old(APPEND_TO_OLD_LOG_FILE) {} delete_old(APPEND_TO_OLD_LOG_FILE) {}
...@@ -358,11 +358,11 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) { ...@@ -358,11 +358,11 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) {
CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0); CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
#endif #endif
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
// Don't bother initializing g_vlog_info unless we use one of the // Don't bother initializing |g_vlog_info| unless we use one of the
// vlog switches. // vlog switches.
if (command_line->HasSwitch(switches::kV) || if (command_line->HasSwitch(switches::kV) ||
command_line->HasSwitch(switches::kVModule)) { command_line->HasSwitch(switches::kVModule)) {
// NOTE: If g_vlog_info has already been initialized, it might be in use // NOTE: If |g_vlog_info| has already been initialized, it might be in use
// by another thread. Don't delete the old VLogInfo, just create a second // by another thread. Don't delete the old VLogInfo, just create a second
// one. We keep track of both to avoid memory leak warnings. // one. We keep track of both to avoid memory leak warnings.
CHECK(!g_vlog_info_prev); CHECK(!g_vlog_info_prev);
...@@ -371,13 +371,13 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) { ...@@ -371,13 +371,13 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) {
g_vlog_info = g_vlog_info =
new VlogInfo(command_line->GetSwitchValueASCII(switches::kV), new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
command_line->GetSwitchValueASCII(switches::kVModule), command_line->GetSwitchValueASCII(switches::kVModule),
&min_log_level); &g_min_log_level);
} }
logging_destination = settings.logging_dest; g_logging_destination = settings.logging_dest;
// ignore file options unless logging to file is set. // ignore file options unless logging to file is set.
if ((logging_destination & LOG_TO_FILE) == 0) if ((g_logging_destination & LOG_TO_FILE) == 0)
return true; return true;
LoggingLock::Init(settings.lock_log, settings.log_file); LoggingLock::Init(settings.lock_log, settings.log_file);
...@@ -387,21 +387,21 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) { ...@@ -387,21 +387,21 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) {
// default log file will re-initialize to the new options. // default log file will re-initialize to the new options.
CloseLogFileUnlocked(); CloseLogFileUnlocked();
if (!log_file_name) if (!g_log_file_name)
log_file_name = new PathString(); g_log_file_name = new PathString();
*log_file_name = settings.log_file; *g_log_file_name = settings.log_file;
if (settings.delete_old == DELETE_OLD_LOG_FILE) if (settings.delete_old == DELETE_OLD_LOG_FILE)
DeleteFilePath(*log_file_name); DeleteFilePath(*g_log_file_name);
return InitializeLogFileHandle(); return InitializeLogFileHandle();
} }
void SetMinLogLevel(int level) { void SetMinLogLevel(int level) {
min_log_level = std::min(LOG_FATAL, level); g_min_log_level = std::min(LOG_FATAL, level);
} }
int GetMinLogLevel() { int GetMinLogLevel() {
return min_log_level; return g_min_log_level;
} }
int GetVlogVerbosity() { int GetVlogVerbosity() {
...@@ -410,8 +410,8 @@ int GetVlogVerbosity() { ...@@ -410,8 +410,8 @@ int GetVlogVerbosity() {
int GetVlogLevelHelper(const char* file, size_t N) { int GetVlogLevelHelper(const char* file, size_t N) {
DCHECK_GT(N, 0U); DCHECK_GT(N, 0U);
// Note: g_vlog_info may change on a different thread during startup // Note: |g_vlog_info| may change on a different thread during startup
// (but will always be valid or NULL). // (but will always be valid or nullptr).
VlogInfo* vlog_info = g_vlog_info; VlogInfo* vlog_info = g_vlog_info;
return vlog_info ? return vlog_info ?
vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) : vlog_info->GetVlogLevel(base::StringPiece(file, N - 1)) :
...@@ -420,10 +420,10 @@ int GetVlogLevelHelper(const char* file, size_t N) { ...@@ -420,10 +420,10 @@ int GetVlogLevelHelper(const char* file, size_t N) {
void SetLogItems(bool enable_process_id, bool enable_thread_id, void SetLogItems(bool enable_process_id, bool enable_thread_id,
bool enable_timestamp, bool enable_tickcount) { bool enable_timestamp, bool enable_tickcount) {
log_process_id = enable_process_id; g_log_process_id = enable_process_id;
log_thread_id = enable_thread_id; g_log_thread_id = enable_thread_id;
log_timestamp = enable_timestamp; g_log_timestamp = enable_timestamp;
log_tickcount = enable_tickcount; g_log_tickcount = enable_tickcount;
} }
void SetShowErrorDialogs(bool enable_dialogs) { void SetShowErrorDialogs(bool enable_dialogs) {
...@@ -475,7 +475,7 @@ void DisplayDebugMessageInDialog(const std::string& str) { ...@@ -475,7 +475,7 @@ void DisplayDebugMessageInDialog(const std::string& str) {
// Message.exe" in the same directory as the application. If it // Message.exe" in the same directory as the application. If it
// exists, we use it, otherwise, we use a regular message box. // exists, we use it, otherwise, we use a regular message box.
wchar_t prog_name[MAX_PATH]; wchar_t prog_name[MAX_PATH];
GetModuleFileNameW(NULL, prog_name, MAX_PATH); GetModuleFileNameW(nullptr, prog_name, MAX_PATH);
wchar_t* backslash = wcsrchr(prog_name, '\\'); wchar_t* backslash = wcsrchr(prog_name, '\\');
if (backslash) if (backslash)
backslash[1] = 0; backslash[1] = 0;
...@@ -490,20 +490,20 @@ void DisplayDebugMessageInDialog(const std::string& str) { ...@@ -490,20 +490,20 @@ void DisplayDebugMessageInDialog(const std::string& str) {
startup_info.cb = sizeof(startup_info); startup_info.cb = sizeof(startup_info);
PROCESS_INFORMATION process_info; PROCESS_INFORMATION process_info;
if (CreateProcessW(prog_name, &cmdline[0], NULL, NULL, false, 0, NULL, if (CreateProcessW(prog_name, &cmdline[0], nullptr, nullptr, false, 0,
NULL, &startup_info, &process_info)) { nullptr, nullptr, &startup_info, &process_info)) {
WaitForSingleObject(process_info.hProcess, INFINITE); WaitForSingleObject(process_info.hProcess, INFINITE);
CloseHandle(process_info.hThread); CloseHandle(process_info.hThread);
CloseHandle(process_info.hProcess); CloseHandle(process_info.hProcess);
} else { } else {
// debug process broken, let's just do a message box // debug process broken, let's just do a message box
MessageBoxW(NULL, &cmdline[0], L"Fatal error", MessageBoxW(nullptr, &cmdline[0], L"Fatal error",
MB_OK | MB_ICONHAND | MB_TOPMOST); MB_OK | MB_ICONHAND | MB_TOPMOST);
} }
#else #else
// We intentionally don't implement a dialog on other platforms. // We intentionally don't implement a dialog on other platforms.
// You can just look at stderr. // You can just look at stderr.
#endif #endif // defined(OS_WIN)
} }
#endif // !defined(NDEBUG) #endif // !defined(NDEBUG)
...@@ -556,7 +556,7 @@ LogMessage::~LogMessage() { ...@@ -556,7 +556,7 @@ LogMessage::~LogMessage() {
return; return;
} }
if ((logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { if ((g_logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) {
#if defined(OS_WIN) #if defined(OS_WIN)
OutputDebugStringA(str_newline.c_str()); OutputDebugStringA(str_newline.c_str());
#elif defined(OS_ANDROID) #elif defined(OS_ANDROID)
...@@ -589,7 +589,7 @@ LogMessage::~LogMessage() { ...@@ -589,7 +589,7 @@ LogMessage::~LogMessage() {
} }
// write to log file // write to log file
if ((logging_destination & LOG_TO_FILE) != 0) { if ((g_logging_destination & LOG_TO_FILE) != 0) {
// We can have multiple threads and/or processes, so try to prevent them // We can have multiple threads and/or processes, so try to prevent them
// from clobbering each other's writes. // from clobbering each other's writes.
// If the client app did not call InitLogging, and the lock has not // If the client app did not call InitLogging, and the lock has not
...@@ -597,21 +597,21 @@ LogMessage::~LogMessage() { ...@@ -597,21 +597,21 @@ LogMessage::~LogMessage() {
// to do this at the same time, there will be a race condition to create // to do this at the same time, there will be a race condition to create
// the lock. This is why InitLogging should be called from the main // the lock. This is why InitLogging should be called from the main
// thread at the beginning of execution. // thread at the beginning of execution.
LoggingLock::Init(LOCK_LOG_FILE, NULL); LoggingLock::Init(LOCK_LOG_FILE, nullptr);
LoggingLock logging_lock; LoggingLock logging_lock;
if (InitializeLogFileHandle()) { if (InitializeLogFileHandle()) {
#if defined(OS_WIN) #if defined(OS_WIN)
SetFilePointer(log_file, 0, 0, SEEK_END); SetFilePointer(g_log_file, 0, 0, SEEK_END);
DWORD num_written; DWORD num_written;
WriteFile(log_file, WriteFile(g_log_file,
static_cast<const void*>(str_newline.c_str()), static_cast<const void*>(str_newline.c_str()),
static_cast<DWORD>(str_newline.length()), static_cast<DWORD>(str_newline.length()),
&num_written, &num_written,
NULL); nullptr);
#else #else
ignore_result(fwrite( ignore_result(fwrite(
str_newline.data(), str_newline.size(), 1, log_file)); str_newline.data(), str_newline.size(), 1, g_log_file));
fflush(log_file); fflush(g_log_file);
#endif #endif
} }
} }
...@@ -651,12 +651,12 @@ void LogMessage::Init(const char* file, int line) { ...@@ -651,12 +651,12 @@ void LogMessage::Init(const char* file, int line) {
// TODO(darin): It might be nice if the columns were fixed width. // TODO(darin): It might be nice if the columns were fixed width.
stream_ << '['; stream_ << '[';
if (log_process_id) if (g_log_process_id)
stream_ << CurrentProcessId() << ':'; stream_ << CurrentProcessId() << ':';
if (log_thread_id) if (g_log_thread_id)
stream_ << base::PlatformThread::CurrentId() << ':'; stream_ << base::PlatformThread::CurrentId() << ':';
if (log_timestamp) { if (g_log_timestamp) {
time_t t = time(NULL); time_t t = time(nullptr);
struct tm local_time = {0}; struct tm local_time = {0};
#ifdef _MSC_VER #ifdef _MSC_VER
localtime_s(&local_time, &t); localtime_s(&local_time, &t);
...@@ -673,7 +673,7 @@ void LogMessage::Init(const char* file, int line) { ...@@ -673,7 +673,7 @@ void LogMessage::Init(const char* file, int line) {
<< std::setw(2) << tm_time->tm_sec << std::setw(2) << tm_time->tm_sec
<< ':'; << ':';
} }
if (log_tickcount) if (g_log_tickcount)
stream_ << TickCount() << ':'; stream_ << TickCount() << ':';
if (severity_ >= 0) if (severity_ >= 0)
stream_ << log_severity_name(severity_); stream_ << log_severity_name(severity_);
...@@ -707,8 +707,8 @@ BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { ...@@ -707,8 +707,8 @@ BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
const int kErrorMessageBufferSize = 256; const int kErrorMessageBufferSize = 256;
char msgbuf[kErrorMessageBufferSize]; char msgbuf[kErrorMessageBufferSize];
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
DWORD len = FormatMessageA(flags, NULL, error_code, 0, msgbuf, DWORD len = FormatMessageA(flags, nullptr, error_code, 0, msgbuf,
arraysize(msgbuf), NULL); arraysize(msgbuf), nullptr);
if (len) { if (len) {
// Messages returned by system end with line breaks. // Messages returned by system end with line breaks.
return base::CollapseWhitespaceASCII(msgbuf, true) + return base::CollapseWhitespaceASCII(msgbuf, true) +
...@@ -723,7 +723,7 @@ BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { ...@@ -723,7 +723,7 @@ BASE_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) {
} }
#else #else
#error Not implemented #error Not implemented
#endif #endif // defined(OS_WIN)
#if defined(OS_WIN) #if defined(OS_WIN)
...@@ -754,7 +754,7 @@ ErrnoLogMessage::ErrnoLogMessage(const char* file, ...@@ -754,7 +754,7 @@ ErrnoLogMessage::ErrnoLogMessage(const char* file,
ErrnoLogMessage::~ErrnoLogMessage() { ErrnoLogMessage::~ErrnoLogMessage() {
stream() << ": " << SystemErrorCodeToString(err_); stream() << ": " << SystemErrorCodeToString(err_);
} }
#endif // OS_WIN #endif // defined(OS_WIN)
void CloseLogFile() { void CloseLogFile() {
LoggingLock logging_lock; LoggingLock logging_lock;
...@@ -762,7 +762,7 @@ void CloseLogFile() { ...@@ -762,7 +762,7 @@ void CloseLogFile() {
} }
void RawLog(int level, const char* message) { void RawLog(int level, const char* message) {
if (level >= min_log_level) { if (level >= g_min_log_level) {
size_t bytes_written = 0; size_t bytes_written = 0;
const size_t message_len = strlen(message); const size_t message_len = strlen(message);
int rv; int rv;
...@@ -797,8 +797,8 @@ void RawLog(int level, const char* message) { ...@@ -797,8 +797,8 @@ void RawLog(int level, const char* message) {
#if defined(OS_WIN) #if defined(OS_WIN)
std::wstring GetLogFileFullPath() { std::wstring GetLogFileFullPath() {
if (log_file_name) if (g_log_file_name)
return *log_file_name; return *g_log_file_name;
return std::wstring(); return std::wstring();
} }
#endif #endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment