proxygen
StandardLogHandlerTest.cpp
Go to the documentation of this file.
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  */
17 
18 #include <folly/Conv.h>
22 #include <folly/logging/LogLevel.h>
25 #include <folly/logging/LoggerDB.h>
27 
28 using namespace folly;
29 using std::make_shared;
30 
31 namespace {
32 class TestLogFormatter : public LogFormatter {
33  public:
34  std::string formatMessage(
35  const LogMessage& message,
36  const LogCategory* handlerCategory) override {
37  return folly::to<std::string>(
38  logLevelToString(message.getLevel()),
39  "::",
40  message.getCategory()->getName(),
41  "::",
42  handlerCategory->getName(),
43  "::",
44  message.getFileName(),
45  "::",
46  message.getLineNumber(),
47  "::",
48  message.getMessage());
49  }
50 };
51 
52 class TestLogWriter : public LogWriter {
53  public:
54  void writeMessage(folly::StringPiece buffer, uint32_t /* flags */ = 0)
55  override {
56  messages_.emplace_back(buffer.str());
57  }
58  void flush() override {}
59 
60  std::vector<std::string>& getMessages() {
61  return messages_;
62  }
63  const std::vector<std::string>& getMessages() const {
64  return messages_;
65  }
66 
67  bool ttyOutput() const override {
68  return false;
69  }
70 
71  private:
72  std::vector<std::string> messages_;
73 };
74 } // namespace
75 
77  auto writer = make_shared<TestLogWriter>();
78  LogHandlerConfig config{"std_test"};
79  StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
80 
82  auto logCategory = db.getCategory("log_cat");
83  auto handlerCategory = db.getCategory("handler_cat");
84 
85  LogMessage msg{logCategory,
87  "src/test.cpp",
88  1234,
89  "testMethod",
90  std::string{"hello world"}};
91  handler.handleMessage(msg, handlerCategory);
92  ASSERT_EQ(1, writer->getMessages().size());
93  EXPECT_EQ(
94  "DBG8::log_cat::handler_cat::src/test.cpp::1234::hello world",
95  writer->getMessages()[0]);
96 }
97 
98 TEST(StandardLogHandler, levelCheck) {
99  auto writer = make_shared<TestLogWriter>();
100  LogHandlerConfig config{"std_test"};
101  StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
102 
104  auto logCategory = db.getCategory("log_cat");
105  auto handlerCategory = db.getCategory("handler_cat");
106 
107  auto logMsg = [&](LogLevel level, folly::StringPiece message) {
108  LogMessage msg{
109  logCategory, level, "src/test.cpp", 1234, "testMethod", message};
110  handler.handleMessage(msg, handlerCategory);
111  };
112 
113  handler.setLevel(LogLevel::WARN);
114  logMsg(LogLevel::INFO, "info");
115  logMsg(LogLevel::WARN, "beware");
116  logMsg(LogLevel::ERR, "whoops");
117  logMsg(LogLevel::DBG1, "debug stuff");
118 
119  auto& messages = writer->getMessages();
120  ASSERT_EQ(2, messages.size());
121  EXPECT_EQ(
122  "WARN::log_cat::handler_cat::src/test.cpp::1234::beware", messages.at(0));
123  EXPECT_EQ(
124  "ERR::log_cat::handler_cat::src/test.cpp::1234::whoops", messages.at(1));
125  messages.clear();
126 
127  handler.setLevel(LogLevel::DBG2);
128  logMsg(LogLevel::DBG3, "dbg");
129  logMsg(LogLevel::DBG1, "here");
130  logMsg(LogLevel::DBG2, "and here");
131  logMsg(LogLevel::ERR, "oh noes");
132  logMsg(LogLevel::DBG9, "very verbose");
133 
134  ASSERT_EQ(3, messages.size());
135  EXPECT_EQ(
136  "DBG1::log_cat::handler_cat::src/test.cpp::1234::here", messages.at(0));
137  EXPECT_EQ(
138  "DBG2::log_cat::handler_cat::src/test.cpp::1234::and here",
139  messages.at(1));
140  EXPECT_EQ(
141  "ERR::log_cat::handler_cat::src/test.cpp::1234::oh noes", messages.at(2));
142  messages.clear();
143 }
static struct message messages[5]
Definition: test.c:75
std::vector< uint8_t > buffer(kBufferSize+16)
Definition: test.c:42
void handleMessage(const LogMessage &message, const LogCategory *handlerCategory) override
std::string str() const
Definition: Range.h:591
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static bool simple
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
string logLevelToString(LogLevel level)
Definition: LogLevel.cpp:109
AHArrayT::Config config
void handler(int, siginfo_t *, void *)
const std::string & getMessage() const
Definition: LogMessage.h:105
LogLevel getLevel() const
Definition: LogMessage.h:80
void setLevel(LogLevel level)
folly::StringPiece getFileName() const
Definition: LogMessage.h:84
std::string message
Definition: SPDYCodec.cpp:133
const std::string & getName() const
Definition: LogCategory.h:66
LogLevel
Definition: LogLevel.h:38
unsigned int getLineNumber() const
Definition: LogMessage.h:89
const char * string
Definition: Conv.cpp:212
const LogCategory * getCategory() const
Definition: LogMessage.h:76
TEST(SequencedExecutor, CPUThreadPoolExecutor)