14 #include <unordered_map>
18 #include <boost/test/unit_test.hpp>
65 LogPrintf(
"Sentinel log to reopen log file\n");
79 const std::string_view result_prefix{
"tests: msg ("};
80 BOOST_CHECK_EQUAL(micro_timer.LogMsg(
"msg").substr(0, result_prefix.size()), result_prefix);
92 std::ifstream file{tmp_log_path};
93 std::vector<std::string> log_lines;
94 for (std::string log; std::getline(file, log);) {
95 log_lines.push_back(log);
97 std::vector<std::string> expected = {
98 "[src1:1] [fn1] [net] foo1: bar1",
99 "[src2:2] [fn2] [net:info] foo2: bar2",
100 "[src3:3] [fn3] [debug] foo3: bar3",
101 "[src4:4] [fn4] foo4: bar4",
102 "[src5:5] [fn5] [debug] foo5: bar5",
103 "[src6:6] [fn6] foo6: bar6",
105 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
118 std::ifstream file{tmp_log_path};
119 std::vector<std::string> log_lines;
120 for (std::string log; std::getline(file, log);) {
121 log_lines.push_back(log);
123 std::vector<std::string> expected = {
127 "[net:info] foo8: bar8",
128 "[net:warning] foo9: bar9",
129 "[net:error] foo10: bar10",
130 "[validation:info] foo11: bar11",
132 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
142 std::ifstream file{tmp_log_path};
143 std::vector<std::string> log_lines;
144 for (std::string log; std::getline(file, log);) {
145 log_lines.push_back(log);
147 std::vector<std::string> expected = {
150 "[warning] foo9: bar9",
151 "[error] foo10: bar10",
153 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
160 std::vector<std::pair<BCLog::LogFlags, std::string>> expected_category_names;
161 const auto category_names =
SplitString(concatenated_category_names,
',');
162 for (
const auto& category_name : category_names) {
164 const auto trimmed_category_name =
TrimString(category_name);
166 expected_category_names.emplace_back(category, trimmed_category_name);
169 std::vector<std::string> expected;
170 for (
const auto& [category,
name] : expected_category_names) {
171 LogPrint(category,
"foo: %s\n",
"bar");
172 std::string expected_log =
"[";
173 expected_log +=
name;
174 expected_log +=
"] foo: bar";
175 expected.push_back(expected_log);
178 std::ifstream file{tmp_log_path};
179 std::vector<std::string> log_lines;
180 for (std::string log; std::getline(file, log);) {
181 log_lines.push_back(log);
183 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
204 std::vector<std::string> expected = {
205 "[http:info] foo1: bar1",
206 "[validation:warning] foo3: bar3",
207 "[rpc:error] foo4: bar4",
208 "[net:warning] foo5: bar5",
209 "[net:error] foo7: bar7",
211 std::ifstream file{tmp_log_path};
212 std::vector<std::string> log_lines;
213 for (std::string log; std::getline(file, log);) {
214 log_lines.push_back(log);
216 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
226 const char* argv_test[] = {
"bitcoind",
"-loglevel=debug"};
231 BOOST_REQUIRE(result);
240 const char* argv_test[] = {
"bitcoind",
"-loglevel=net:trace"};
245 BOOST_REQUIRE(result);
250 BOOST_REQUIRE(net_it != category_levels.end());
259 const char* argv_test[] = {
"bitcoind",
"-loglevel=debug",
"-loglevel=net:trace",
"-loglevel=http:info"};
264 BOOST_REQUIRE(result);
@ ALLOW_ANY
disable validation
bool ParseParameters(int argc, const char *const argv[], std::string &error)
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
bool m_log_sourcelocations
void SetLogLevel(Level level)
std::unordered_map< LogFlags, Level > CategoryLevels() const
void EnableCategory(LogFlags flag)
std::atomic< bool > m_reopen_file
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels)
std::string LogCategoriesString() const
Returns a string with the log categories in alphabetical order.
RAII-style object that outputs timing information to logs.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
BOOST_AUTO_TEST_SUITE_END()
Common init functions shared by bitcoin-node, bitcoin-wallet, etc.
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Return true if str parses as a log category and set the flag.
BCLog::Logger & LogInstance()
#define LogPrintLevel(category, level,...)
static void LogPrintf_(const std::string &logging_function, const std::string &source_file, const int source_line, const BCLog::LogFlags flag, const BCLog::Level level, const char *fmt, const Args &... args)
#define LogPrint(category,...)
#define LogTrace(category,...)
#define LogDebug(category,...)
#define LogPrintfCategory(category,...)
BOOST_AUTO_TEST_CASE(logging_timer)
BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
static void ResetLogger()
constexpr auto DEFAULT_LOG_LEVEL
util::Result< void > SetLoggingLevel(const ArgsManager &args)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
std::vector< std::string > SplitString(std::string_view str, char sep)
std::unordered_map< BCLog::LogFlags, BCLog::Level > prev_category_levels
BCLog::Level prev_log_level
bool prev_log_threadnames
bool prev_log_sourcelocations