Allow LogTrace(), LogDebug(), LogInfo(), LogWarning(), and LogError() macros to accept context arguments to provide more information in log messages and more control over logging to callers.
For example, with this change wallet code can log with (in #30343):
0- WalletLogPrintf("Error: cannot erase address book entry data\n");
1+ LogError(m_log, "Error: cannot erase address book entry data\n");
And kernel code can log with (in #30342):
0- LogDebug(BCLog::VALIDATION, "Block mutated: %s\n", state.ToString());
1+ LogDebug(log, "Block mutated: %s\n", state.ToString());
where m_log / log types can vary and be defined by the subsystem. This allows subsystems to attach extra context to log messages like wallet names, or request ids. It also allows more than a single log stream to exist, and for messages to be logged to specified streams. Having a single global log stream works very well for bitcoind and will not change. But it limits functionality of libbitcoinkernel, forcing all potential users of the library, including applications written in different languages and running in different environments (like jupyter notebooks) to use shared global state and not be able to do things like get an isolated log trace from one operation.
The change is fully backwards compatible and does not require any updates to code that wants to continue using the macros as they are. The implementation of the macros is also improved. They now provide improved error messages when called with the wrong arguments. And they only call underlying ShouldLog Log and Format hooks as needed to avoid unnecessary work. The PR also adds more test coverage and documentation.
Note: Originally this PR also removed some restrictions around passing category constants to log macros to try to make them more consistent, but these changes were too controversial and have been dropped.
This is based on #34778. The non-base commits are: