PrintfTest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <folly/logging/printf.h>
  17. #include <folly/logging/test/TestLogHandler.h>
  18. #include <folly/portability/GTest.h>
  19. using namespace folly;
  20. using std::make_shared;
  21. TEST(PrintfTest, printfStyleMacros) {
  22. LoggerDB db{LoggerDB::TESTING};
  23. Logger logger{&db, "test"};
  24. auto* category = logger.getCategory();
  25. auto handler = make_shared<TestLogHandler>();
  26. category->addHandler(handler);
  27. category->setLevel(LogLevel::DBG, true);
  28. Logger foo{&db, "test.foo.bar"};
  29. Logger foobar{&db, "test.foo.bar"};
  30. Logger footest{&db, "test.foo.test"};
  31. Logger footest1234{&db, "test.foo.test.1234"};
  32. Logger other{&db, "test.other"};
  33. db.setLevel("test", LogLevel::ERR);
  34. db.setLevel("test.foo", LogLevel::DBG2);
  35. db.setLevel("test.foo.test", LogLevel::DBG7);
  36. auto& messages = handler->getMessages();
  37. // test.other's effective level should be INFO, so a DBG0
  38. // message to it should be discarded
  39. FB_LOGC(other, DBG0, "this should be discarded: %d", 5);
  40. ASSERT_EQ(0, messages.size());
  41. // Disabled log messages should not evaluate their arguments
  42. bool argumentEvaluated = false;
  43. auto getValue = [&] {
  44. argumentEvaluated = true;
  45. return 5;
  46. };
  47. FB_LOGC(foobar, DBG3, "discarded message: %d", getValue());
  48. EXPECT_FALSE(argumentEvaluated);
  49. FB_LOGC(foobar, DBG1, "this message should pass: %d", getValue());
  50. ASSERT_EQ(1, messages.size());
  51. EXPECT_EQ("this message should pass: 5", messages[0].first.getMessage());
  52. EXPECT_TRUE(argumentEvaluated);
  53. messages.clear();
  54. // The FB_LOGC() macro should work even if the format string does not contain
  55. // any format sequences. Ideally people would just use FB_LOG() if they
  56. // aren't actually formatting anything, but making FB_LOGC() work in this
  57. // scenario still makes it easier for people to switch legacy printf-style
  58. // code to FB_LOGC().
  59. FB_LOGC(foobar, DBG1, "no actual format arguments");
  60. ASSERT_EQ(1, messages.size());
  61. EXPECT_EQ("no actual format arguments", messages[0].first.getMessage());
  62. messages.clear();
  63. // Similar checks with XLOGC()
  64. auto* xlogCategory = XLOG_GET_CATEGORY();
  65. xlogCategory->addHandler(handler);
  66. xlogCategory->setLevel(LogLevel::DBG5, true);
  67. argumentEvaluated = false;
  68. XLOGC(DBG9, "failing log check: %d", getValue());
  69. EXPECT_FALSE(argumentEvaluated);
  70. XLOGC(DBG5, "passing log: %03d", getValue());
  71. ASSERT_EQ(1, messages.size());
  72. EXPECT_EQ("passing log: 005", messages[0].first.getMessage());
  73. EXPECT_TRUE(argumentEvaluated);
  74. messages.clear();
  75. XLOGC(DBG1, "no xlog format arguments");
  76. ASSERT_EQ(1, messages.size());
  77. EXPECT_EQ("no xlog format arguments", messages[0].first.getMessage());
  78. messages.clear();
  79. XLOGC_IF(DBG1, false, "no xlog format arguments");
  80. ASSERT_EQ(0, messages.size());
  81. XLOGC_IF(DBG1, true, "xlog format arguments");
  82. ASSERT_EQ(1, messages.size());
  83. messages.clear();
  84. argumentEvaluated = false;
  85. XLOGC_IF(DBG1, true, "xlog format string %d", getValue());
  86. ASSERT_EQ(1, messages.size());
  87. EXPECT_TRUE(argumentEvaluated);
  88. messages.clear();
  89. argumentEvaluated = false;
  90. XLOGC_IF(DBG1, false, "xlog format string %d", getValue());
  91. ASSERT_EQ(0, messages.size());
  92. EXPECT_FALSE(argumentEvaluated);
  93. messages.clear();
  94. // more complex conditional expressions
  95. std::array<bool, 2> conds = {{false, true}};
  96. for (unsigned i = 0; i < conds.size(); i++) {
  97. for (unsigned j = 0; j < conds.size(); j++) {
  98. argumentEvaluated = false;
  99. XLOGC_IF(
  100. DBG1, conds[i] && conds[j], "testing conditional %d", getValue());
  101. EXPECT_EQ((conds[i] && conds[j]) ? 1 : 0, messages.size());
  102. messages.clear();
  103. if (conds[i] && conds[j]) {
  104. EXPECT_TRUE(argumentEvaluated);
  105. } else {
  106. EXPECT_FALSE(argumentEvaluated);
  107. }
  108. argumentEvaluated = false;
  109. XLOGC_IF(
  110. DBG1, conds[i] || conds[j], "testing conditional %d", getValue());
  111. EXPECT_EQ((conds[i] || conds[j]) ? 1 : 0, messages.size());
  112. messages.clear();
  113. if (conds[i] || conds[j]) {
  114. EXPECT_TRUE(argumentEvaluated);
  115. } else {
  116. EXPECT_FALSE(argumentEvaluated);
  117. }
  118. }
  119. }
  120. XLOGC_IF(DBG1, 0x6 & 0x2, "More conditional 1");
  121. EXPECT_EQ(1, messages.size());
  122. messages.clear();
  123. XLOGC_IF(DBG1, 0x6 | 0x2, "More conditional 2");
  124. EXPECT_EQ(1, messages.size());
  125. messages.clear();
  126. XLOGC_IF(DBG1, 0x6 | 0x2 ? true : false, "More conditional 3");
  127. EXPECT_EQ(1, messages.size());
  128. messages.clear();
  129. XLOGC_IF(DBG1, 0x6 | 0x2 ? true : false, "More conditional 3");
  130. EXPECT_EQ(1, messages.size());
  131. messages.clear();
  132. XLOGC_IF(DBG1, 0x3 & 0x4 ? true : false, "More conditional 4");
  133. EXPECT_EQ(0, messages.size());
  134. messages.clear();
  135. XLOGC_IF(DBG1, false ? true : false, "More conditional 5");
  136. EXPECT_EQ(0, messages.size());
  137. messages.clear();
  138. // Errors attempting to format the message should not throw
  139. FB_LOGC(footest1234, ERR, "width overflow: %999999999999999999999d", 5);
  140. ASSERT_EQ(1, messages.size());
  141. EXPECT_EQ(
  142. "error formatting printf-style log message: "
  143. "width overflow: %999999999999999999999d",
  144. messages[0].first.getMessage());
  145. messages.clear();
  146. }