diff options
author | Anthony Towns <aj@erisian.com.au> | 2023-08-22 13:23:38 +1000 |
---|---|---|
committer | Anthony Towns <aj@erisian.com.au> | 2023-12-20 15:59:48 +1000 |
commit | f7ce5ac08c669ac763e275bb7c82dcfb2b1b6c33 (patch) | |
tree | 2bc830fec16764c0196930f3576081c7e3631590 /src | |
parent | fbd7642c8e5b70327e019382320f5ef0a651ecc5 (diff) | |
download | bitcoin-f7ce5ac08c669ac763e275bb7c82dcfb2b1b6c33.tar.xz |
logging: add LogError, LogWarning, LogInfo, LogDebug, LogTrace
These provide simple and clear ways to write the most common logging
operations:
LogInfo("msg");
LogDebug(BCLog::LogFlags::NET, "msg");
LogError("msg");
LogWarning("msg");
LogTrace(BCLog::LogFlags::NET, "msg");
For cases where the level cannot be hardcoded, LogPrintLevel(category,
level, ...) remains available.
Diffstat (limited to 'src')
-rw-r--r-- | src/logging.h | 13 | ||||
-rw-r--r-- | src/test/logging_tests.cpp | 24 |
2 files changed, 33 insertions, 4 deletions
diff --git a/src/logging.h b/src/logging.h index 9b56f7af0d..525e0aec6d 100644 --- a/src/logging.h +++ b/src/logging.h @@ -237,9 +237,12 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st #define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__) // Log unconditionally. -#define LogPrintf(...) LogPrintLevel_(BCLog::LogFlags::NONE, BCLog::Level::Info, __VA_ARGS__) +#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__) +#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, __VA_ARGS__) +#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, __VA_ARGS__) -// Log unconditionally, prefixing the output with the passed category name. +// Deprecated unconditional logging. +#define LogPrintf(...) LogInfo(__VA_ARGS__) #define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::Info, __VA_ARGS__) // Use a macro instead of a function for conditional logging to prevent @@ -254,7 +257,11 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st } while (0) // Log conditionally, prefixing the output with the passed category name. -#define LogPrint(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__) +#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__) +#define LogTrace(category, ...) LogPrintLevel(category, BCLog::Level::Trace, __VA_ARGS__) + +// Deprecated conditional logging +#define LogPrint(category, ...) LogDebug(category, __VA_ARGS__) template <typename... Args> bool error(const char* fmt, const Args&... args) diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp index abc878c5a5..88e3ec94b7 100644 --- a/src/test/logging_tests.cpp +++ b/src/test/logging_tests.cpp @@ -105,10 +105,11 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup) BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end()); } -BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup) +BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacrosDeprecated, LogSetup) { LogPrintf("foo5: %s\n", "bar5"); LogPrint(BCLog::NET, "foo6: %s\n", "bar6"); + LogPrintLevel(BCLog::NET, BCLog::Level::Trace, "foo4: %s\n", "bar4"); // not logged LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo7: %s\n", "bar7"); LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8"); LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9"); @@ -131,6 +132,27 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup) BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end()); } +BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup) +{ + LogTrace(BCLog::NET, "foo6: %s\n", "bar6"); // not logged + LogDebug(BCLog::NET, "foo7: %s\n", "bar7"); + LogInfo("foo8: %s\n", "bar8"); + LogWarning("foo9: %s\n", "bar9"); + LogError("foo10: %s\n", "bar10"); + std::ifstream file{tmp_log_path}; + std::vector<std::string> log_lines; + for (std::string log; std::getline(file, log);) { + log_lines.push_back(log); + } + std::vector<std::string> expected = { + "[net] foo7: bar7", + "foo8: bar8", + "[warning] foo9: bar9", + "[error] foo10: bar10", + }; + BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end()); +} + BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup) { LogInstance().EnableCategory(BCLog::LogFlags::ALL); |