Don't crash when logging if CommandLine is uninitialized.
The base::CommandLine singleton is usually initialized from
main(), but some gtest-based unit tests don't have control
over that function. Guard CommandLine accesses in
base/logging.cc to handle this gracefully.
Bug: 24536569
Change-Id: If3eab0f36c28bb6b3cf3569c61c7d7cec5cf7b10
(Reconstructed from b8c214021901c7f18bf76f1fdb4cec6e8cf8db49)
diff --git a/base/logging.cc b/base/logging.cc
index e86c6ab..156f19a 100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -407,21 +407,23 @@
// Can log only to the system debug log.
CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0);
#endif
- base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
- // Don't bother initializing |g_vlog_info| unless we use one of the
- // vlog switches.
- if (command_line->HasSwitch(switches::kV) ||
- command_line->HasSwitch(switches::kVModule)) {
- // 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
- // one. We keep track of both to avoid memory leak warnings.
- CHECK(!g_vlog_info_prev);
- g_vlog_info_prev = g_vlog_info;
+ if (base::CommandLine::InitializedForCurrentProcess()) {
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
+ // Don't bother initializing |g_vlog_info| unless we use one of the
+ // vlog switches.
+ if (command_line->HasSwitch(switches::kV) ||
+ command_line->HasSwitch(switches::kVModule)) {
+ // 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
+ // one. We keep track of both to avoid memory leak warnings.
+ CHECK(!g_vlog_info_prev);
+ g_vlog_info_prev = g_vlog_info;
- g_vlog_info =
- new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
- command_line->GetSwitchValueASCII(switches::kVModule),
- &g_min_log_level);
+ g_vlog_info =
+ new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
+ command_line->GetSwitchValueASCII(switches::kVModule),
+ &g_min_log_level);
+ }
}
g_logging_destination = settings.logging_dest;
@@ -776,6 +778,9 @@
__android_log_write(priority, "chromium", str_newline.c_str());
__android_log_write(
priority,
+ base::CommandLine::InitializedForCurrentProcess() ?
+ base::CommandLine::ForCurrentProcess()->
+ GetProgram().BaseName().value().c_str() : nullptr,
str_newline.c_str());
#endif // 0
ignore_result(fwrite(str_newline.data(), str_newline.size(), 1, stderr));