Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
logging_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2019-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <init/common.h>
6#include <logging.h>
7#include <logging/timer.h>
9#include <util/string.h>
10
11#include <chrono>
12#include <fstream>
13#include <iostream>
14#include <unordered_map>
15#include <utility>
16#include <vector>
17
18#include <boost/test/unit_test.hpp>
19
22
23BOOST_FIXTURE_TEST_SUITE(logging_tests, BasicTestingSetup)
24
30
31struct LogSetup : public BasicTestingSetup {
39 std::unordered_map<BCLog::LogFlags, BCLog::Level> prev_category_levels;
41
43 tmp_log_path{m_args.GetDataDirBase() / "tmp_debug.log"},
44 prev_reopen_file{LogInstance().m_reopen_file},
45 prev_print_to_file{LogInstance().m_print_to_file},
46 prev_log_timestamps{LogInstance().m_log_timestamps},
47 prev_log_threadnames{LogInstance().m_log_threadnames},
48 prev_log_sourcelocations{LogInstance().m_log_sourcelocations},
49 prev_category_levels{LogInstance().CategoryLevels()},
50 prev_log_level{LogInstance().LogLevel()}
51 {
57
58 // Prevent tests from failing when the line number of the logs changes.
60
63 }
64
77};
78
80{
81 auto micro_timer = BCLog::Timer<std::chrono::microseconds>("tests", "end_msg");
82 const std::string_view result_prefix{"tests: msg ("};
83 BOOST_CHECK_EQUAL(micro_timer.LogMsg("msg").substr(0, result_prefix.size()), result_prefix);
84}
85
87{
89 LogPrintf_("fn1", "src1", 1, BCLog::LogFlags::NET, BCLog::Level::Debug, "foo1: %s\n", "bar1");
90 LogPrintf_("fn2", "src2", 2, BCLog::LogFlags::NET, BCLog::Level::Info, "foo2: %s\n", "bar2");
91 LogPrintf_("fn3", "src3", 3, BCLog::LogFlags::ALL, BCLog::Level::Debug, "foo3: %s\n", "bar3");
92 LogPrintf_("fn4", "src4", 4, BCLog::LogFlags::ALL, BCLog::Level::Info, "foo4: %s\n", "bar4");
93 LogPrintf_("fn5", "src5", 5, BCLog::LogFlags::NONE, BCLog::Level::Debug, "foo5: %s\n", "bar5");
94 LogPrintf_("fn6", "src6", 6, BCLog::LogFlags::NONE, BCLog::Level::Info, "foo6: %s\n", "bar6");
95 std::ifstream file{tmp_log_path};
96 std::vector<std::string> log_lines;
97 for (std::string log; std::getline(file, log);) {
98 log_lines.push_back(log);
99 }
100 std::vector<std::string> expected = {
101 "[src1:1] [fn1] [net] foo1: bar1",
102 "[src2:2] [fn2] [net:info] foo2: bar2",
103 "[src3:3] [fn3] [debug] foo3: bar3",
104 "[src4:4] [fn4] foo4: bar4",
105 "[src5:5] [fn5] [debug] foo5: bar5",
106 "[src6:6] [fn6] foo6: bar6",
107 };
108 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
109}
110
111BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacrosDeprecated, LogSetup)
112{
113 LogPrintf("foo5: %s\n", "bar5");
114 LogPrint(BCLog::NET, "foo6: %s\n", "bar6");
115 LogPrintLevel(BCLog::NET, BCLog::Level::Trace, "foo4: %s\n", "bar4"); // not logged
116 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo7: %s\n", "bar7");
117 LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
118 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
119 LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
120 LogPrintfCategory(BCLog::VALIDATION, "foo11: %s\n", "bar11");
121 std::ifstream file{tmp_log_path};
122 std::vector<std::string> log_lines;
123 for (std::string log; std::getline(file, log);) {
124 log_lines.push_back(log);
125 }
126 std::vector<std::string> expected = {
127 "foo5: bar5",
128 "[net] foo6: bar6",
129 "[net] foo7: bar7",
130 "[net:info] foo8: bar8",
131 "[net:warning] foo9: bar9",
132 "[net:error] foo10: bar10",
133 "[validation:info] foo11: bar11",
134 };
135 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
136}
137
138BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
139{
140 LogTrace(BCLog::NET, "foo6: %s\n", "bar6"); // not logged
141 LogDebug(BCLog::NET, "foo7: %s\n", "bar7");
142 LogInfo("foo8: %s\n", "bar8");
143 LogWarning("foo9: %s\n", "bar9");
144 LogError("foo10: %s\n", "bar10");
145 std::ifstream file{tmp_log_path};
146 std::vector<std::string> log_lines;
147 for (std::string log; std::getline(file, log);) {
148 log_lines.push_back(log);
149 }
150 std::vector<std::string> expected = {
151 "[net] foo7: bar7",
152 "foo8: bar8",
153 "[warning] foo9: bar9",
154 "[error] foo10: bar10",
155 };
156 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
157}
158
159BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
160{
162 const auto concatenated_category_names = LogInstance().LogCategoriesString();
163 std::vector<std::pair<BCLog::LogFlags, std::string>> expected_category_names;
164 const auto category_names = SplitString(concatenated_category_names, ',');
165 for (const auto& category_name : category_names) {
166 BCLog::LogFlags category;
167 const auto trimmed_category_name = TrimString(category_name);
168 BOOST_REQUIRE(GetLogCategory(category, trimmed_category_name));
169 expected_category_names.emplace_back(category, trimmed_category_name);
170 }
171
172 std::vector<std::string> expected;
173 for (const auto& [category, name] : expected_category_names) {
174 LogPrint(category, "foo: %s\n", "bar");
175 std::string expected_log = "[";
176 expected_log += name;
177 expected_log += "] foo: bar";
178 expected.push_back(expected_log);
179 }
180
181 std::ifstream file{tmp_log_path};
182 std::vector<std::string> log_lines;
183 for (std::string log; std::getline(file, log);) {
184 log_lines.push_back(log);
185 }
186 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
187}
188
189BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
190{
192
194 LogInstance().SetCategoryLogLevel(/*category_str=*/"net", /*level_str=*/"info");
195
196 // Global log level
197 LogPrintLevel(BCLog::HTTP, BCLog::Level::Info, "foo1: %s\n", "bar1");
198 LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Trace, "foo2: %s. This log level is lower than the global one.\n", "bar2");
200 LogPrintLevel(BCLog::RPC, BCLog::Level::Error, "foo4: %s\n", "bar4");
201
202 // Category-specific log level
203 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo5: %s\n", "bar5");
204 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo6: %s. This log level is the same as the global one but lower than the category-specific one, which takes precedence. \n", "bar6");
205 LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo7: %s\n", "bar7");
206
207 std::vector<std::string> expected = {
208 "[http:info] foo1: bar1",
209 "[validation:warning] foo3: bar3",
210 "[rpc:error] foo4: bar4",
211 "[net:warning] foo5: bar5",
212 "[net:error] foo7: bar7",
213 };
214 std::ifstream file{tmp_log_path};
215 std::vector<std::string> log_lines;
216 for (std::string log; std::getline(file, log);) {
217 log_lines.push_back(log);
218 }
219 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
220}
221
223{
224 // Set global log level
225 {
226 ResetLogger();
229 const char* argv_test[] = {"bitcoind", "-loglevel=debug"};
230 std::string err;
231 BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
232
233 auto result = init::SetLoggingLevel(args);
234 BOOST_REQUIRE(result);
236 }
237
238 // Set category-specific log level
239 {
240 ResetLogger();
243 const char* argv_test[] = {"bitcoind", "-loglevel=net:trace"};
244 std::string err;
245 BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
246
247 auto result = init::SetLoggingLevel(args);
248 BOOST_REQUIRE(result);
250
251 const auto& category_levels{LogInstance().CategoryLevels()};
252 const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
253 BOOST_REQUIRE(net_it != category_levels.end());
254 BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
255 }
256
257 // Set both global log level and category-specific log level
258 {
259 ResetLogger();
262 const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:trace", "-loglevel=http:info"};
263 std::string err;
264 BOOST_REQUIRE(args.ParseParameters(4, argv_test, err));
265
266 auto result = init::SetLoggingLevel(args);
267 BOOST_REQUIRE(result);
269
270 const auto& category_levels{LogInstance().CategoryLevels()};
271 BOOST_CHECK_EQUAL(category_levels.size(), 2);
272
273 const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
274 BOOST_CHECK(net_it != category_levels.end());
275 BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
276
277 const auto http_it{category_levels.find(BCLog::LogFlags::HTTP)};
278 BOOST_CHECK(http_it != category_levels.end());
279 BOOST_CHECK_EQUAL(http_it->second, BCLog::Level::Info);
280 }
281}
282
ArgsManager & args
Definition bitcoind.cpp:270
@ ALLOW_ANY
disable validation
Definition args.h:104
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition args.cpp:178
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition args.cpp:563
bool m_log_sourcelocations
Definition logging.h:144
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition logging.h:196
void SetLogLevel(Level level)
Definition logging.h:204
fs::path m_file_path
Definition logging.h:147
bool m_log_threadnames
Definition logging.h:143
void EnableCategory(LogFlags flag)
Definition logging.cpp:121
bool m_log_timestamps
Definition logging.h:141
std::atomic< bool > m_reopen_file
Definition logging.h:148
bool m_print_to_file
Definition logging.h:139
std::unordered_map< LogFlags, Level > CategoryLevels() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition logging.h:191
std::string LogCategoriesString() const
Returns a string with the log categories in alphabetical order.
Definition logging.h:220
RAII-style object that outputs timing information to logs.
Definition timer.h:24
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:33
BOOST_AUTO_TEST_SUITE_END()
Common init functions shared by bitcoin-node, bitcoin-wallet, etc.
bool GetLogCategory(BCLog::LogFlags &flag, std::string_view str)
Return true if str parses as a log category and set the flag.
Definition logging.cpp:216
BCLog::Logger & LogInstance()
Definition logging.cpp:23
#define LogPrintLevel(category, level,...)
Definition logging.h:281
#define LogWarning(...)
Definition logging.h:270
#define LogPrint(category,...)
Definition logging.h:293
#define LogInfo(...)
Definition logging.h:269
#define LogError(...)
Definition logging.h:271
#define LogTrace(category,...)
Definition logging.h:290
static void LogPrintf_(std::string_view logging_function, std::string_view source_file, const int source_line, const BCLog::LogFlags flag, const BCLog::Level level, const char *fmt, const Args &... args)
Definition logging.h:252
#define LogDebug(category,...)
Definition logging.h:289
#define LogPrintfCategory(category,...)
Definition logging.h:275
#define LogPrintf(...)
Definition logging.h:274
BOOST_AUTO_TEST_CASE(logging_timer)
BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
static void ResetLogger()
Level
Definition logging.h:75
LogFlags
Definition logging.h:40
@ ALL
Definition logging.h:73
@ RPC
Definition logging.h:49
@ HTTP
Definition logging.h:45
@ NONE
Definition logging.h:41
@ VALIDATION
Definition logging.h:63
@ MEMPOOL
Definition logging.h:44
@ NET
Definition logging.h:42
constexpr auto DEFAULT_LOG_LEVEL
Definition logging.h:82
util::Result< void > SetLoggingLevel(const ArgsManager &args)
Definition common.cpp:58
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition string.h:59
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition string.h:79
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:18
#define BOOST_CHECK(expr)
Definition object.cpp:17
const char * name
Definition rest.cpp:49
Basic testing setup.
ArgsManager m_args
fs::path tmp_log_path
bool prev_reopen_file
fs::path prev_log_path
std::unordered_map< BCLog::LogFlags, BCLog::Level > prev_category_levels
bool prev_print_to_file
BCLog::Level prev_log_level
bool prev_log_threadnames
bool prev_log_sourcelocations
bool prev_log_timestamps
BOOST_CHECK_EQUAL_COLLECTIONS(R1L.begin(), R1L.end(), R1Array, R1Array+uint256::size())