-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Description: adds a new SinkDelegate called LambdaDelegate which uses user-provided callbacks to drive log functionality. Risk Level: low Testing: new unit tests and integration test in main_interface_test. Signed-off-by: Jose Nino <[email protected]> Signed-off-by: JP Simard <[email protected]>
- Loading branch information
Showing
9 changed files
with
162 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@envoy//bazel:envoy_build_system.bzl", "envoy_cc_library", "envoy_package") | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_package() | ||
|
||
envoy_cc_library( | ||
name = "lambda_logger_delegate_lib", | ||
srcs = ["lambda_logger_delegate.cc"], | ||
hdrs = ["lambda_logger_delegate.h"], | ||
repository = "@envoy", | ||
deps = [ | ||
"//library/common/data:utility_lib", | ||
"//library/common/types:c_types_lib", | ||
"@envoy//source/common/common:macros", | ||
"@envoy//source/common/common:minimal_logger_lib", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "library/common/common/lambda_logger_delegate.h" | ||
|
||
#include <iostream> | ||
|
||
#include "library/common/data/utility.h" | ||
|
||
namespace Envoy { | ||
namespace Logger { | ||
|
||
LambdaDelegate::LambdaDelegate(envoy_logger logger, DelegatingLogSinkSharedPtr log_sink) | ||
: SinkDelegate(log_sink), logger_(logger) { | ||
setDelegate(); | ||
} | ||
|
||
LambdaDelegate::~LambdaDelegate() { restoreDelegate(); } | ||
|
||
void LambdaDelegate::log(absl::string_view msg) { | ||
logger_.log(Data::Utility::copyToBridgeData(msg), logger_.context); | ||
} | ||
|
||
} // namespace Logger | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
#include "common/common/logger.h" | ||
|
||
#include "absl/strings/string_view.h" | ||
#include "library/common/types/c_types.h" | ||
|
||
namespace Envoy { | ||
namespace Logger { | ||
|
||
class LambdaDelegate : public SinkDelegate { | ||
public: | ||
LambdaDelegate(envoy_logger logger, DelegatingLogSinkSharedPtr log_sink); | ||
~LambdaDelegate() override; | ||
|
||
// SinkDelegate | ||
void log(absl::string_view msg) override; | ||
// Currently unexposed. May be desired in the future. | ||
void flush() override{}; | ||
|
||
private: | ||
envoy_logger logger_; | ||
}; | ||
|
||
using LambdaDelegatePtr = std::unique_ptr<LambdaDelegate>; | ||
|
||
} // namespace Logger | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
load("@envoy//bazel:envoy_build_system.bzl", "envoy_cc_test", "envoy_package") | ||
|
||
licenses(["notice"]) # Apache 2 | ||
|
||
envoy_package() | ||
|
||
envoy_cc_test( | ||
name = "lambda_logger_delegate_test", | ||
srcs = ["lambda_logger_delegate_test.cc"], | ||
repository = "@envoy", | ||
deps = [ | ||
"//library/common/common:lambda_logger_delegate_lib", | ||
"//library/common/data:utility_lib", | ||
"//library/common/types:c_types_lib", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "library/common/common/lambda_logger_delegate.h" | ||
#include "library/common/data/utility.h" | ||
|
||
using testing::_; | ||
using testing::HasSubstr; | ||
|
||
namespace Envoy { | ||
namespace Logger { | ||
|
||
TEST(LambdaDelegate, LogCb) { | ||
std::string expected_msg = "Hello LambdaDelegate"; | ||
std::string actual_msg; | ||
|
||
LambdaDelegate delegate = LambdaDelegate({[](envoy_data data, void* context) -> void { | ||
auto* actual_msg = static_cast<std::string*>(context); | ||
*actual_msg = Data::Utility::copyToString(data); | ||
data.release(data.context); | ||
}, | ||
&actual_msg}, | ||
Registry::getSink()); | ||
|
||
ENVOY_LOG_MISC(error, expected_msg); | ||
EXPECT_THAT(actual_msg, HasSubstr(expected_msg)); | ||
} | ||
|
||
} // namespace Logger | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters