Skip to content

Commit f7ce5ac

Browse files
committed
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.
1 parent fbd7642 commit f7ce5ac

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

doc/developer-notes.md

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

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

@@ -891,7 +932,7 @@ Strings and formatting
891932
`wcstoll`, `wcstombs`, `wcstoul`, `wcstoull`, `wcstoumax`, `wcswidth`,
892933
`wcsxfrm`, `wctob`, `wctomb`, `wctrans`, `wctype`, `wcwidth`, `wprintf`
893934
894-
- For `strprintf`, `LogPrint`, `LogPrintf` formatting characters don't need size specifiers.
935+
- For `strprintf`, `LogInfo`, `LogDebug`, etc formatting characters don't need size specifiers.
895936
896937
- *Rationale*: Bitcoin Core uses tinyformat, which is type safe. Leave them out to avoid confusion.
897938
@@ -903,7 +944,7 @@ Strings and formatting
903944
904945
- *Rationale*: Although this is guaranteed to be safe starting with C++11, `.data()` communicates the intent better.
905946
906-
- Do not use it when passing strings to `tfm::format`, `strprintf`, `LogPrint[f]`.
947+
- Do not use it when passing strings to `tfm::format`, `strprintf`, `LogInfo`, `LogDebug`, etc.
907948
908949
- *Rationale*: This is redundant. Tinyformat handles strings.
909950

src/logging.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,12 @@ static inline void LogPrintf_(const std::string& logging_function, const std::st
237237
#define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
238238

239239
// Log unconditionally.
240-
#define LogPrintf(...) LogPrintLevel_(BCLog::LogFlags::NONE, BCLog::Level::Info, __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__)
241243

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

245248
// 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
254257
} while (0)
255258

256259
// Log conditionally, prefixing the output with the passed category name.
257-
#define LogPrint(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
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__)
258265

259266
template <typename... Args>
260267
bool error(const char* fmt, const Args&... args)

src/test/logging_tests.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,11 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
105105
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
106106
}
107107

108-
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
108+
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacrosDeprecated, LogSetup)
109109
{
110110
LogPrintf("foo5: %s\n", "bar5");
111111
LogPrint(BCLog::NET, "foo6: %s\n", "bar6");
112+
LogPrintLevel(BCLog::NET, BCLog::Level::Trace, "foo4: %s\n", "bar4"); // not logged
112113
LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "foo7: %s\n", "bar7");
113114
LogPrintLevel(BCLog::NET, BCLog::Level::Info, "foo8: %s\n", "bar8");
114115
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "foo9: %s\n", "bar9");
@@ -131,6 +132,27 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros, LogSetup)
131132
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
132133
}
133134

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",
152+
};
153+
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
154+
}
155+
134156
BOOST_FIXTURE_TEST_CASE(logging_LogPrintMacros_CategoryName, LogSetup)
135157
{
136158
LogInstance().EnableCategory(BCLog::LogFlags::ALL);

test/lint/lint-format-strings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
'fprintf,1',
2121
'tfm::format,1', # Assuming tfm::::format(std::ostream&, ...
2222
'LogConnectFailure,1',
23+
'LogError,0',
24+
'LogWarning,0',
25+
'LogInfo,0',
26+
'LogDebug,1',
27+
'LogTrace,1',
2328
'LogPrint,1',
2429
'LogPrintf,0',
2530
'LogPrintfCategory,1',

0 commit comments

Comments
 (0)