aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon Atack <jon@atack.com>2022-05-25 18:26:54 +0200
committerJon Atack <jon@atack.com>2022-06-08 14:02:54 +0200
commiteb8aab759fb15824a5dd3004e689d0eb5b884a32 (patch)
tree3dc6ec7fdb4199a4d5115a016355834f23c50a83
parentb9416c3847cd347238a9d75d949327f69e187d79 (diff)
downloadbitcoin-eb8aab759fb15824a5dd3004e689d0eb5b884a32.tar.xz
logging: add LogPrintfCategory to log unconditionally with category
prefixing the output with the passed category name. - add documentation - add a unit test - update lint-logs.py - update lint-format-strings.py
-rw-r--r--src/logging.h8
-rw-r--r--src/test/logging_tests.cpp5
-rwxr-xr-xtest/lint/lint-format-strings.py1
-rwxr-xr-xtest/lint/lint-logs.py4
4 files changed, 14 insertions, 4 deletions
diff --git a/src/logging.h b/src/logging.h
index 8a896b6b33..50869ad89a 100644
--- a/src/logging.h
+++ b/src/logging.h
@@ -199,13 +199,18 @@ 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::None, __VA_ARGS__)
+// Log unconditionally, prefixing the output with the passed category name.
+#define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__)
+
// Use a macro instead of a function for conditional logging to prevent
// evaluating arguments when logging for the category is not enabled.
+
+// Log conditionally, prefixing the output with the passed category name.
#define LogPrint(category, ...) \
do { \
if (LogAcceptCategory((category), BCLog::Level::Debug)) { \
@@ -213,6 +218,7 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st
} \
} while (0)
+// Log conditionally, prefixing the output with the passed category name and severity level.
#define LogPrintLevel(category, level, ...) \
do { \
if (LogAcceptCategory((category), (level))) { \
diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp
index 3f6a605945..5a5e3b3f1f 100644
--- a/src/test/logging_tests.cpp
+++ b/src/test/logging_tests.cpp
@@ -103,6 +103,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
+ LogPrintfCategory(BCLog::VALIDATION, "foo11: %s\n", "bar11");
std::ifstream file{tmp_log_path};
std::vector<std::string> log_lines;
for (std::string log; std::getline(file, log);) {
@@ -114,7 +115,9 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
"[net:debug] foo7: bar7",
"[net:info] foo8: bar8",
"[net:warning] foo9: bar9",
- "[net:error] foo10: bar10"};
+ "[net:error] foo10: bar10",
+ "[validation] foo11: bar11",
+ };
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
}
diff --git a/test/lint/lint-format-strings.py b/test/lint/lint-format-strings.py
index 412cf86791..522023e328 100755
--- a/test/lint/lint-format-strings.py
+++ b/test/lint/lint-format-strings.py
@@ -22,6 +22,7 @@ FUNCTION_NAMES_AND_NUMBER_OF_LEADING_ARGUMENTS = [
'LogConnectFailure,1',
'LogPrint,1',
'LogPrintf,0',
+ 'LogPrintfCategory,1',
'LogPrintLevel,2',
'printf,0',
'snprintf,2',
diff --git a/test/lint/lint-logs.py b/test/lint/lint-logs.py
index de53729b4e..aaf697467d 100755
--- a/test/lint/lint-logs.py
+++ b/test/lint/lint-logs.py
@@ -16,12 +16,12 @@ from subprocess import check_output
def main():
- logs_list = check_output(["git", "grep", "--extended-regexp", r"(LogPrintLevel|LogPrintf?)\(", "--", "*.cpp"], universal_newlines=True, encoding="utf8").splitlines()
+ logs_list = check_output(["git", "grep", "--extended-regexp", r"(LogPrintLevel|LogPrintfCategory|LogPrintf?)\(", "--", "*.cpp"], universal_newlines=True, encoding="utf8").splitlines()
unterminated_logs = [line for line in logs_list if not re.search(r'(\\n"|/\* Continued \*/)', line)]
if unterminated_logs != []:
- print("All calls to LogPrintf(), LogPrint(), LogPrintLevel(), and WalletLogPrintf() should be terminated with \"\\n\".")
+ print("All calls to LogPrintf(), LogPrintfCategory(), LogPrint(), LogPrintLevel(), and WalletLogPrintf() should be terminated with \"\\n\".")
print("")
for line in unterminated_logs: