SyncLevelTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/test/TestUtils.h>
  17. #include <queue>
  18. #include <folly/logging/Init.h>
  19. #include <folly/logging/LogConfigParser.h>
  20. #include <folly/logging/LogHandlerFactory.h>
  21. #include <folly/logging/LogWriter.h>
  22. #include <folly/logging/LoggerDB.h>
  23. #include <folly/logging/StandardLogHandler.h>
  24. #include <folly/logging/StandardLogHandlerFactory.h>
  25. #include <folly/logging/xlog.h>
  26. namespace folly {
  27. class TestLogWriter : public LogWriter {
  28. public:
  29. void writeMessage(folly::StringPiece /* buffer */, uint32_t /* flags */ = 0)
  30. override {
  31. unflushed_messages_count++;
  32. }
  33. void flush() override {
  34. flushed_messages_count += unflushed_messages_count;
  35. unflushed_messages_count = 0;
  36. }
  37. int flushed_messages_count{0};
  38. int unflushed_messages_count{0};
  39. bool ttyOutput() const override {
  40. return false;
  41. }
  42. };
  43. class TestHandlerFactory : public LogHandlerFactory {
  44. public:
  45. TestHandlerFactory(const std::shared_ptr<TestLogWriter> writer)
  46. : writer_(writer) {}
  47. StringPiece getType() const override {
  48. return "test";
  49. }
  50. std::shared_ptr<LogHandler> createHandler(const Options& options) override {
  51. TestWriterFactory writerFactory{writer_};
  52. return StandardLogHandlerFactory::createHandler(
  53. getType(), &writerFactory, options);
  54. }
  55. private:
  56. std::shared_ptr<TestLogWriter> writer_;
  57. class TestWriterFactory : public StandardLogHandlerFactory::WriterFactory {
  58. public:
  59. TestWriterFactory(std::shared_ptr<TestLogWriter> writer)
  60. : writer_(writer) {}
  61. bool processOption(StringPiece /* name */, StringPiece /* value */)
  62. override {
  63. return false;
  64. }
  65. std::shared_ptr<LogWriter> createWriter() override {
  66. return writer_;
  67. }
  68. private:
  69. std::shared_ptr<TestLogWriter> writer_;
  70. };
  71. };
  72. } // namespace folly
  73. using namespace folly;
  74. namespace {
  75. class SyncLevelTest : public testing::Test {
  76. public:
  77. SyncLevelTest() {
  78. writer = std::make_shared<TestLogWriter>();
  79. db.registerHandlerFactory(
  80. std::make_unique<TestHandlerFactory>(writer), true);
  81. db.resetConfig(
  82. parseLogConfig("test=INFO:default; default=test:sync_level=WARN"));
  83. }
  84. LoggerDB db{LoggerDB::TESTING};
  85. Logger logger{&db, "test"};
  86. std::shared_ptr<TestLogWriter> writer;
  87. };
  88. } // namespace
  89. TEST_F(SyncLevelTest, NoLogTest) {
  90. FB_LOG(logger, DBG9) << "DBG9";
  91. FB_LOG(logger, DBG1) << "DBG1";
  92. FB_LOG(logger, DBG0) << "DBG0";
  93. EXPECT_EQ(writer->unflushed_messages_count, 0);
  94. EXPECT_EQ(writer->flushed_messages_count, 0);
  95. }
  96. TEST_F(SyncLevelTest, SimpleAsyncTest) {
  97. FB_LOG(logger, INFO) << "INFO";
  98. FB_LOG(logger, INFO) << "INFO";
  99. FB_LOG(logger, INFO) << "INFO";
  100. FB_LOG(logger, INFO) << "INFO";
  101. EXPECT_EQ(writer->unflushed_messages_count, 4);
  102. EXPECT_EQ(writer->flushed_messages_count, 0);
  103. FB_LOG(logger, WARN) << "WARN";
  104. EXPECT_EQ(writer->unflushed_messages_count, 0);
  105. EXPECT_EQ(writer->flushed_messages_count, 5);
  106. FB_LOG(logger, DBG0) << "DBG0";
  107. FB_LOG(logger, INFO9) << "INFO9";
  108. EXPECT_EQ(writer->unflushed_messages_count, 1);
  109. EXPECT_EQ(writer->flushed_messages_count, 5);
  110. FB_LOG(logger, INFO) << "INFO";
  111. FB_LOG(logger, WARN) << "WARN";
  112. EXPECT_EQ(writer->unflushed_messages_count, 0);
  113. EXPECT_EQ(writer->flushed_messages_count, 8);
  114. FB_LOG(logger, INFO) << "INFO";
  115. FB_LOG(logger, CRITICAL) << "CRITICAL";
  116. EXPECT_EQ(writer->unflushed_messages_count, 0);
  117. EXPECT_EQ(writer->flushed_messages_count, 10);
  118. }