Skip to content

Commit a10c0d1

Browse files
otmeta-codesync[bot]
authored andcommitted
Add test on conditional evaluation of the message
Summary: Avoiding evaluation of the message expression is an important feature, since it may be expensive while being almost always disabled, however this guarantee is not specified anywhere and the way that the code ensures this is a bit subtle, so add a test to make sure we don't regress on this. Reviewed By: skrueger Differential Revision: D90598684 fbshipit-source-id: 11f41246c1ab313aebaca2a8b2e047899ce5ec04
1 parent 4e7e207 commit a10c0d1

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

third-party/folly/src/folly/logging/test/XlogTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,27 @@ TEST_F(XlogTest, xlogIf) {
151151
XLOGF_IF(DBG1, true, "plain format string");
152152
ASSERT_EQ(1, messages.size());
153153
messages.clear();
154+
155+
// If the log is not enabled (false condition or below current level) the
156+
// message expression should not be evaluated.
157+
bool called = false;
158+
const auto call = [&] {
159+
called = true;
160+
return "called";
161+
};
162+
163+
XLOG_IF(INFO, false) << call();
164+
EXPECT_FALSE(called);
165+
XLOGF_IF(INFO, false, "{}", call());
166+
EXPECT_FALSE(called);
167+
168+
XLOG(DBG2) << call();
169+
EXPECT_FALSE(called);
170+
XLOGF(DBG2, "{}", call());
171+
EXPECT_FALSE(called);
172+
173+
XLOG(INFO) << call();
174+
EXPECT_TRUE(called);
154175
}
155176

156177
TEST_F(XlogTest, xlog) {

0 commit comments

Comments
 (0)