Allow update_engine to log to both logd and filesystem

Add a new option "logtofile" which default to false.
1. If both "logtostderr" and "logtofile" present, write to system debug
and log file.
2. If only "logtostderr" presents, write to system debug.
3. Otherwise write to log file only.

This keep the old logging behavior for omaha.

Bug: 65568605
Test: apply an update on pixel2 and check the log.
Change-Id: Id998f626fe5ebefbf6f4dffc19aee1c552343ab6
diff --git a/main.cc b/main.cc
index 4275bc1..91805ae 100644
--- a/main.cc
+++ b/main.cc
@@ -38,6 +38,7 @@
 namespace chromeos_update_engine {
 namespace {
 
+#ifndef __ANDROID__
 void SetupLogSymlink(const string& symlink_path, const string& log_path) {
   // TODO(petkov): To ensure a smooth transition between non-timestamped and
   // timestamped logs, move an existing log to start the first timestamped
@@ -75,23 +76,27 @@
   SetupLogSymlink(kLogSymlink, kLogPath);
   return kLogSymlink;
 }
+#endif  // __ANDROID__
 
-void SetupLogging(bool log_to_std_err) {
-  string log_file;
+void SetupLogging(bool log_to_system, bool log_to_file) {
   logging::LoggingSettings log_settings;
   log_settings.lock_log = logging::DONT_LOCK_LOG_FILE;
-  log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
+  log_settings.logging_dest = static_cast<logging::LoggingDestination>(
+      (log_to_system ? logging::LOG_TO_SYSTEM_DEBUG_LOG : 0) |
+      (log_to_file ? logging::LOG_TO_FILE : 0));
+  log_settings.log_file = nullptr;
 
-  if (log_to_std_err) {
-    // Log to stderr initially.
-    log_settings.log_file = nullptr;
-    log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
-  } else {
+  string log_file;
+  if (log_to_file) {
+#ifdef __ANDROID__
+    log_file = "/data/misc/update_engine/update_engine.log";
+    log_settings.delete_old = logging::DELETE_OLD_LOG_FILE;
+#else
     log_file = SetupLogFile("/var/log");
+    log_settings.delete_old = logging::APPEND_TO_OLD_LOG_FILE;
+#endif  // __ANDROID__
     log_settings.log_file = log_file.c_str();
-    log_settings.logging_dest = logging::LOG_TO_FILE;
   }
-
   logging::InitLogging(log_settings);
 }
 
@@ -99,6 +104,7 @@
 }  // namespace chromeos_update_engine
 
 int main(int argc, char** argv) {
+  DEFINE_bool(logtofile, false, "Write logs to a file in log_dir.");
   DEFINE_bool(logtostderr, false,
               "Write logs to stderr instead of to a file in log_dir.");
   DEFINE_bool(foreground, false,
@@ -106,7 +112,15 @@
 
   chromeos_update_engine::Terminator::Init();
   brillo::FlagHelper::Init(argc, argv, "Chromium OS Update Engine");
-  chromeos_update_engine::SetupLogging(FLAGS_logtostderr);
+
+  // We have two logging flags "--logtostderr" and "--logtofile"; and the logic
+  // to choose the logging destination is:
+  // 1. --logtostderr --logtofile -> logs to both
+  // 2. --logtostderr             -> logs to system debug
+  // 3. --logtofile or no flags   -> logs to file
+  bool log_to_system = FLAGS_logtostderr;
+  bool log_to_file = FLAGS_logtofile || !FLAGS_logtostderr;
+  chromeos_update_engine::SetupLogging(log_to_system, log_to_file);
   if (!FLAGS_foreground)
     PLOG_IF(FATAL, daemon(0, 0) == 1) << "daemon() failed";
 
diff --git a/update_engine.rc b/update_engine.rc
index b6a706a..0576045 100644
--- a/update_engine.rc
+++ b/update_engine.rc
@@ -1,4 +1,4 @@
-service update_engine /system/bin/update_engine --logtostderr --foreground
+service update_engine /system/bin/update_engine --logtostderr --logtofile --foreground
     class late_start
     user root
     group root system wakelock inet cache