logging: improve the AsyncFileWriter flush test()
[folly.git] / folly / experimental / logging / test / StandardLogHandlerTest.cpp
1 /*
2  * Copyright 2004-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/Conv.h>
17 #include <folly/experimental/logging/LogCategory.h>
18 #include <folly/experimental/logging/LogFormatter.h>
19 #include <folly/experimental/logging/LogLevel.h>
20 #include <folly/experimental/logging/LogMessage.h>
21 #include <folly/experimental/logging/LogWriter.h>
22 #include <folly/experimental/logging/LoggerDB.h>
23 #include <folly/experimental/logging/StandardLogHandler.h>
24 #include <folly/portability/GTest.h>
25
26 using namespace folly;
27 using std::make_shared;
28
29 namespace {
30 class TestLogFormatter : public LogFormatter {
31  public:
32   std::string formatMessage(
33       const LogMessage& message,
34       const LogCategory* handlerCategory) override {
35     return folly::to<std::string>(
36         logLevelToString(message.getLevel()),
37         "::",
38         message.getCategory()->getName(),
39         "::",
40         handlerCategory->getName(),
41         "::",
42         message.getFileName(),
43         "::",
44         message.getLineNumber(),
45         "::",
46         message.getMessage());
47   }
48 };
49
50 class TestLogWriter : public LogWriter {
51  public:
52   void writeMessage(folly::StringPiece buffer, uint32_t /* flags */ = 0)
53       override {
54     messages_.emplace_back(buffer.str());
55   }
56   void flush() override {}
57
58   std::vector<std::string>& getMessages() {
59     return messages_;
60   }
61   const std::vector<std::string>& getMessages() const {
62     return messages_;
63   }
64
65  private:
66   std::vector<std::string> messages_;
67 };
68 }
69
70 TEST(StandardLogHandler, simple) {
71   auto writer = make_shared<TestLogWriter>();
72   StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
73
74   LoggerDB db{LoggerDB::TESTING};
75   auto logCategory = db.getCategory("log_cat");
76   auto handlerCategory = db.getCategory("handler_cat");
77
78   LogMessage msg{logCategory,
79                  LogLevel::DBG8,
80                  "src/test.cpp",
81                  1234,
82                  std::string{"hello world"}};
83   handler.handleMessage(msg, handlerCategory);
84   ASSERT_EQ(1, writer->getMessages().size());
85   EXPECT_EQ(
86       "DBG8::log_cat::handler_cat::src/test.cpp::1234::hello world",
87       writer->getMessages()[0]);
88 }
89
90 TEST(StandardLogHandler, levelCheck) {
91   auto writer = make_shared<TestLogWriter>();
92   StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
93
94   LoggerDB db{LoggerDB::TESTING};
95   auto logCategory = db.getCategory("log_cat");
96   auto handlerCategory = db.getCategory("handler_cat");
97
98   auto logMsg = [&](LogLevel level, folly::StringPiece message) {
99     LogMessage msg{logCategory, level, "src/test.cpp", 1234, message};
100     handler.handleMessage(msg, handlerCategory);
101   };
102
103   handler.setLevel(LogLevel::WARN);
104   logMsg(LogLevel::INFO, "info");
105   logMsg(LogLevel::WARN, "beware");
106   logMsg(LogLevel::ERR, "whoops");
107   logMsg(LogLevel::DBG1, "debug stuff");
108
109   auto& messages = writer->getMessages();
110   ASSERT_EQ(2, messages.size());
111   EXPECT_EQ(
112       "WARN::log_cat::handler_cat::src/test.cpp::1234::beware", messages.at(0));
113   EXPECT_EQ(
114       "ERR::log_cat::handler_cat::src/test.cpp::1234::whoops", messages.at(1));
115   messages.clear();
116
117   handler.setLevel(LogLevel::DBG2);
118   logMsg(LogLevel::DBG3, "dbg");
119   logMsg(LogLevel::DBG1, "here");
120   logMsg(LogLevel::DBG2, "and here");
121   logMsg(LogLevel::ERR, "oh noes");
122   logMsg(LogLevel::DBG9, "very verbose");
123
124   ASSERT_EQ(3, messages.size());
125   EXPECT_EQ(
126       "DBG1::log_cat::handler_cat::src/test.cpp::1234::here", messages.at(0));
127   EXPECT_EQ(
128       "DBG2::log_cat::handler_cat::src/test.cpp::1234::and here",
129       messages.at(1));
130   EXPECT_EQ(
131       "ERR::log_cat::handler_cat::src/test.cpp::1234::oh noes", messages.at(2));
132   messages.clear();
133 }