Skip to content

Commit

Permalink
logger: remove exceptions (#37265)
Browse files Browse the repository at this point in the history
Risk Level: low
Testing: updated tests
Docs Changes: n/a
Release Notes: n/a
envoyproxy/envoy-mobile#176

Signed-off-by: Alyssa Wilk <alyssar@chromium.org>
  • Loading branch information
alyssawilk authored Nov 21, 2024
1 parent e0c9f1e commit 19d1308
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
17 changes: 11 additions & 6 deletions source/common/common/logger_delegates.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<FileSinkDelegate>>
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<FileSinkDelegate>(
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();
}

Expand Down
9 changes: 7 additions & 2 deletions source/common/common/logger_delegates.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::unique_ptr<FileSinkDelegate>>
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_;
};
Expand Down
6 changes: 4 additions & 2 deletions source/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Logger::FileSinkDelegate>(
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<Logger::FileSinkDelegate>);
}
END_TRY
CATCH(const EnvoyException& e, {
Expand Down
2 changes: 1 addition & 1 deletion test/test_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Logger::FileSinkDelegate>(
file_logger = *Logger::FileSinkDelegate::create(
TestEnvironment::getOptions().logPath(), access_log_manager, Logger::Registry::getSink());
}

Expand Down
1 change: 0 additions & 1 deletion tools/code_format/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 19d1308

Please sign in to comment.