Skip to content

Commit eb8aab7

Browse files
committed
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
1 parent b9416c3 commit eb8aab7

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

src/logging.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,20 +199,26 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st
199199
}
200200
}
201201

202-
203202
#define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
204203

204+
// Log unconditionally.
205205
#define LogPrintf(...) LogPrintLevel_(BCLog::LogFlags::NONE, BCLog::Level::None, __VA_ARGS__)
206206

207+
// Log unconditionally, prefixing the output with the passed category name.
208+
#define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__)
209+
207210
// Use a macro instead of a function for conditional logging to prevent
208211
// evaluating arguments when logging for the category is not enabled.
212+
213+
// Log conditionally, prefixing the output with the passed category name.
209214
#define LogPrint(category, ...) \
210215
do { \
211216
if (LogAcceptCategory((category), BCLog::Level::Debug)) { \
212217
LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__); \
213218
} \
214219
} while (0)
215220

221+
// Log conditionally, prefixing the output with the passed category name and severity level.
216222
#define LogPrintLevel(category, level, ...) \
217223
do { \
218224
if (LogAcceptCategory((category), (level))) { \

src/test/logging_tests.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
103103
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
104104
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
105105
LogPrintLevel(BCLog::NET, BCLog::Level::Error, "foo10: %s\n", "bar10");
106+
LogPrintfCategory(BCLog::VALIDATION, "foo11: %s\n", "bar11");
106107
std::ifstream file{tmp_log_path};
107108
std::vector<std::string> log_lines;
108109
for (std::string log; std::getline(file, log);) {
@@ -114,7 +115,9 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
114115
"[net:debug] foo7: bar7",
115116
"[net:info] foo8: bar8",
116117
"[net:warning] foo9: bar9",
117-
"[net:error] foo10: bar10"};
118+
"[net:error] foo10: bar10",
119+
"[validation] foo11: bar11",
120+
};
118121
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
119122
}
120123

test/lint/lint-format-strings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'LogConnectFailure,1',
2323
'LogPrint,1',
2424
'LogPrintf,0',
25+
'LogPrintfCategory,1',
2526
'LogPrintLevel,2',
2627
'printf,0',
2728
'snprintf,2',

test/lint/lint-logs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717

1818
def main():
19-
logs_list = check_output(["git", "grep", "--extended-regexp", r"(LogPrintLevel|LogPrintf?)\(", "--", "*.cpp"], universal_newlines=True, encoding="utf8").splitlines()
19+
logs_list = check_output(["git", "grep", "--extended-regexp", r"(LogPrintLevel|LogPrintfCategory|LogPrintf?)\(", "--", "*.cpp"], universal_newlines=True, encoding="utf8").splitlines()
2020

2121
unterminated_logs = [line for line in logs_list if not re.search(r'(\\n"|/\* Continued \*/)', line)]
2222

2323
if unterminated_logs != []:
24-
print("All calls to LogPrintf(), LogPrint(), LogPrintLevel(), and WalletLogPrintf() should be terminated with \"\\n\".")
24+
print("All calls to LogPrintf(), LogPrintfCategory(), LogPrint(), LogPrintLevel(), and WalletLogPrintf() should be terminated with \"\\n\".")
2525
print("")
2626

2727
for line in unterminated_logs:

0 commit comments

Comments
 (0)