[storage] Logging to serial

The cl renables minfs and blobfs logging
to serial. This is a workaround for
bug 66476.

Test: Booted to see messages on serial like
"[/pkg/bin/minfs] INFO: [minfs.cc(1273)] Replaying journal"

Bug: 66263
Fixed: 68158

Change-Id: Iba67acfb866e567e56466038381df68cac75b256
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/474241
Commit-Queue: Vikram Auradkar <auradkar@google.com>
Reviewed-by: Chris Suter <csuter@google.com>
diff --git a/src/storage/bin/blobfs/BUILD.gn b/src/storage/bin/blobfs/BUILD.gn
index 3f4f9f2..c08addb 100644
--- a/src/storage/bin/blobfs/BUILD.gn
+++ b/src/storage/bin/blobfs/BUILD.gn
@@ -14,6 +14,7 @@
     "//sdk/lib/fdio",
     "//sdk/lib/syslog/cpp",
     "//src/storage/blobfs",
+    "//src/storage/lib/utils:use_debug_log",
     "//third_party/boringssl",
     "//zircon/public/lib/bitmap",
     "//zircon/public/lib/fbl",
diff --git a/src/storage/bin/blobfs/main.cc b/src/storage/bin/blobfs/main.cc
index 1500faf5..cd48ed0 100644
--- a/src/storage/bin/blobfs/main.cc
+++ b/src/storage/bin/blobfs/main.cc
@@ -36,6 +36,7 @@
 #include "src/storage/blobfs/fsck.h"
 #include "src/storage/blobfs/mkfs.h"
 #include "src/storage/blobfs/mount.h"
+#include "src/storage/lib/utils/use_debug_log.h"
 
 namespace {
 
@@ -302,6 +303,9 @@
 }  // namespace
 
 int main(int argc, char** argv) {
+  // TODO(fxbug.dev/66476)
+  storage::UseDebugLog("blobfs");
+
   CommandFunction func = nullptr;
   auto options_or = ProcessArgs(argc, argv, &func);
   if (options_or.is_error()) {
diff --git a/src/storage/bin/minfs/BUILD.gn b/src/storage/bin/minfs/BUILD.gn
index b7369bd..86ec12e 100644
--- a/src/storage/bin/minfs/BUILD.gn
+++ b/src/storage/bin/minfs/BUILD.gn
@@ -13,6 +13,7 @@
     "//sdk/fidl/fuchsia.minfs:fuchsia.minfs_c",
     "//sdk/lib/fdio",
     "//sdk/lib/syslog/cpp",
+    "//src/storage/lib/utils:use_debug_log",
     "//src/storage/minfs",
     "//zircon/public/lib/async",
     "//zircon/public/lib/async-cpp",
diff --git a/src/storage/bin/minfs/main.cc b/src/storage/bin/minfs/main.cc
index 71d3e0a..947b57f 100644
--- a/src/storage/bin/minfs/main.cc
+++ b/src/storage/bin/minfs/main.cc
@@ -31,6 +31,7 @@
 #include <block-client/cpp/block-device.h>
 #include <block-client/cpp/remote-block-device.h>
 
+#include "src/storage/lib/utils/use_debug_log.h"
 #include "src/storage/minfs/fsck.h"
 #include "src/storage/minfs/minfs.h"
 
@@ -157,6 +158,8 @@
 }  // namespace
 
 int main(int argc, char** argv) {
+  // TODO(fxbug.dev/66476)
+  storage::UseDebugLog("minfs");
   minfs::MountOptions options;
 
   const std::vector<Command> commands = {
diff --git a/src/storage/fshost/BUILD.gn b/src/storage/fshost/BUILD.gn
index e459fa3..bb4c391 100644
--- a/src/storage/fshost/BUILD.gn
+++ b/src/storage/fshost/BUILD.gn
@@ -219,6 +219,7 @@
     "//sdk/fidl/fuchsia.fshost:fuchsia.fshost_llcpp",
     "//sdk/fidl/fuchsia.io:fuchsia.io_llcpp",
     "//sdk/fidl/fuchsia.ldsvc:fuchsia.ldsvc_c",
+    "//src/storage/lib/utils:use_debug_log",
     "//zircon/system/ulib/cobalt-client",
     "//zircon/system/ulib/fidl-async:fidl-async-cpp",
     "//zircon/system/ulib/fs-management",
diff --git a/src/storage/fshost/main.cc b/src/storage/fshost/main.cc
index c322e66..48cfa41 100644
--- a/src/storage/fshost/main.cc
+++ b/src/storage/fshost/main.cc
@@ -43,6 +43,7 @@
 #include "fs-manager.h"
 #include "metrics.h"
 #include "src/storage/fshost/deprecated-loader-service.h"
+#include "src/storage/lib/utils/use_debug_log.h"
 
 namespace fio = ::llcpp::fuchsia::io;
 
@@ -173,61 +174,6 @@
   return ZX_OK;
 }
 
-zx_status_t LogToDebugLog(llcpp::fuchsia::boot::WriteOnlyLog::SyncClient log_client) {
-  auto result = log_client.Get();
-  if (result.status() != ZX_OK) {
-    return result.status();
-  }
-  char process_name[ZX_MAX_NAME_LEN] = {};
-  zx::process::self()->get_property(ZX_PROP_NAME, process_name, sizeof(process_name));
-  const char* tag = process_name;
-  fx_logger_config_t logger_config{
-      .min_severity = fx_logger_get_min_severity(fx_log_get_logger()),
-      .console_fd = -1,
-      .log_service_channel = ZX_HANDLE_INVALID,
-      .tags = &tag,
-      .num_tags = 1,
-  };
-  zx_status_t status = fdio_fd_create(result.Unwrap()->log.release(), &logger_config.console_fd);
-  if (status != ZX_OK) {
-    return status;
-  }
-  return fx_log_reconfigure(&logger_config);
-}
-
-zx::status<llcpp::fuchsia::boot::WriteOnlyLog::SyncClient> ConnectToWriteLog() {
-  zx::channel local, remote;
-  zx_status_t status = zx::channel::create(0, &local, &remote);
-  if (status != ZX_OK) {
-    return zx::error(status);
-  }
-  status = fdio_service_connect("/svc/fuchsia.boot.WriteOnlyLog", remote.release());
-  if (status != ZX_OK) {
-    return zx::error(status);
-  }
-
-  return zx::ok(llcpp::fuchsia::boot::WriteOnlyLog::SyncClient(std::move(local)));
-}
-
-// Opens handle to log write service and reconfigures syslog to use that
-// handle for logging. This is a short term fix for a bug where in on a
-// board with userdebug build, no logs show up on serial.
-// TODO(fxbug.dev/66476)
-void UseDebugLog() {
-  auto log_client_or = ConnectToWriteLog();
-  if (log_client_or.is_error()) {
-    std::cerr << "fshost: failed to get write log: "
-              << zx_status_get_string(log_client_or.error_value()) << std::endl;
-    return;
-  }
-
-  if (auto status = LogToDebugLog(std::move(log_client_or.value())); status != ZX_OK) {
-    std::cerr << "fshost: Failed to reconfigure logger to use debuglog: "
-              << zx_status_get_string(status) << std::endl;
-    return;
-  }
-}
-
 Config GetConfig(const FshostBootArgs& boot_args) {
   std::ifstream file("/pkg/config/fshost");
   Config::Options options;
@@ -281,7 +227,7 @@
   Config config = GetConfig(*boot_args);
 
   if (!config.is_set(Config::kUseSyslog))
-    UseDebugLog();
+    storage::UseDebugLog("fshost");
 
   FX_LOGS(INFO) << "Config: " << config;
 
diff --git a/src/storage/lib/utils/BUILD.gn b/src/storage/lib/utils/BUILD.gn
index 856d1d0..3a62ee83 100644
--- a/src/storage/lib/utils/BUILD.gn
+++ b/src/storage/lib/utils/BUILD.gn
@@ -14,3 +14,17 @@
     "//zircon/system/ulib/fdio-caller",
   ]
 }
+
+source_set("use_debug_log") {
+  sources = [
+    "use_debug_log.cc",
+    "use_debug_log.h",
+  ]
+  deps = [
+    "//sdk/fidl/fuchsia.boot:fuchsia.boot_c",
+    "//sdk/fidl/fuchsia.boot:fuchsia.boot_llcpp",
+    "//sdk/lib/syslog/cpp",
+    "//zircon/system/ulib/fdio-caller",
+    "//zircon/system/ulib/syslog",
+  ]
+}
diff --git a/src/storage/lib/utils/use_debug_log.cc b/src/storage/lib/utils/use_debug_log.cc
new file mode 100644
index 0000000..80a5141c
--- /dev/null
+++ b/src/storage/lib/utils/use_debug_log.cc
@@ -0,0 +1,76 @@
+// Copyright 2021 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/storage/lib/utils/use_debug_log.h"
+
+#include <fuchsia/boot/c/fidl.h>
+#include <fuchsia/boot/llcpp/fidl.h>
+#include <lib/fdio/cpp/caller.h>
+#include <lib/fdio/directory.h>
+#include <lib/syslog/cpp/macros.h>
+#include <lib/syslog/global.h>
+#include <lib/zx/process.h>
+#include <lib/zx/status.h>
+#include <zircon/processargs.h>
+
+#include <iostream>
+
+namespace storage {
+namespace {
+
+zx_status_t LogToDebugLog(llcpp::fuchsia::boot::WriteOnlyLog::SyncClient log_client) {
+  auto result = log_client.Get();
+  if (result.status() != ZX_OK) {
+    return result.status();
+  }
+  char process_name[ZX_MAX_NAME_LEN] = {};
+  zx::process::self()->get_property(ZX_PROP_NAME, process_name, sizeof(process_name));
+  const char* tag = process_name;
+  fx_logger_config_t logger_config{
+      .min_severity = fx_logger_get_min_severity(fx_log_get_logger()),
+      .console_fd = -1,
+      .log_service_channel = ZX_HANDLE_INVALID,
+      .tags = &tag,
+      .num_tags = 1,
+  };
+  zx_status_t status = fdio_fd_create(result.Unwrap()->log.release(), &logger_config.console_fd);
+  if (status != ZX_OK) {
+    return status;
+  }
+  return fx_log_reconfigure(&logger_config);
+}
+
+zx::status<llcpp::fuchsia::boot::WriteOnlyLog::SyncClient> ConnectToWriteLog() {
+  zx::channel local, remote;
+  zx_status_t status = zx::channel::create(0, &local, &remote);
+  if (status != ZX_OK) {
+    return zx::error(status);
+  }
+  status = fdio_service_connect("/svc/fuchsia.boot.WriteOnlyLog", remote.release());
+  if (status != ZX_OK) {
+    return zx::error(status);
+  }
+
+  return zx::ok(llcpp::fuchsia::boot::WriteOnlyLog::SyncClient(std::move(local)));
+}
+
+}  // namespace
+
+void UseDebugLog(const std::string& tag) {
+  auto log_client_or = ConnectToWriteLog();
+  if (log_client_or.is_error()) {
+    std::cerr << tag
+              << ": failed to get write log: " << zx_status_get_string(log_client_or.error_value())
+              << std::endl;
+    return;
+  }
+
+  if (auto status = LogToDebugLog(std::move(log_client_or.value())); status != ZX_OK) {
+    std::cerr << tag
+              << ": Failed to reconfigure logger to use debuglog: " << zx_status_get_string(status)
+              << std::endl;
+    return;
+  }
+}
+}  // namespace storage
diff --git a/src/storage/lib/utils/use_debug_log.h b/src/storage/lib/utils/use_debug_log.h
new file mode 100644
index 0000000..d21729d
--- /dev/null
+++ b/src/storage/lib/utils/use_debug_log.h
@@ -0,0 +1,24 @@
+// Copyright 2021 The Fuchsia Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef SRC_STORAGE_LIB_UTILS_USE_DEBUG_LOG_H_
+#define SRC_STORAGE_LIB_UTILS_USE_DEBUG_LOG_H_
+
+#include <lib/zx/channel.h>
+#include <lib/zx/status.h>
+
+#include <string>
+#include <string_view>
+
+namespace storage {
+
+// Opens handle to log write service and reconfigures syslog to use that
+// handle for logging. This is a short term fix for a bug where in on a
+// board with userdebug build, no logs show up on serial.
+// TODO(fxbug.dev/66476)
+void UseDebugLog(const std::string& tag);
+
+}  // namespace storage
+
+#endif  // SRC_STORAGE_LIB_UTILS_USE_DEBUG_LOG_H_