Skip to content

Conversation

cpakulski
Copy link
Contributor

Modified main logging macro to compare logger's severity with severity of the message to be logged. If message severity is lower, logger is not called and code passed as params is not evaluated.

…age severity.

Code passed to logger as params will NOT be evaluated if message's severity is lower than logger's severity.

Signed-off-by: Christoph Pakulski <paker8848@gmail.com>
@alyssawilk alyssawilk requested a review from mrice32 February 26, 2018 21:41
Copy link
Member

@ggreenway ggreenway left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a nice improvement.

The release build tests are failing with this.

// log message with higher severity and make sure that params were evaluated.
GET_MISC_LOGGER().set_level(spdlog::level::info);
ENVOY_LOG_MISC(warn, "test message '{}'", i++);
ASSERT_THAT(i, testing::Eq(2));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPECT_EQ(i, 2);

// params should not be evaluated.
GET_MISC_LOGGER().set_level(spdlog::level::critical);
ENVOY_LOG_MISC(error, "test message '{}'", i++);
ASSERT_THAT(i, testing::Eq(1));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EXPECT_EQ(i, 1);

@@ -37,4 +37,24 @@ TEST(Logger, All) {
// Misc logging with no facility.
ENVOY_LOG_MISC(info, "fake message");
}

TEST(Logger, evaluateParams) {
auto i = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give this a type: uint32_t i = 1;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Will change that to uint32_t. Thanks.

}

TEST(Logger, doNotEvaluateParams) {
auto i = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please give this a type.

Copy link
Member

@mrice32 mrice32 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks for adding this! Just one comment about the macro.

@@ -192,7 +207,10 @@ template <Id id> class Loggable {
/**
* Convenience macro to log to a user-specified logger.
*/
#define ENVOY_LOG_TO_LOGGER(LOGGER, LEVEL, ...) ENVOY_LOG_##LEVEL##_TO_LOGGER(LOGGER, ##__VA_ARGS__)
#define ENVOY_LOG_TO_LOGGER(LOGGER, LEVEL, ...) \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a test for this? I think this may suffer from the not-a-statement macro issue (see here), but I'm not sure. Specifically, I'm worried about the case:

if (condition) ENVOY_LOG_TO_LOGGER(LOGGER, LEVEL, ...);
else f();

This may be the reason that the behavior at the callsite in source/common/http/filter/lua/lua_filter.cc needed to be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapped macro into do ... while(0) based on the link you sent (thanks!).
I had to keep changes for source/common/http/filter/lua/lua_filter.cc.
spdlog::log ... returns void and statement
return ENVOY_LOG...
was an easy way to log and exit function in one step. It will not work with the new macro.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM, thanks!

Added test to verify that the macro can be used inside if .. else ...

Signed-off-by: Christoph Pakulski <paker8848@gmail.com>
@ggreenway ggreenway self-assigned this Feb 27, 2018
ggreenway
ggreenway previously approved these changes Feb 27, 2018
Copy link
Member

@mrice32 mrice32 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@alyssawilk
Copy link
Contributor

I'd be happy to merge today but it looks like the tests are still failing?

@cpakulski
Copy link
Contributor Author

I am working on fixing the problem with CI tests. When ASSERT is used not in Envoy namespace compiler cannot find certain values.

@alyssawilk
Copy link
Contributor

@cpakulski one other small request - would you mind editing your original description to match the pull request template? It's helpful for when we commit to have a standard format. thanks!

https://github.com/envoyproxy/envoy/blob/master/PULL_REQUEST_TEMPLATE.md

@cpakulski
Copy link
Contributor Author

@alyssawilk How to run all CI tests on developer's machine? Just want to make sure that it all passes before PR. No problem - I will update description to match the template.

@alyssawilk
Copy link
Contributor

I don't think there's a single command to run all of them but for any build you can do something like
./ci/run_envoy_docker.sh ./ci/do_ci.sh bazel.asan
(check out do_ci for the list of other builds)

@cpakulski cpakulski changed the title Fix for issue 2330. Macros evaluate logging level before invoking logger logger: macros evaluate logging level before invoking logger Feb 28, 2018
@cpakulski
Copy link
Contributor Author

Description: logger's current severity level is checked before calling logger's API. This avoids evaluating parameters passed to the logger when message severity is lower than logger's setting. Before this change, params were evaluated even when the message was later on dropped by logger.
Risk Level: Medium. This is simple fix, but the code is extensively used across the project.
Testing: unit tests: added 3 tests to test/common/common/log_macros_test.cc
Docs Changes: N/A
Release Notes: N/A

…nvoy namespace.

Signed-off-by: christoph <paker8848@gmail.com>
@@ -7,6 +7,8 @@
#include "absl/strings/string_view.h"
#include "testing/base/public/benchmark.h"

using namespace Envoy;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this code be put into namespace Envoy, instead of adding a "using"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see two options:

  • move test code into Envoy namespace (most test code is already in that namespace - only few tests run in default ns)
  • change MACRO to use FQ namespace Envoy::Logger::Logger instead of Logger::Logger.

I think that first option is a bit cleaner - will try to do that and will ask you to review it. Thanks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with either of those solutions.

…:Logger.

There are some benchmark tests which invoke logger from default namespace.

Signed-off-by: Christoph Pakulski <paker8848@gmail.com>
ggreenway
ggreenway previously approved these changes Mar 2, 2018
@ggreenway
Copy link
Member

This looks good, but somehow you've angered the DCO-bot. I think it's because your DCO-sign-off doesn't match the name in your git commit author. I've never seen that error before.

@cpakulski
Copy link
Contributor Author

@ggreenway Somehow I messed it up - do not know exactly how. I have fixed it in local repo and will push --force to overwrite the latest commit. Just waiting for other circleci tests to finish.

@ggreenway ggreenway merged commit 8078092 into envoyproxy:master Mar 2, 2018
@junr03
Copy link
Member

junr03 commented Mar 5, 2018

@cpakulski this PR causes compilation failures when running with -DNVLOG because it does not fence evaluating the LOGGER arg behind the appropriate #idnfef. One place that this surfaces is in the connection handler, where the logger instance variable is fenced in https://github.com/envoyproxy/envoy/blob/master/source/server/connection_handler_impl.h#L163-L165, and thus is not present when the macro is evaluated, when you call LOGGER.level() here: https://github.com/envoyproxy/envoy/pull/2676/files#diff-a5a6b6a3e1f886a6a49364ede0758acbR212

This used to not be a problem because the first use of the LOGGER argument in the macro (before this PR) is fenced appropriately:

#ifdef NVLOG
#define ENVOY_LOG_trace_TO_LOGGER(LOGGER, ...)
#define ENVOY_LOG_debug_TO_LOGGER(LOGGER, ...)
#else
#define ENVOY_LOG_trace_TO_LOGGER(LOGGER, ...) LOGGER.trace(LOG_PREFIX __VA_ARGS__)
#define ENVOY_LOG_debug_TO_LOGGER(LOGGER, ...) LOGGER.debug(LOG_PREFIX __VA_ARGS__)
#endif

I will leave it up to @lita (who is trying to get a deploy out at Lyft) if she wants to revert. And to coordinate with you @cpakulski

cc @alyssawilk @ggreenway

@lita
Copy link

lita commented Mar 5, 2018

Yea I am going to have to revert, as I am not able to quickly fix this myself. Feel free to submit another PR with the fix. Thanks!

@lita
Copy link

lita commented Mar 5, 2018

Actually, rather than revert, I will merge lyft's envoy to the change before yours. If you can upstream the fix before Thursday, then there will be no need to revert.

@lita
Copy link

lita commented Mar 5, 2018

After further discussion with @ccaraman, we are going to revert this PR.

lita pushed a commit to lita/envoy that referenced this pull request Mar 5, 2018
…nvoyproxy#2676)"

This reverts commit 8078092.

Signed-off-by: Lita Cho <lcho@lyft.com>
htuch pushed a commit that referenced this pull request Mar 6, 2018
…2676)" (#2730)

This reverts commit 8078092.

Signed-off-by: Lita Cho <lcho@lyft.com>
jpsim pushed a commit that referenced this pull request Nov 28, 2022
Signed-off-by: Alyssa Wilk <alyssar@chromium.org>
Signed-off-by: JP Simard <jp@jpsim.com>
jpsim pushed a commit that referenced this pull request Nov 29, 2022
Signed-off-by: Alyssa Wilk <alyssar@chromium.org>
Signed-off-by: JP Simard <jp@jpsim.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants