14 #include <unordered_map> 18 #include <boost/test/unit_test.hpp> 68 LogPrintf(
"Sentinel log to reopen log file\n");
82 const std::string_view result_prefix{
"tests: msg ("};
83 BOOST_CHECK_EQUAL(micro_timer.LogMsg(
"msg").substr(0, result_prefix.size()), result_prefix);
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);
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",
108 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
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);
126 std::vector<std::string> expected = {
130 "[net:info] foo8: bar8",
131 "[net:warning] foo9: bar9",
132 "[net:error] foo10: bar10",
133 "[validation:info] foo11: bar11",
135 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
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);
150 std::vector<std::string> expected = {
153 "[warning] foo9: bar9",
154 "[error] foo10: bar10",
156 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
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) {
167 const auto trimmed_category_name =
TrimString(category_name);
169 expected_category_names.emplace_back(category, trimmed_category_name);
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);
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);
186 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
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",
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);
219 BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
229 const char* argv_test[] = {
"bitcoind",
"-loglevel=debug"};
234 BOOST_REQUIRE(result);
243 const char* argv_test[] = {
"bitcoind",
"-loglevel=net:trace"};
248 BOOST_REQUIRE(result);
253 BOOST_REQUIRE(net_it != category_levels.end());
262 const char* argv_test[] = {
"bitcoind",
"-loglevel=debug",
"-loglevel=net:trace",
"-loglevel=http:info"};
267 BOOST_REQUIRE(result);
void EnableCategory(LogFlags flag)
std::unordered_map< BCLog::LogFlags, BCLog::Level > prev_category_levels
std::vector< std::string > SplitString(std::string_view str, char sep)
BCLog::Logger & LogInstance()
std::string TrimString(std::string_view str, std::string_view pattern=" \\\)
#define LogPrint(category,...)
std::unordered_map< LogFlags, Level > CategoryLevels() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
RAII-style object that outputs timing information to logs.
bool GetLogCategory(BCLog::LogFlags &flag, std::string_view str)
Return true if str parses as a log category and set the flag.
BCLog::Level prev_log_level
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)
util::Result< void > SetLoggingLevel(const ArgsManager &args)
std::atomic< bool > m_reopen_file
bool ParseParameters(int argc, const char *const argv[], std::string &error)
bool m_log_sourcelocations
fs::path GetDataDirBase() const
Get data directory path.
constexpr auto DEFAULT_LOG_LEVEL
Common init functions shared by bitcoin-node, bitcoin-wallet, etc.
BOOST_AUTO_TEST_CASE(logging_timer)
BOOST_AUTO_TEST_SUITE_END()
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
#define LogPrintLevel(category, level,...)
#define LogPrintfCategory(category,...)
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
void SetLogLevel(Level level)
#define LogDebug(category,...)
#define BOOST_CHECK_EQUAL(v1, v2)
bool prev_log_threadnames
static void ResetLogger()
#define LogTrace(category,...)
bool prev_log_sourcelocations
BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
std::string LogCategoriesString() const
Returns a string with the log categories in alphabetical order.
#define BOOST_CHECK(expr)