2 * Copyright 2004-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <folly/experimental/logging/LogCategory.h>
17 #include <folly/experimental/logging/LogHandler.h>
18 #include <folly/experimental/logging/LogMessage.h>
19 #include <folly/experimental/logging/LoggerDB.h>
20 #include <folly/experimental/logging/test/TestLogHandler.h>
21 #include <folly/experimental/logging/test/XlogHeader1.h>
22 #include <folly/experimental/logging/test/XlogHeader2.h>
23 #include <folly/experimental/logging/xlog.h>
24 #include <folly/portability/GTest.h>
26 using namespace folly;
27 using std::make_shared;
29 XLOG_SET_CATEGORY("xlog_test.main_file");
31 // Note that the XLOG* macros always use the main LoggerDB singleton.
32 // There is no way to get them to use a test LoggerDB during unit tests.
34 // Therefore any configuration we do here affects the main log category
35 // settings for the entire program. Fortunately all of the other unit tests do
36 // use testing LoggerDB objects.
39 auto handler = make_shared<TestLogHandler>();
40 LoggerDB::get()->getCategory("xlog_test")->addHandler(handler);
41 auto& messages = handler->getMessages();
43 // info messages are not enabled initially.
44 EXPECT_FALSE(XLOG_IS_ON(INFO));
45 EXPECT_TRUE(XLOG_IS_ON(ERR));
46 XLOG(INFO, "testing 1");
47 EXPECT_EQ(0, messages.size());
50 // Increase the log level, then log a message.
51 LoggerDB::get()->setLevel("xlog_test.main_file", LogLevel::DBG1);
53 XLOG(DBG1, "testing: ", 1, 2, 3);
54 ASSERT_EQ(1, messages.size());
55 EXPECT_EQ("testing: 123", messages[0].first.getMessage());
56 EXPECT_TRUE(messages[0].first.getFileName().endsWith("XlogTest.cpp"))
57 << "unexpected file name: " << messages[0].first.getFileName();
58 EXPECT_EQ(LogLevel::DBG1, messages[0].first.getLevel());
59 EXPECT_EQ("xlog_test.main_file", messages[0].first.getCategory()->getName());
60 EXPECT_EQ("xlog_test", messages[0].second->getName());
63 XLOGF(WARN, "number: {:>3d}; string: {}", 12, "foo");
64 ASSERT_EQ(1, messages.size());
65 EXPECT_EQ("number: 12; string: foo", messages[0].first.getMessage());
66 EXPECT_TRUE(messages[0].first.getFileName().endsWith("XlogTest.cpp"))
67 << "unexpected file name: " << messages[0].first.getFileName();
68 EXPECT_EQ(LogLevel::WARN, messages[0].first.getLevel());
69 EXPECT_EQ("xlog_test.main_file", messages[0].first.getCategory()->getName());
70 EXPECT_EQ("xlog_test", messages[0].second->getName());
73 XLOG(DBG2, "this log check should not pass");
74 EXPECT_EQ(0, messages.size());
77 // Test stream arguments to XLOG()
78 XLOG(INFO) << "stream test: " << 1 << ", two, " << 3;
79 ASSERT_EQ(1, messages.size());
80 EXPECT_EQ("stream test: 1, two, 3", messages[0].first.getMessage());
81 EXPECT_TRUE(messages[0].first.getFileName().endsWith("XlogTest.cpp"))
82 << "unexpected file name: " << messages[0].first.getFileName();
83 EXPECT_EQ(LogLevel::INFO, messages[0].first.getLevel());
84 EXPECT_EQ("xlog_test.main_file", messages[0].first.getCategory()->getName());
85 EXPECT_EQ("xlog_test", messages[0].second->getName());
89 TEST(Xlog, perFileCategoryHandling) {
90 using namespace logging_test;
92 auto handler = make_shared<TestLogHandler>();
94 ->getCategory("folly.experimental.logging.test")
95 ->addHandler(handler);
96 LoggerDB::get()->setLevel("folly.experimental.logging.test", LogLevel::DBG9);
97 auto& messages = handler->getMessages();
99 // Use the simple helper function in XlogHeader2
100 testXlogHdrFunction("factor", 99);
101 ASSERT_EQ(1, messages.size());
102 EXPECT_EQ("test: factor=99", messages[0].first.getMessage());
103 EXPECT_TRUE(messages[0].first.getFileName().endsWith("XlogHeader2.h"))
104 << "unexpected file name: " << messages[0].first.getFileName();
105 EXPECT_EQ(LogLevel::DBG3, messages[0].first.getLevel());
107 "folly.experimental.logging.test.XlogHeader2",
108 messages[0].first.getCategory()->getName());
109 EXPECT_EQ("folly.experimental.logging.test", messages[0].second->getName());
112 // Test the loop function from XlogHeader1
113 testXlogHdrLoop(3, "hello world");
114 ASSERT_EQ(5, messages.size());
115 EXPECT_EQ("starting: hello world", messages[0].first.getMessage());
116 EXPECT_TRUE(messages[0].first.getFileName().endsWith("XlogHeader1.h"))
117 << "unexpected file name: " << messages[0].first.getFileName();
118 EXPECT_EQ(LogLevel::DBG1, messages[0].first.getLevel());
120 "folly.experimental.logging.test.XlogHeader1",
121 messages[0].first.getCategory()->getName());
122 EXPECT_EQ("folly.experimental.logging.test", messages[0].second->getName());
124 EXPECT_EQ("test: hello world", messages[1].first.getMessage());
125 EXPECT_EQ("test: hello world", messages[2].first.getMessage());
126 EXPECT_EQ("test: hello world", messages[3].first.getMessage());
127 EXPECT_EQ("finished: hello world", messages[4].first.getMessage());
128 EXPECT_EQ(LogLevel::DBG5, messages[1].first.getLevel());
129 EXPECT_EQ(LogLevel::DBG5, messages[2].first.getLevel());
130 EXPECT_EQ(LogLevel::DBG5, messages[3].first.getLevel());
131 EXPECT_EQ(LogLevel::DBG1, messages[4].first.getLevel());
134 // Reduce the log level so that the messages inside the loop
135 // should not be logged.
136 LoggerDB::get()->setLevel("folly.experimental.logging.test", LogLevel::DBG2);
137 testXlogHdrLoop(300, "hello world");
138 ASSERT_EQ(2, messages.size());
139 EXPECT_EQ("starting: hello world", messages[0].first.getMessage());
140 EXPECT_EQ("finished: hello world", messages[1].first.getMessage());
143 // Call the helpers function in XlogFile1.cpp and XlogFile2.cpp and makes
144 // sure their categories are reported correctly.
145 testXlogFile1Dbg1("foobar 1234");
146 ASSERT_EQ(1, messages.size());
147 EXPECT_EQ("file1: foobar 1234", messages[0].first.getMessage());
149 "folly.experimental.logging.test.XlogFile1",
150 messages[0].first.getCategory()->getName());
153 testXlogFile2Dbg1("hello world");
154 ASSERT_EQ(1, messages.size());
155 EXPECT_EQ("file2: hello world", messages[0].first.getMessage());
157 "folly.experimental.logging.test.XlogFile2",
158 messages[0].first.getCategory()->getName());
161 // Adjust the log level and make sure the changes take effect for the .cpp
163 LoggerDB::get()->setLevel("folly.experimental.logging.test", LogLevel::INFO);
164 testXlogFile1Dbg1("log check should fail now");
165 testXlogFile2Dbg1("this should fail too");
166 EXPECT_EQ(0, messages.size());
169 LoggerDB::get()->setLevel(
170 "folly.experimental.logging.test.XlogFile1", LogLevel::DBG1);
171 testXlogFile1Dbg1("this log check should pass now");
172 testXlogFile2Dbg1("but this one should still fail");
173 ASSERT_EQ(1, messages.size());
175 "file1: this log check should pass now", messages[0].first.getMessage());
177 "folly.experimental.logging.test.XlogFile1",
178 messages[0].first.getCategory()->getName());