Bitcoin Core 28.99.0
P2P Digital Currency
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
24
25static void ResetLogger()
26{
29}
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
66 {
68 LogPrintf("Sentinel log to reopen log file\n");
76 }
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
86BOOST_FIXTURE_TEST_CASE(logging_LogPrintStr, LogSetup)
87{
89 LogInstance().LogPrintStr("foo1: bar1", "fn1", "src1", 1, BCLog::LogFlags::NET, BCLog::Level::Debug);
90 LogInstance().LogPrintStr("foo2: bar2", "fn2", "src2", 2, BCLog::LogFlags::NET, BCLog::Level::Info);
91 LogInstance().LogPrintStr("foo3: bar3", "fn3", "src3", 3, BCLog::LogFlags::ALL, BCLog::Level::Debug);
92 LogInstance().LogPrintStr("foo4: bar4", "fn4", "src4", 4, BCLog::LogFlags::ALL, BCLog::Level::Info);
93 LogInstance().LogPrintStr("foo5: bar5", "fn5", "src5", 5, BCLog::LogFlags::NONE, BCLog::Level::Debug);
94 LogInstance().LogPrintStr("foo6: bar6", "fn6", "src6", 6, BCLog::LogFlags::NONE, BCLog::Level::Info);
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 LogPrintLevel(BCLog::NET, BCLog::Level::Trace, "foo4: %s\n", "bar4"); // not logged
115 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo7: %s\n", "bar7");
116 LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
117 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
118 LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
119 std::ifstream file{tmp_log_path};
120 std::vector<std::string> log_lines;
121 for (std::string log; std::getline(file, log);) {
122 log_lines.push_back(log);
123 }
124 std::vector<std::string> expected = {
125 "foo5: bar5",
126 "[net] foo7: bar7",
127 "[net:info] foo8: bar8",
128 "[net:warning] foo9: bar9",
129 "[net:error] foo10: bar10",
130 };
131 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
132}
133
134BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
135{
136 LogTrace(BCLog::NET, "foo6: %s", "bar6"); // not logged
137 LogDebug(BCLog::NET, "foo7: %s", "bar7");
138 LogInfo("foo8: %s", "bar8");
139 LogWarning("foo9: %s", "bar9");
140 LogError("foo10: %s", "bar10");
141 std::ifstream file{tmp_log_path};
142 std::vector<std::string> log_lines;
143 for (std::string log; std::getline(file, log);) {
144 log_lines.push_back(log);
145 }
146 std::vector<std::string> expected = {
147 "[net] foo7: bar7",
148 "foo8: bar8",
149 "[warning] foo9: bar9",
150 "[error] foo10: bar10",
151 };
152 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
153}
154
155BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
156{
158 const auto concatenated_category_names = LogInstance().LogCategoriesString();
159 std::vector<std::pair<BCLog::LogFlags, std::string>> expected_category_names;
160 const auto category_names = SplitString(concatenated_category_names, ',');
161 for (const auto& category_name : category_names) {
162 BCLog::LogFlags category;
163 const auto trimmed_category_name = TrimString(category_name);
164 BOOST_REQUIRE(GetLogCategory(category, trimmed_category_name));
165 expected_category_names.emplace_back(category, trimmed_category_name);
166 }
167
168 std::vector<std::string> expected;
169 for (const auto& [category, name] : expected_category_names) {
170 LogDebug(category, "foo: %s\n", "bar");
171 std::string expected_log = "[";
172 expected_log += name;
173 expected_log += "] foo: bar";
174 expected.push_back(expected_log);
175 }
176
177 std::ifstream file{tmp_log_path};
178 std::vector<std::string> log_lines;
179 for (std::string log; std::getline(file, log);) {
180 log_lines.push_back(log);
181 }
182 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
183}
184
185BOOST_FIXTURE_TEST_CASE(logging_SeverityLevels, LogSetup)
186{
188
190 LogInstance().SetCategoryLogLevel(/*category_str=*/"net", /*level_str=*/"info");
191
192 // Global log level
193 LogPrintLevel(BCLog::HTTP, BCLog::Level::Info, "foo1: %s\n", "bar1");
194 LogPrintLevel(BCLog::MEMPOOL, BCLog::Level::Trace, "foo2: %s. This log level is lower than the global one.\n", "bar2");
196 LogPrintLevel(BCLog::RPC, BCLog::Level::Error, "foo4: %s\n", "bar4");
197
198 // Category-specific log level
199 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo5: %s\n", "bar5");
200 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");
201 LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo7: %s\n", "bar7");
202
203 std::vector<std::string> expected = {
204 "[http:info] foo1: bar1",
205 "[validation:warning] foo3: bar3",
206 "[rpc:error] foo4: bar4",
207 "[net:warning] foo5: bar5",
208 "[net:error] foo7: bar7",
209 };
210 std::ifstream file{tmp_log_path};
211 std::vector<std::string> log_lines;
212 for (std::string log; std::getline(file, log);) {
213 log_lines.push_back(log);
214 }
215 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
216}
217
219{
220 // Set global log level
221 {
222 ResetLogger();
225 const char* argv_test[] = {"bitcoind", "-loglevel=debug"};
226 std::string err;
227 BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
228
229 auto result = init::SetLoggingLevel(args);
230 BOOST_REQUIRE(result);
232 }
233
234 // Set category-specific log level
235 {
236 ResetLogger();
239 const char* argv_test[] = {"bitcoind", "-loglevel=net:trace"};
240 std::string err;
241 BOOST_REQUIRE(args.ParseParameters(2, argv_test, err));
242
243 auto result = init::SetLoggingLevel(args);
244 BOOST_REQUIRE(result);
246
247 const auto& category_levels{LogInstance().CategoryLevels()};
248 const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
249 BOOST_REQUIRE(net_it != category_levels.end());
250 BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
251 }
252
253 // Set both global log level and category-specific log level
254 {
255 ResetLogger();
258 const char* argv_test[] = {"bitcoind", "-loglevel=debug", "-loglevel=net:trace", "-loglevel=http:info"};
259 std::string err;
260 BOOST_REQUIRE(args.ParseParameters(4, argv_test, err));
261
262 auto result = init::SetLoggingLevel(args);
263 BOOST_REQUIRE(result);
265
266 const auto& category_levels{LogInstance().CategoryLevels()};
267 BOOST_CHECK_EQUAL(category_levels.size(), 2);
268
269 const auto net_it{category_levels.find(BCLog::LogFlags::NET)};
270 BOOST_CHECK(net_it != category_levels.end());
271 BOOST_CHECK_EQUAL(net_it->second, BCLog::Level::Trace);
272
273 const auto http_it{category_levels.find(BCLog::LogFlags::HTTP)};
274 BOOST_CHECK(http_it != category_levels.end());
275 BOOST_CHECK_EQUAL(http_it->second, BCLog::Level::Info);
276 }
277}
278
ArgsManager & args
Definition: bitcoind.cpp:277
@ ALLOW_ANY
disable validation
Definition: args.h:106
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:179
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:564
bool m_log_sourcelocations
Definition: logging.h:138
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition: logging.h:190
void SetLogLevel(Level level)
Definition: logging.h:198
fs::path m_file_path
Definition: logging.h:141
bool m_log_threadnames
Definition: logging.h:137
void EnableCategory(LogFlags flag)
Definition: logging.cpp:121
bool m_log_timestamps
Definition: logging.h:135
void LogPrintStr(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Send a string to the log output.
Definition: logging.cpp:387
std::atomic< bool > m_reopen_file
Definition: logging.h:142
bool m_print_to_file
Definition: logging.h:133
std::unordered_map< LogFlags, Level > CategoryLevels() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Definition: logging.h:185
std::string LogCategoriesString() const
Returns a string with the log categories in alphabetical order.
Definition: logging.h:214
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_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
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:24
#define LogPrintLevel(category, level,...)
Definition: logging.h:272
#define LogWarning(...)
Definition: logging.h:262
#define LogInfo(...)
Definition: logging.h:261
#define LogError(...)
Definition: logging.h:263
#define LogTrace(category,...)
Definition: logging.h:281
#define LogDebug(category,...)
Definition: logging.h:280
#define LogPrintf(...)
Definition: logging.h:266
BOOST_FIXTURE_TEST_CASE(logging_LogPrintStr, LogSetup)
BOOST_AUTO_TEST_CASE(logging_timer)
static void ResetLogger()
Level
Definition: logging.h:76
constexpr auto DEFAULT_LOG_LEVEL
Definition: logging.h:83
LogFlags
Definition: logging.h:41
@ ALL
Definition: logging.h:74
@ RPC
Definition: logging.h:50
@ HTTP
Definition: logging.h:46
@ NONE
Definition: logging.h:42
@ VALIDATION
Definition: logging.h:64
@ MEMPOOL
Definition: logging.h:45
@ NET
Definition: logging.h:43
util::Result< void > SetLoggingLevel(const ArgsManager &args)
Definition: common.cpp:59
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:136
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:156
#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.
Definition: setup_common.h:63
ArgsManager m_args
Test-specific arguments and settings.
Definition: setup_common.h:98
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