]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crimson/osd: reset logger before exit 46522/head
authorKefu Chai <tchaikov@gmail.com>
Sun, 5 Jun 2022 10:30:28 +0000 (18:30 +0800)
committerKefu Chai <tchaikov@gmail.com>
Sun, 5 Jun 2022 10:36:03 +0000 (18:36 +0800)
* extract the code to set logging fstream into a dedicated function
* do not reset logging until the end of the seastar application.

before this change, `reset_logger` is created in the
`if (auto log_file = local_conf()->log_file; !log_file.empty())` branch,
so its life cycle ends when the `if` block ends. in other words,
the cerr fstream is used for logging after the `if` block ends.
this is not the expected behavior.

after this changge, `reset_logger` is created out of the `if` block.
so we won't reset the logger back to `cerr` until the lambda passed to
`seastar::async()` exits.

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/crimson/osd/main.cc

index 4f0661076bec6b5e669f72150a1f12af77158e3f..3855a39700c3d6dea9e33d56c3b11eb81c51c2f1 100644 (file)
@@ -174,6 +174,21 @@ static void override_seastar_opts(std::vector<const char*>& args)
   }
 }
 
+static std::ofstream maybe_set_logger()
+{
+  std::ofstream log_file_stream;
+  if (auto log_file = local_conf()->log_file; !log_file.empty()) {
+    log_file_stream.open(log_file, std::ios::app | std::ios::out);
+    try {
+      seastar::throw_system_error_on(log_file_stream.fail());
+    } catch (const std::system_error& e) {
+      ceph_abort_msg(fmt::format("unable to open log file: {}", e.what()));
+    }
+    logger().set_ostream(log_file_stream);
+  }
+  return log_file_stream;
+}
+
 int main(int argc, const char* argv[])
 {
   seastar::app_template::config app_cfg;
@@ -239,19 +254,10 @@ int main(int argc, const char* argv[])
           local_conf().parse_config_files(conf_file_list).get();
           local_conf().parse_env().get();
           local_conf().parse_argv(config_proxy_args).get();
-          std::ofstream log_file_stream;
-          if (auto log_file = local_conf()->log_file; !log_file.empty()) {
-            log_file_stream.open(log_file, std::ios::app | std::ios::out);
-            try {
-              seastar::throw_system_error_on(log_file_stream.fail());
-            } catch (const std::system_error& e) {
-              ceph_abort_msg(fmt::format("unable to open log file: {}", e.what()));
-            }
-            auto reset_logger = seastar::defer([] {
-              logger().set_ostream(std::cerr);
-            });
-            logger().set_ostream(log_file_stream);
-          }
+          auto log_file_stream = maybe_set_logger();
+          auto reset_logger = seastar::defer([] {
+            logger().set_ostream(std::cerr);
+          });
           if (const auto ret = pidfile_write(local_conf()->pid_file);
               ret == -EACCES || ret == -EAGAIN) {
             ceph_abort_msg(