Skip to content

Commit 7ff8e6b

Browse files
committed
Merge bitcoin/bitcoin#28318: logging: Simplify API for level based logging
e60fc7d logging: Replace uses of LogPrintfCategory (Anthony Towns) f7ce5ac logging: add LogError, LogWarning, LogInfo, LogDebug, LogTrace (Anthony Towns) fbd7642 logging: add -loglevelalways=1 option (Anthony Towns) 782bb6a logging: treat BCLog::ALL like BCLog::NONE (Anthony Towns) 667ce3e logging: Drop BCLog::Level::None (Anthony Towns) ab34dc6 logging: Log Info messages unconditionally (Anthony Towns) dfe98b6 logging: make [cat:debug] and [info] implicit (Anthony Towns) c5c76dc logging: refactor: pull prefix code out (Anthony Towns) Pull request description: Replace `LogPrint*` functions with severity based logging functions: * `LogInfo(...)`, `LogWarning(...)`, `LogError(...)` for unconditional (uncategorised) logging (replaces `LogPrintf`) * `LogDebug(CATEGORY, ...)` and `LogTrace(CATEGORY, ...)` for conditional logging (replaces `LogPrint`) * `LogPrintLevel(CATEGORY, LEVEL, ...)` for when the level isn't known in advance, or a category needs to be added for an info/warning/error log message (mostly unchanged, but rarely needed) Logs look roughly as they do now with `LogInfo` not having an `[info]` prefix, and `LogDebug` having a `[cat]` prefix, rather than a `[cat:debug]` prefix. This removes `BCLog::Level::None` entirely -- for `LogFlags::NONE` just use `Level::Info`, for any actual category, use `Level::Debug`. Adds docs to developer-notes about when to use which level. Adds `-loglevelalways=1` option so that you get `[net:debug]`, `[all:info]`, `[all:warning]` etc, which might be helpful for automated parsing, or just if you like everything to be consistent. Defaults to off to reduce noise in the default config, and to avoid unnecessary changes on upgrades. Changes the behaviour of `LogPrintLevel(CATEGORY, BCLog::Level::Info, ...)` to be logged unconditionally, rather than only being an additional optional logging level in addition to trace and debug. Does not change the behaviour of `LogPrintLevel(NONE, Debug, ...)` and `LogPrintLevel(NONE, Trace, ...)` being no-ops. ACKs for top commit: maflcko: re-ACK e60fc7d 🌚 achow101: ACK e60fc7d stickies-v: ACK e60fc7d jamesob: ACK e60fc7d ([`jamesob/ackr/28318.1.ajtowns.logging_simplify_api_for`](https://github.com/jamesob/bitcoin/tree/ackr/28318.1.ajtowns.logging_simplify_api_for)) Tree-SHA512: e7a4588779b148242495b7b6f64198a00c314cd57100affab11c43e9d39c9bbf85118ee2002792087fdcffdea08c84576e20844b3079f27083e26ddd7ca15d7f
2 parents 632a2bb + e60fc7d commit 7ff8e6b

File tree

10 files changed

+138
-54
lines changed

10 files changed

+138
-54
lines changed

doc/developer-notes.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,47 @@ General Bitcoin Core
725725
- *Explanation*: If the test suite is to be updated for a change, this has to
726726
be done first.
727727

728+
Logging
729+
-------
730+
731+
The macros `LogInfo`, `LogDebug`, `LogTrace`, `LogWarning` and `LogError` are available for
732+
logging messages. They should be used as follows:
733+
734+
- `LogDebug(BCLog::CATEGORY, fmt, params...)` is what you want
735+
most of the time, and it should be used for log messages that are
736+
useful for debugging and can reasonably be enabled on a production
737+
system (that has sufficient free storage space). They will be logged
738+
if the program is started with `-debug=category` or `-debug=1`.
739+
Note that `LogPrint(BCLog::CATEGORY, fmt, params...)` is a deprecated
740+
alias for `LogDebug`.
741+
742+
- `LogInfo(fmt, params...)` should only be used rarely, eg for startup
743+
messages or for infrequent and important events such as a new block tip
744+
being found or a new outbound connection being made. These log messages
745+
are unconditional so care must be taken that they can't be used by an
746+
attacker to fill up storage. Note that `LogPrintf(fmt, params...)` is
747+
a deprecated alias for `LogInfo`.
748+
749+
- `LogError(fmt, params...)` should be used in place of `LogInfo` for
750+
severe problems that require the node (or a subsystem) to shut down
751+
entirely (eg, insufficient storage space).
752+
753+
- `LogWarning(fmt, params...)` should be used in place of `LogInfo` for
754+
severe problems that the node admin should address, but are not
755+
severe enough to warrant shutting down the node (eg, system time
756+
appears to be wrong, unknown soft fork appears to have activated).
757+
758+
- `LogTrace(BCLog::CATEGORY, fmt, params...) should be used in place of
759+
`LogDebug` for log messages that would be unusable on a production
760+
system, eg due to being too noisy in normal use, or too resource
761+
intensive to process. These will be logged if the startup
762+
options `-debug=category -loglevel=category:trace` or `-debug=1
763+
-loglevel=trace` are selected.
764+
765+
Note that the format strings and parameters of `LogDebug` and `LogTrace`
766+
are only evaluated if the logging category is enabled, so you must be
767+
careful to avoid side-effects in those expressions.
768+
728769
Wallet
729770
-------
730771

@@ -887,7 +928,7 @@ Strings and formatting
887928
`wcstoll`, `wcstombs`, `wcstoul`, `wcstoull`, `wcstoumax`, `wcswidth`,
888929
`wcsxfrm`, `wctob`, `wctomb`, `wctrans`, `wctype`, `wcwidth`, `wprintf`
889930
890-
- For `strprintf`, `LogPrint`, `LogPrintf` formatting characters don't need size specifiers.
931+
- For `strprintf`, `LogInfo`, `LogDebug`, etc formatting characters don't need size specifiers.
891932
892933
- *Rationale*: Bitcoin Core uses tinyformat, which is type safe. Leave them out to avoid confusion.
893934
@@ -899,7 +940,7 @@ Strings and formatting
899940
900941
- *Rationale*: Although this is guaranteed to be safe starting with C++11, `.data()` communicates the intent better.
901942
902-
- Do not use it when passing strings to `tfm::format`, `strprintf`, `LogPrint[f]`.
943+
- Do not use it when passing strings to `tfm::format`, `strprintf`, `LogInfo`, `LogDebug`, etc.
903944
904945
- *Rationale*: This is redundant. Tinyformat handles strings.
905946

src/httpserver.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ bool InitHTTPServer(const util::SignalInterrupt& interrupt)
463463

464464
LogPrint(BCLog::HTTP, "Initialized HTTP server\n");
465465
int workQueueDepth = std::max((long)gArgs.GetIntArg("-rpcworkqueue", DEFAULT_HTTP_WORKQUEUE), 1L);
466-
LogPrintfCategory(BCLog::HTTP, "creating work queue of depth %d\n", workQueueDepth);
466+
LogDebug(BCLog::HTTP, "creating work queue of depth %d\n", workQueueDepth);
467467

468468
g_work_queue = std::make_unique<WorkQueue<HTTPClosure>>(workQueueDepth);
469469
// transfer ownership to eventBase/HTTP via .release()
@@ -485,9 +485,8 @@ static std::vector<std::thread> g_thread_http_workers;
485485

486486
void StartHTTPServer()
487487
{
488-
LogPrint(BCLog::HTTP, "Starting HTTP server\n");
489488
int rpcThreads = std::max((long)gArgs.GetIntArg("-rpcthreads", DEFAULT_HTTP_THREADS), 1L);
490-
LogPrintfCategory(BCLog::HTTP, "starting %d worker threads\n", rpcThreads);
489+
LogInfo("Starting HTTP server with %d worker threads\n", rpcThreads);
491490
g_thread_http = std::thread(ThreadHTTP, eventBase);
492491

493492
for (int i = 0; i < rpcThreads; i++) {

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15451545
if (status == node::ChainstateLoadStatus::SUCCESS) {
15461546
uiInterface.InitMessage(_("Verifying blocks…").translated);
15471547
if (chainman.m_blockman.m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
1548-
LogPrintfCategory(BCLog::PRUNE, "pruned datadir may not have more than %d blocks; only checking available blocks\n",
1548+
LogWarning("pruned datadir may not have more than %d blocks; only checking available blocks\n",
15491549
MIN_BLOCKS_TO_KEEP);
15501550
}
15511551
std::tie(status, error) = catch_exceptions([&]{ return VerifyLoadedChainstate(chainman, options);});

src/init/common.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ void AddLoggingArgs(ArgsManager& argsman)
4040
#endif
4141
argsman.AddArg("-logsourcelocations", strprintf("Prepend debug output with name of the originating source location (source file, line number and function name) (default: %u)", DEFAULT_LOGSOURCELOCATIONS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4242
argsman.AddArg("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
43+
argsman.AddArg("-loglevelalways", strprintf("Always prepend a category and level (default: %u)", DEFAULT_LOGLEVELALWAYS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4344
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -daemon. To disable logging to file, set -nodebuglogfile)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4445
argsman.AddArg("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
4546
}
@@ -55,6 +56,7 @@ void SetLoggingOptions(const ArgsManager& args)
5556
LogInstance().m_log_threadnames = args.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
5657
#endif
5758
LogInstance().m_log_sourcelocations = args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
59+
LogInstance().m_always_print_category_level = args.GetBoolArg("-loglevelalways", DEFAULT_LOGLEVELALWAYS);
5860

5961
fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
6062
}

src/logging.cpp

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
126126

127127
bool BCLog::Logger::WillLogCategoryLevel(BCLog::LogFlags category, BCLog::Level level) const
128128
{
129-
// Log messages at Warning and Error level unconditionally, so that
129+
// Log messages at Info, Warning and Error level unconditionally, so that
130130
// important troubleshooting information doesn't get lost.
131-
if (level >= BCLog::Level::Warning) return true;
131+
if (level >= BCLog::Level::Info) return true;
132132

133133
if (!WillLogCategory(category)) return false;
134134

@@ -202,7 +202,7 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
202202
return false;
203203
}
204204

205-
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level) const
205+
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level)
206206
{
207207
switch (level) {
208208
case BCLog::Level::Trace:
@@ -215,8 +215,6 @@ std::string BCLog::Logger::LogLevelToStr(BCLog::Level level) const
215215
return "warning";
216216
case BCLog::Level::Error:
217217
return "error";
218-
case BCLog::Level::None:
219-
return "";
220218
}
221219
assert(false);
222220
}
@@ -307,8 +305,6 @@ static std::optional<BCLog::Level> GetLogLevel(const std::string& level_str)
307305
return BCLog::Level::Warning;
308306
} else if (level_str == "error") {
309307
return BCLog::Level::Error;
310-
} else if (level_str == "none") {
311-
return BCLog::Level::None;
312308
} else {
313309
return std::nullopt;
314310
}
@@ -341,7 +337,7 @@ static constexpr std::array<BCLog::Level, 3> LogLevelsList()
341337
std::string BCLog::Logger::LogLevelsString() const
342338
{
343339
const auto& levels = LogLevelsList();
344-
return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ", [this](BCLog::Level level) { return LogLevelToStr(level); });
340+
return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ", [](BCLog::Level level) { return LogLevelToStr(level); });
345341
}
346342

347343
std::string BCLog::Logger::LogTimestampStr(const std::string& str)
@@ -392,29 +388,39 @@ namespace BCLog {
392388
}
393389
} // namespace BCLog
394390

395-
void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
391+
std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level level) const
396392
{
397-
StdLockGuard scoped_lock(m_cs);
398-
std::string str_prefixed = LogEscapeMessage(str);
393+
if (category == LogFlags::NONE) category = LogFlags::ALL;
399394

400-
if ((category != LogFlags::NONE || level != Level::None) && m_started_new_line) {
401-
std::string s{"["};
395+
const bool has_category{m_always_print_category_level || category != LogFlags::ALL};
402396

403-
if (category != LogFlags::NONE) {
404-
s += LogCategoryToStr(category);
405-
}
397+
// If there is no category, Info is implied
398+
if (!has_category && level == Level::Info) return {};
406399

407-
if (category != LogFlags::NONE && level != Level::None) {
408-
// Only add separator if both flag and level are not NONE
409-
s += ":";
410-
}
400+
std::string s{"["};
401+
if (has_category) {
402+
s += LogCategoryToStr(category);
403+
}
411404

412-
if (level != Level::None) {
413-
s += LogLevelToStr(level);
414-
}
405+
if (m_always_print_category_level || !has_category || level != Level::Debug) {
406+
// If there is a category, Debug is implied, so don't add the level
407+
408+
// Only add separator if we have a category
409+
if (has_category) s += ":";
410+
s += Logger::LogLevelToStr(level);
411+
}
412+
413+
s += "] ";
414+
return s;
415+
}
415416

416-
s += "] ";
417-
str_prefixed.insert(0, s);
417+
void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
418+
{
419+
StdLockGuard scoped_lock(m_cs);
420+
std::string str_prefixed = LogEscapeMessage(str);
421+
422+
if (m_started_new_line) {
423+
str_prefixed.insert(0, GetLogPrefix(category, level));
418424
}
419425

420426
if (m_log_sourcelocations && m_started_new_line) {

src/logging.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ static const bool DEFAULT_LOGIPS = false;
2525
static const bool DEFAULT_LOGTIMESTAMPS = true;
2626
static const bool DEFAULT_LOGTHREADNAMES = false;
2727
static const bool DEFAULT_LOGSOURCELOCATIONS = false;
28+
static constexpr bool DEFAULT_LOGLEVELALWAYS = false;
2829
extern const char * const DEFAULT_DEBUGLOGFILE;
2930

3031
extern bool fLogIPs;
@@ -77,7 +78,6 @@ namespace BCLog {
7778
Info, // Default
7879
Warning,
7980
Error,
80-
None, // Internal use only
8181
};
8282
constexpr auto DEFAULT_LOG_LEVEL{Level::Debug};
8383

@@ -120,10 +120,13 @@ namespace BCLog {
120120
bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
121121
bool m_log_threadnames = DEFAULT_LOGTHREADNAMES;
122122
bool m_log_sourcelocations = DEFAULT_LOGSOURCELOCATIONS;
123+
bool m_always_print_category_level = DEFAULT_LOGLEVELALWAYS;
123124

124125
fs::path m_file_path;
125126
std::atomic<bool> m_reopen_file{false};
126127

128+
std::string GetLogPrefix(LogFlags category, Level level) const;
129+
127130
/** Send a string to the log output */
128131
void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level);
129132

@@ -194,7 +197,7 @@ namespace BCLog {
194197
std::string LogLevelsString() const;
195198

196199
//! Returns the string representation of a log level.
197-
std::string LogLevelToStr(BCLog::Level level) const;
200+
static std::string LogLevelToStr(BCLog::Level level);
198201

199202
bool DefaultShrinkDebugFile() const;
200203
};
@@ -234,22 +237,17 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st
234237
#define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
235238

236239
// Log unconditionally.
237-
#define LogPrintf(...) LogPrintLevel_(BCLog::LogFlags::NONE, BCLog::Level::None, __VA_ARGS__)
240+
#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
241+
#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, __VA_ARGS__)
242+
#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, __VA_ARGS__)
238243

239-
// Log unconditionally, prefixing the output with the passed category name.
240-
#define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__)
244+
// Deprecated unconditional logging.
245+
#define LogPrintf(...) LogInfo(__VA_ARGS__)
246+
#define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::Info, __VA_ARGS__)
241247

242248
// Use a macro instead of a function for conditional logging to prevent
243249
// evaluating arguments when logging for the category is not enabled.
244250

245-
// Log conditionally, prefixing the output with the passed category name.
246-
#define LogPrint(category, ...) \
247-
do { \
248-
if (LogAcceptCategory((category), BCLog::Level::Debug)) { \
249-
LogPrintLevel_(category, BCLog::Level::None, __VA_ARGS__); \
250-
} \
251-
} while (0)
252-
253251
// Log conditionally, prefixing the output with the passed category name and severity level.
254252
#define LogPrintLevel(category, level, ...) \
255253
do { \
@@ -258,6 +256,13 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st
258256
} \
259257
} while (0)
260258

259+
// Log conditionally, prefixing the output with the passed category name.
260+
#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
261+
#define LogTrace(category, ...) LogPrintLevel(category, BCLog::Level::Trace, __VA_ARGS__)
262+
263+
// Deprecated conditional logging
264+
#define LogPrint(category, ...) LogDebug(category, __VA_ARGS__)
265+
261266
template <typename... Args>
262267
bool error(const char* fmt, const Args&... args)
263268
{

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4405,7 +4405,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
44054405
}
44064406

44074407
if (received_new_header) {
4408-
LogPrintfCategory(BCLog::NET, "Saw new cmpctblock header hash=%s peer=%d\n",
4408+
LogInfo("Saw new cmpctblock header hash=%s peer=%d\n",
44094409
blockhash.ToString(), pfrom.GetId());
44104410
}
44114411

src/test/logging_tests.cpp

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,32 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
8484
{
8585
LogInstance().m_log_sourcelocations = true;
8686
LogPrintf_("fn1", "src1", 1, BCLog::LogFlags::NET, BCLog::Level::Debug, "foo1: %s\n", "bar1");
87-
LogPrintf_("fn2", "src2", 2, BCLog::LogFlags::NET, BCLog::Level::None, "foo2: %s\n", "bar2");
88-
LogPrintf_("fn3", "src3", 3, BCLog::LogFlags::NONE, BCLog::Level::Debug, "foo3: %s\n", "bar3");
89-
LogPrintf_("fn4", "src4", 4, BCLog::LogFlags::NONE, BCLog::Level::None, "foo4: %s\n", "bar4");
87+
LogPrintf_("fn2", "src2", 2, BCLog::LogFlags::NET, BCLog::Level::Info, "foo2: %s\n", "bar2");
88+
LogPrintf_("fn3", "src3", 3, BCLog::LogFlags::ALL, BCLog::Level::Debug, "foo3: %s\n", "bar3");
89+
LogPrintf_("fn4", "src4", 4, BCLog::LogFlags::ALL, BCLog::Level::Info, "foo4: %s\n", "bar4");
90+
LogPrintf_("fn5", "src5", 5, BCLog::LogFlags::NONE, BCLog::Level::Debug, "foo5: %s\n", "bar5");
91+
LogPrintf_("fn6", "src6", 6, BCLog::LogFlags::NONE, BCLog::Level::Info, "foo6: %s\n", "bar6");
9092
std::ifstream file{tmp_log_path};
9193
std::vector<std::string> log_lines;
9294
for (std::string log; std::getline(file, log);) {
9395
log_lines.push_back(log);
9496
}
9597
std::vector<std::string> expected = {
96-
"[src1:1] [fn1] [net:debug] foo1: bar1",
97-
"[src2:2] [fn2] [net] foo2: bar2",
98+
"[src1:1] [fn1] [net] foo1: bar1",
99+
"[src2:2] [fn2] [net:info] foo2: bar2",
98100
"[src3:3] [fn3] [debug] foo3: bar3",
99101
"[src4:4] [fn4] foo4: bar4",
102+
"[src5:5] [fn5] [debug] foo5: bar5",
103+
"[src6:6] [fn6] foo6: bar6",
100104
};
101105
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
102106
}
103107

104-
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
108+
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacrosDeprecated, LogSetup)
105109
{
106110
LogPrintf("foo5: %s\n", "bar5");
107111
LogPrint(BCLog::NET, "foo6: %s\n", "bar6");
112+
LogPrintLevel(BCLog::NET, BCLog::Level::Trace, "foo4: %s\n", "bar4"); // not logged
108113
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo7: %s\n", "bar7");
109114
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
110115
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
@@ -118,11 +123,32 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
118123
std::vector<std::string> expected = {
119124
"foo5: bar5",
120125
"[net] foo6: bar6",
121-
"[net:debug] foo7: bar7",
126+
"[net] foo7: bar7",
122127
"[net:info] foo8: bar8",
123128
"[net:warning] foo9: bar9",
124129
"[net:error] foo10: bar10",
125-
"[validation] foo11: bar11",
130+
"[validation:info] foo11: bar11",
131+
};
132+
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
133+
}
134+
135+
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
136+
{
137+
LogTrace(BCLog::NET, "foo6: %s\n", "bar6"); // not logged
138+
LogDebug(BCLog::NET, "foo7: %s\n", "bar7");
139+
LogInfo("foo8: %s\n", "bar8");
140+
LogWarning("foo9: %s\n", "bar9");
141+
LogError("foo10: %s\n", "bar10");
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);
146+
}
147+
std::vector<std::string> expected = {
148+
"[net] foo7: bar7",
149+
"foo8: bar8",
150+
"[warning] foo9: bar9",
151+
"[error] foo10: bar10",
126152
};
127153
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
128154
}

src/torcontrol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ void TorController::add_onion_cb(TorControlConnection& _conn, const TorControlRe
433433
return;
434434
}
435435
service = LookupNumeric(std::string(service_id+".onion"), Params().GetDefaultPort());
436-
LogPrintfCategory(BCLog::TOR, "Got service ID %s, advertising service %s\n", service_id, service.ToStringAddrPort());
436+
LogInfo("Got tor service ID %s, advertising service %s\n", service_id, service.ToStringAddrPort());
437437
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
438438
LogPrint(BCLog::TOR, "Cached service private key to %s\n", fs::PathToString(GetPrivateKeyFile()));
439439
} else {

0 commit comments

Comments
 (0)