Skip to content

log: Remove trailing and embedded newlines from log messages - #36904

Open
maflcko wants to merge 4 commits into
bitcoin:masterfrom
maflcko:2611-less-trailing-newline
Open

maflcko wants to merge 4 commits into
bitcoin:masterfrom
maflcko:2611-less-trailing-newline

Conversation

@maflcko

@maflcko maflcko commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

The log functions sometimes have a trailing newline character. This is perfectly fine, because that char will be stripped again by the log framework. However, it is still confusing:

  • After log: prevent user input from injecting fake log lines #35833 embedded newlines are escaped and a single trailing one is excluded. It would be clearer to not have a special carve-out here. Also the carve-out may be missing two-or-more trailing newlines.
  • The trailing newline may incorrectly imply that newlines are supported at all.

So remove them, and also fix a few places to remove embedded newlines.

@maflcko
maflcko marked this pull request as draft November 17, 2026 21:01
@DrahtBot

DrahtBot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK jeanpablojp, stickies-v

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #37094 (validation: validate assumeutxo snapshots with a single chainstate by fjahr)
  • #37060 (zmq: improve handling of invalid configurations by jlopp)
  • #37038 (coins: store spent dbcache entries compactly by LarryRuane)
  • #36980 (log: mask SENSITIVE rw_settings in LogArgs by enessubass)
  • #36969 (net: Warn only once when a proxy or the I2P proxy is unreachable by fjahr)
  • #36858 (refactor: break the blockstorage/validation circular dependency by pzafonte)
  • #36856 (kernel: add manual pruning by pzafonte)
  • #36820 (refactor: remove more dead code by fanquake)
  • #36738 (refactor: move block writes and pruning into BlockManager by pzafonte)
  • #36540 (mempool: remove time-based transaction expiry by ismaelsadeeq)
  • #36434 (http: support unix sockets by pinheadmz)
  • #36326 (kernel: Use typed errors for fatal and flush error notifications by arejula27)
  • #35998 (wallet: Handle or explicitly ignore WalletBatch write failures by achow101)
  • #35906 (First steps towards a stateless, side-effect free validation library by purpleKarrot)
  • #35902 (zmq: Log bind error at Error level, abort startup on init error by chriszeng1010)
  • #35591 ([DO NOT MERGE] Erlay: bandwidth-efficient transaction relay protocol (Full implementation) by sr-gi)
  • #35355 (Use atomics for determining whether trace logging is enabled by ajtowns)
  • #34778 (logging: rewrite macros to enforce restrictions at compile-time, improve efficiency and usability by ryanofsky)
  • #34534 (rpc: Manual prune lock management (Take 2) by fjahr)
  • #34400 (wallet: parallel fast rescan by Eunovo)
  • #34132 (coins,dbwrapper: make database point-read failures fatal by l0rinc)
  • #34038 (logging: replace -loglevel with -trace, expose trace logging via RPC by ajtowns)
  • #33324 (blocks: add resumable reobfuscation for existing block files by l0rinc)
  • #29256 (log, refactor: Allow log macros to accept context arguments by ryanofsky)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@maflcko
maflcko force-pushed the 2611-less-trailing-newline branch from 32505bc to fafab6e Compare November 18, 2026 12:35
@maflcko
maflcko marked this pull request as ready for review November 18, 2026 12:46
@maflcko

maflcko commented Nov 18, 2026 •

Copy link
Copy Markdown
Member Author

Ok, went ahead to exclude all reviewed pull requests in the scripted-diff. All remaining conflicts don't have any current review, or are waiting to have their reviews addressed in a push anyway.

Edit: Also pushed a fix to remove the last embedded newlines. Can be tested with something like:

diff --git a/src/logging.cpp b/src/logging.cpp
index f5d184ba33..b2b68ba0c3 100644
--- a/src/logging.cpp
+++ b/src/logging.cpp
@@ -343,2 +343,3 @@ namespace BCLog {
             uint8_t ch = (uint8_t)ch_in;
+ Assert(ch!='\n');
             if (ch >= 32 && ch != '\x7f') {

@jeanpablojp jeanpablojp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

nit: One log format string still has embedded newlines after the CommitTransaction fix, the LogWarning("\n\n************************\n%s", message) in PrintExceptionContinue, which predates this PR.
In feature_init.py it comes out in debug.log as \x0a\x0a************************\x0aEXCEPTION: ....

@maflcko maflcko changed the title scripted-diff: Remove trailing newlines from one-line log messages log: Remove trailing and embedded newlines from log messages Nov 24, 2026
@maflcko
maflcko force-pushed the 2611-less-trailing-newline branch from fa3a813 to fa78dbf Compare November 24, 2026 09:27
@maflcko

maflcko commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

PrintExceptionContinue

Thx, added a fix for that as well. Can be tested via:

diff --git a/src/init.cpp b/src/init.cpp
index 8a7b6d15a2..a592bc76a4 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -1544,2 +1544,4 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
     }
+//throw int{};
+throw std::runtime_error{"hi"};
 

and then: bitcoind -printtoconsole=1.

Before: [init] [util/exception.cpp:39] [PrintExceptionContinue] [warning] \x0a\x0a************************\x0aEXCEPTION: St13runtime_error \x0ahi \x0abitcoin in AppInit()

After: [init] [util/exception.cpp:33] [PrintExceptionContinue] [warning] ***** EXCEPTION thread: init, caught at: bitcoind.cpp:246 type: St13runtime_error what: "hi"

@stickies-v

Copy link
Copy Markdown
Contributor

Concept ACK

MarcoFalke added 4 commits December 1, 2026 14:11
The replacement skips log messages that contain: " or \
This keeps the script simple and allows to handle the special cases
manually.

-BEGIN VERIFY SCRIPT-

 sed --in-place --regexp-extended 's/(Log(Debug|Error|Info|Trace|Warning)\([^"]*"[^"\\]*)\\n"([,)])/\1"\3/g' $(
     git grep -l --extended-regexp 'Log(Debug|Error|Info|Trace|Warning)\(' -- \
          './src' \
          ':(exclude)src/init.cpp' \
          ':(exclude)src/net.cpp' \
          ':(exclude)src/net_processing.cpp' \
          ':(exclude)src/blockencodings.cpp' \
          ':(exclude)src/netbase.cpp'
          # Some active files in other PRs are excluded due to code-conflicts
 )

-END VERIFY SCRIPT-
The stringify function is only used for debugging and all the newline
characters are turned into \x0a escapes.

This is fine, but it would be better to just avoid the newlines in the
first place and use a single line.
This ends up in the debug log, which can not deal with newlines.
The prior function had a lot of issues:

* On Windows it printed the exe file name, which is of questionable
  benefit, when other context is more precise.
* On non-Windows it printed "bitcoin" as the file name, which doesn't
  seem to be helpful either.
* The function accepted a thread_name param, which is not accurate,
  because most call sites passed a function name.
* Finally, the function formatted the log with newlines, which will be
  translated into escapes, like:
  `\x0a\x0a************************\x0aEXCEPTION: ...`

Fix all issues, by:

* Dropping the exe file name and instead formatting the source location
  and optional caller-provided context.
* Dropping the redundantly passed function names from callers, now that
  the source location is used.
* Fetching the thread name from the internal getter instead of manually
  passing it each time.
* Formatting it as a single line, to avoid escaped newlines in "normal"
  exception output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants