diff --git a/source/common/common/logger_delegates.cc b/source/common/common/logger_delegates.cc index 52975cd22129..1ab47bb0b503 100644 --- a/source/common/common/logger_delegates.cc +++ b/source/common/common/logger_delegates.cc @@ -9,14 +9,19 @@ namespace Envoy { namespace Logger { -FileSinkDelegate::FileSinkDelegate(const std::string& log_path, - AccessLog::AccessLogManager& log_manager, - DelegatingLogSinkSharedPtr log_sink) - : SinkDelegate(log_sink) { +absl::StatusOr> +FileSinkDelegate::create(const std::string& log_path, AccessLog::AccessLogManager& log_manager, + DelegatingLogSinkSharedPtr log_sink) { auto file_or_error = log_manager.createAccessLog( Filesystem::FilePathAndType{Filesystem::DestinationType::File, log_path}); - THROW_IF_NOT_OK_REF(file_or_error.status()); - log_file_ = file_or_error.value(); + RETURN_IF_NOT_OK_REF(file_or_error.status()); + return std::unique_ptr( + new FileSinkDelegate(std::move(*file_or_error), log_sink)); +} + +FileSinkDelegate::FileSinkDelegate(AccessLog::AccessLogFileSharedPtr&& log_file, + DelegatingLogSinkSharedPtr log_sink) + : SinkDelegate(log_sink), log_file_(std::move(log_file)) { setDelegate(); } diff --git a/source/common/common/logger_delegates.h b/source/common/common/logger_delegates.h index c85d0b1b46a0..1b4659ab1426 100644 --- a/source/common/common/logger_delegates.h +++ b/source/common/common/logger_delegates.h @@ -19,14 +19,19 @@ namespace Logger { */ class FileSinkDelegate : public SinkDelegate { public: - FileSinkDelegate(const std::string& log_path, AccessLog::AccessLogManager& log_manager, - DelegatingLogSinkSharedPtr log_sink); + static absl::StatusOr> + create(const std::string& log_path, AccessLog::AccessLogManager& log_manager, + DelegatingLogSinkSharedPtr log_sink); ~FileSinkDelegate() override; // SinkDelegate void log(absl::string_view msg, const spdlog::details::log_msg& log_msg) override; void flush() override; +protected: + FileSinkDelegate(AccessLog::AccessLogFileSharedPtr&& log_file, + DelegatingLogSinkSharedPtr log_sink); + private: AccessLog::AccessLogFileSharedPtr log_file_; }; diff --git a/source/server/server.cc b/source/server/server.cc index 7486062863db..89e4980fa4c3 100644 --- a/source/server/server.cc +++ b/source/server/server.cc @@ -386,8 +386,10 @@ void InstanceBase::initialize(Network::Address::InstanceConstSharedPtr local_add ComponentFactory& component_factory) { std::function set_up_logger = [&] { TRY_ASSERT_MAIN_THREAD { - file_logger_ = std::make_unique( - options_.logPath(), access_log_manager_, Logger::Registry::getSink()); + file_logger_ = THROW_OR_RETURN_VALUE( + Logger::FileSinkDelegate::create(options_.logPath(), access_log_manager_, + Logger::Registry::getSink()), + std::unique_ptr); } END_TRY CATCH(const EnvoyException& e, { diff --git a/test/test_runner.cc b/test/test_runner.cc index 0770cfca3a2c..158de9299c73 100644 --- a/test/test_runner.cc +++ b/test/test_runner.cc @@ -153,7 +153,7 @@ int TestRunner::runTests(int argc, char** argv) { // However do not redirect to file from death test children as the parent typically // looks for specific output in stderr if (!TestEnvironment::getOptions().logPath().empty() && !is_death_test_child) { - file_logger = std::make_unique( + file_logger = *Logger::FileSinkDelegate::create( TestEnvironment::getOptions().logPath(), access_log_manager, Logger::Registry::getSink()); } diff --git a/tools/code_format/config.yaml b/tools/code_format/config.yaml index be06ac1c392c..0ff97816a6aa 100644 --- a/tools/code_format/config.yaml +++ b/tools/code_format/config.yaml @@ -142,7 +142,6 @@ paths: - source/common/secret/sds_api.cc - source/common/router/router.cc - source/common/config/config_provider_impl.h - - source/common/common/logger_delegates.cc - source/common/grpc/async_client_impl.cc - source/common/grpc/google_grpc_creds_impl.cc - source/common/local_reply/local_reply.cc