logging: add a NEVER_DISCARD flag to LogWriter
[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
57   std::vector<std::string>& getMessages() {
58     return messages_;
59   }
60   const std::vector<std::string>& getMessages() const {
61     return messages_;
62   }
63
64  private:
65   std::vector<std::string> messages_;
66 };
67 }
68
69 TEST(StandardLogHandler, simple) {
70   auto writer = make_shared<TestLogWriter>();
71   StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
72
73   LoggerDB db{LoggerDB::TESTING};
74   auto logCategory = db.getCategory("log_cat");
75   auto handlerCategory = db.getCategory("handler_cat");
76
77   LogMessage msg{logCategory,
78                  LogLevel::DBG8,
79                  "src/test.cpp",
80                  1234,
81                  std::string{"hello world"}};
82   handler.handleMessage(msg, handlerCategory);
83   ASSERT_EQ(1, writer->getMessages().size());
84   EXPECT_EQ(
85       "LogLevel::DBG8::log_cat::handler_cat::src/test.cpp::1234::hello world",
86       writer->getMessages()[0]);
87 }
88
89 TEST(StandardLogHandler, levelCheck) {
90   auto writer = make_shared<TestLogWriter>();
91   StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
92
93   LoggerDB db{LoggerDB::TESTING};
94   auto logCategory = db.getCategory("log_cat");
95   auto handlerCategory = db.getCategory("handler_cat");
96
97   auto logMsg = [&](LogLevel level, folly::StringPiece message) {
98     LogMessage msg{logCategory, level, "src/test.cpp", 1234, message};
99     handler.handleMessage(msg, handlerCategory);
100   };
101
102   handler.setLevel(LogLevel::WARN);
103   logMsg(LogLevel::INFO, "info");
104   logMsg(LogLevel::WARN, "beware");
105   logMsg(LogLevel::ERR, "whoops");
106   logMsg(LogLevel::DBG1, "debug stuff");
107
108   auto& messages = writer->getMessages();
109   ASSERT_EQ(2, messages.size());
110   EXPECT_EQ(
111       "LogLevel::WARN::log_cat::handler_cat::src/test.cpp::1234::beware",
112       messages.at(0));
113   EXPECT_EQ(
114       "LogLevel::ERR::log_cat::handler_cat::src/test.cpp::1234::whoops",
115       messages.at(1));
116   messages.clear();
117
118   handler.setLevel(LogLevel::DBG2);
119   logMsg(LogLevel::DBG3, "dbg");
120   logMsg(LogLevel::DBG1, "here");
121   logMsg(LogLevel::DBG2, "and here");
122   logMsg(LogLevel::ERR, "oh noes");
123   logMsg(LogLevel::DBG9, "very verbose");
124
125   ASSERT_EQ(3, messages.size());
126   EXPECT_EQ(
127       "LogLevel::DBG1::log_cat::handler_cat::src/test.cpp::1234::here",
128       messages.at(0));
129   EXPECT_EQ(
130       "LogLevel::DBG2::log_cat::handler_cat::src/test.cpp::1234::and here",
131       messages.at(1));
132   EXPECT_EQ(
133       "LogLevel::ERR::log_cat::handler_cat::src/test.cpp::1234::oh noes",
134       messages.at(2));
135   messages.clear();
136 }