From: Kefu Chai Date: Sun, 5 Jun 2022 10:30:28 +0000 (+0800) Subject: crimson/osd: reset logger before exit X-Git-Tag: v18.0.0~773^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=66c923d70354415cd1746c4f57cf31f3d55cc1bd;p=ceph.git crimson/osd: reset logger before exit * 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 --- diff --git a/src/crimson/osd/main.cc b/src/crimson/osd/main.cc index 4f0661076bec6..3855a39700c3d 100644 --- a/src/crimson/osd/main.cc +++ b/src/crimson/osd/main.cc @@ -174,6 +174,21 @@ static void override_seastar_opts(std::vector& 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(