From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Joshua Peraza <jperaza@chromium.org>
Date: Wed, 21 Oct 2020 11:10:25 -0700
Subject: Initialize logging for crashpad

Although logging to files is not yet supported by mini_chromium, it is
the default behavior for OS_WIN in chromium. This change should
cause crashpad to log via OutputDebugString() on Windows, instead of
debug.log files. Future work (crbug.com/crashpad/26) should arrange for
logs to be uploaded with reports, embedded in associated minidumps or as
file attachments.

Bug: chromium:711159
Change-Id: I0f9004f7de94dd29d555cc7d23c48a63da6b4bba
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/2425108
Reviewed-by: Mark Mentovai <mark@chromium.org>

diff --git a/base/logging.cc b/base/logging.cc
index 608cc1122b98803b66c4b4a4eee1020ffe94e20c..6f9005ba2f21efe2f81b4fde0360d672b9f58022 100644
--- a/base/logging.cc
+++ b/base/logging.cc
@@ -410,21 +410,23 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) {
            0u);
 #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;
-
-    g_vlog_info =
-        new VlogInfo(command_line->GetSwitchValueASCII(switches::kV),
-                     command_line->GetSwitchValueASCII(switches::kVModule),
-                     &g_min_log_level);
+  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_logging_destination = settings.logging_dest;
@@ -435,7 +437,10 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) {
     config.min_severity = FX_LOG_INFO;
     config.console_fd = -1;
     config.log_service_channel = ZX_HANDLE_INVALID;
-    std::string log_tag = command_line->GetProgram().BaseName().AsUTF8Unsafe();
+    std::string log_tag = base::CommandLine::ForCurrentProcess()
+                              ->GetProgram()
+                              .BaseName()
+                              .AsUTF8Unsafe();
     const char* log_tag_data = log_tag.data();
     config.tags = &log_tag_data;
     config.num_tags = 1;
diff --git a/third_party/crashpad/crashpad/DEPS b/third_party/crashpad/crashpad/DEPS
index 8b958f1887be77ac54c878a9e1474fb67574950a..0cd250f0bb67888e83fcdd8cce856e0d8a8d29a5 100644
--- a/third_party/crashpad/crashpad/DEPS
+++ b/third_party/crashpad/crashpad/DEPS
@@ -42,7 +42,7 @@ deps = {
       '7bde79cc274d06451bf65ae82c012a5d3e476b5a',
   'crashpad/third_party/mini_chromium/mini_chromium':
       Var('chromium_git') + '/chromium/mini_chromium@' +
-      'c426ff98e1d9e9d59777fe8b883a5c0ceeca9ca3',
+      '5fc64bfbf1c000161445c586de45e40464ff2314',
   'crashpad/third_party/libfuzzer/src':
       Var('chromium_git') + '/chromium/llvm-project/compiler-rt/lib/fuzzer.git@' +
       'fda403cf93ecb8792cb1d061564d89a6553ca020',
diff --git a/third_party/crashpad/crashpad/handler/handler_main.cc b/third_party/crashpad/crashpad/handler/handler_main.cc
index e0a262cd1f38bb4b663b6d13b28fcc272d69bef1..6e6adc536208f5094589e885ce6a7610e994b5d3 100644
--- a/third_party/crashpad/crashpad/handler/handler_main.cc
+++ b/third_party/crashpad/crashpad/handler/handler_main.cc
@@ -519,16 +519,26 @@ class ScopedStoppable {
   DISALLOW_COPY_AND_ASSIGN(ScopedStoppable);
 };
 
+void InitCrashpadLogging() {
+  logging::LoggingSettings settings;
+#if defined(OS_CHROMEOS)
+  settings.logging_dest = logging::LOG_TO_FILE;
+  settings.log_file_path = "/var/log/chrome/chrome";
+#elif defined(OS_WIN)
+  settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
+#else
+  settings.logging_dest =
+      logging::LOG_TO_SYSTEM_DEBUG_LOG | logging::LOG_TO_STDERR;
+#endif
+  logging::InitLogging(settings);
+}
+
 }  // namespace
 
 int HandlerMain(int argc,
                 char* argv[],
                 const UserStreamDataSources* user_stream_sources) {
-#if defined(OS_CHROMEOS)
-  if (freopen("/var/log/chrome/chrome", "a", stderr) == nullptr) {
-    PLOG(ERROR) << "Failed to redirect stderr to /var/log/chrome/chrome";
-  }
-#endif
+  InitCrashpadLogging();
 
   InstallCrashHandler();
   CallMetricsRecordNormalExit metrics_record_normal_exit;
diff --git a/third_party/crashpad/crashpad/test/gtest_main.cc b/third_party/crashpad/crashpad/test/gtest_main.cc
index ad3a095ea7657b60b6fc05d09fb04319dbee1b17..5b64a2f886bca5531691963059dd47e89174fb04 100644
--- a/third_party/crashpad/crashpad/test/gtest_main.cc
+++ b/third_party/crashpad/crashpad/test/gtest_main.cc
@@ -12,6 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
+#include "base/logging.h"
 #include "build/build_config.h"
 #include "gtest/gtest.h"
 #include "test/main_arguments.h"
@@ -91,6 +92,12 @@ int main(int argc, char* argv[]) {
 
 #endif  // CRASHPAD_IS_IN_CHROMIUM
 
+// base::TestSuite initializes logging when using Chromium's test launcher.
+logging::LoggingSettings settings;
+settings.logging_dest =
+    logging::LOG_TO_STDERR | logging::LOG_TO_SYSTEM_DEBUG_LOG;
+logging::InitLogging(settings);
+
 #if defined(CRASHPAD_TEST_LAUNCHER_GMOCK)
   testing::InitGoogleMock(&argc, argv);
 #elif defined(CRASHPAD_TEST_LAUNCHER_GTEST)
