Skip to content

Conversation

laanwj
Copy link
Member

@laanwj laanwj commented Mar 9, 2017

Instead of having an exception propagate into the program (which at worst causes a crash) when an error happens while formatting a log message, just print a message to the log. This message clearly indicates what log message was formatted wrongly, and what error happened during formatting it.

Addresses #9423.

Before

2017-03-09 10:58:20 

************************
EXCEPTION: St13runtime_error       
tinyformat: Too many conversion specifiers in format string       
bitcoin in AppInit()       



************************
EXCEPTION: St13runtime_error       
tinyformat: Too many conversion specifiers in format string       
bitcoin in AppInit()       

2017-03-09 10:58:20 Shutdown: In progress...
2017-03-09 10:58:20 scheduler thread interrupt
2017-03-09 10:58:20 Shutdown: done

(and process exits)

After

2017-03-09 10:51:50 Error "tinyformat: Too many conversion specifiers in format string" while formatting log message: Erasing %s %s

(and process continues)

@TheBlueMatt
Copy link
Contributor

Looks good, though I'd prefer if we narrow the catch and change TINYFORMAT_ERROR to throw a more narrow exception type. Not for any specific concern, but...man I hate C++ exceptions.

@laanwj
Copy link
Member Author

laanwj commented Mar 10, 2017

So are there any errors during log message formatting that raise runtime_error that you'd want to escalate? We don't have any sensible/specific exception handling so personally I'd prefer to keep it like this.

@laanwj
Copy link
Member Author

laanwj commented Mar 10, 2017

Ehh oops I guess it makes sense to have the catch() only around tfm::format, though, not LogPrintStr (which does I/O).

Instead of having an exception propagate into the program when an
error happens while formatting a log message, just print a message to
the log.

Addresses bitcoin#9423.
@laanwj laanwj force-pushed the 2017_03_handle_exception_tinyformat branch from 9f51fe8 to 3b092bd Compare March 12, 2017 06:58
@laanwj
Copy link
Member Author

laanwj commented Mar 12, 2017

Okay, updated so that only the formatting is guarded and there is a single LogPrintStr call which can still fail for legit reasons such as disk full...

@TheBlueMatt
Copy link
Contributor

Heh, I knew it needed a tighter catch somehow :P. Anyway, if you dont want to have something more specific that's fine.

utACK 3b092bd

Throw tinyformat::format_error on formatting error instead of the
`std::runtime_error`.
@laanwj
Copy link
Member Author

laanwj commented Mar 13, 2017

LIke this? I don't think this is a very interesting place to get started defining more specific exceptions, but we have to start somewhere...

@TheBlueMatt
Copy link
Contributor

TheBlueMatt commented Mar 13, 2017 via email

@laanwj laanwj merged commit b651270 into bitcoin:master Mar 13, 2017
laanwj added a commit that referenced this pull request Mar 13, 2017
b651270 util: Throw tinyformat::format_error on formatting error (Wladimir J. van der Laan)
3b092bd util: Properly handle errors during log message formatting (Wladimir J. van der Laan)

Tree-SHA512: 85e3b7afec2255fc88034187f1abd6060e9421de17ed4e3d918416f393429a99cc2c974b362099aaaff6970549df47664bea4c857c4e46acc0789663201dc541
sickpig referenced this pull request in sickpig/BitcoinUnlimited Jan 11, 2018
b651270 util: Throw tinyformat::format_error on formatting error (Wladimir J. van der Laan)
3b092bd util: Properly handle errors during log message formatting (Wladimir J. van der Laan)

Tree-SHA512: 85e3b7afec2255fc88034187f1abd6060e9421de17ed4e3d918416f393429a99cc2c974b362099aaaff6970549df47664bea4c857c4e46acc0789663201dc541
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request May 12, 2019
…ormatting

b651270 util: Throw tinyformat::format_error on formatting error (Wladimir J. van der Laan)
3b092bd util: Properly handle errors during log message formatting (Wladimir J. van der Laan)

Tree-SHA512: 85e3b7afec2255fc88034187f1abd6060e9421de17ed4e3d918416f393429a99cc2c974b362099aaaff6970549df47664bea4c857c4e46acc0789663201dc541
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request May 16, 2019
…ormatting

b651270 util: Throw tinyformat::format_error on formatting error (Wladimir J. van der Laan)
3b092bd util: Properly handle errors during log message formatting (Wladimir J. van der Laan)

Tree-SHA512: 85e3b7afec2255fc88034187f1abd6060e9421de17ed4e3d918416f393429a99cc2c974b362099aaaff6970549df47664bea4c857c4e46acc0789663201dc541
codablock pushed a commit to dashpay/dash that referenced this pull request May 21, 2019
…ormatting (#2917)

* Merge bitcoin#9963: util: Properly handle errors during log message formatting

b651270 util: Throw tinyformat::format_error on formatting error (Wladimir J. van der Laan)
3b092bd util: Properly handle errors during log message formatting (Wladimir J. van der Laan)

Tree-SHA512: 85e3b7afec2255fc88034187f1abd6060e9421de17ed4e3d918416f393429a99cc2c974b362099aaaff6970549df47664bea4c857c4e46acc0789663201dc541

* "cast" debugMsg to a c string

Signed-off-by: Pasta <Pasta@dash.org>

"cast" debugMsg to a c string pt 2

Signed-off-by: Pasta <Pasta@dash.org>
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Jan 21, 2020
Summary:
b651270 util: Throw tinyformat::format_error on formatting error (Wladimir J. van der Laan)
3b092bd util: Properly handle errors during log message formatting (Wladimir J. van der Laan)

Tree-SHA512: 85e3b7afec2255fc88034187f1abd6060e9421de17ed4e3d918416f393429a99cc2c974b362099aaaff6970549df47664bea4c857c4e46acc0789663201dc541

Merge #10010: util: rename variable to avoid shadowing

9350e13 util: rename variable to avoid shadowing (Pavol Rusnak)

Tree-SHA512: 8abc09fdb134c913e823754f3f02a4d8ef120a73f252fbc1217dbd2bdd4ed4fffce92d823a66d1fe51607dc021065df8826f21274ef26e55d82575e96d07224f

Backport of Core PR9963 and PR10010
bitcoin/bitcoin#9963
bitcoin/bitcoin#10010

Also needed to pull a change from PR12954:
https://github.com/bitcoin/bitcoin/pull/12954/files#diff-772f489c7d0a32de3badbfbcb5fd200dR69

Test Plan:
Change line init.cpp line 1584 to `LogPrintf("Checkpoints will be verified.\n", fCheckpointsEnabled);`
  make check
  ./bitcoind
Verify the following log message appears at start up:
  2020-01-18T00:33:40Z Error "tinyformat: Not enough conversion specifiers in format string" while formatting log message: Checkpoints will be verified.
Undo change to init.cpp and repeat above verifying normal, pre-patch behavior

Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc

Reviewed By: deadalnix, O1 Bitcoin ABC, #bitcoin_abc

Differential Revision: https://reviews.bitcoinabc.org/D5010
barrystyle pushed a commit to PACGlobalOfficial/PAC that referenced this pull request Jan 22, 2020
…ormatting (dashpay#2917)

* Merge bitcoin#9963: util: Properly handle errors during log message formatting

b651270 util: Throw tinyformat::format_error on formatting error (Wladimir J. van der Laan)
3b092bd util: Properly handle errors during log message formatting (Wladimir J. van der Laan)

Tree-SHA512: 85e3b7afec2255fc88034187f1abd6060e9421de17ed4e3d918416f393429a99cc2c974b362099aaaff6970549df47664bea4c857c4e46acc0789663201dc541

* "cast" debugMsg to a c string

Signed-off-by: Pasta <Pasta@dash.org>

"cast" debugMsg to a c string pt 2

Signed-off-by: Pasta <Pasta@dash.org>
furszy added a commit to PIVX-Project/PIVX that referenced this pull request Mar 27, 2020
9fb0a43 util: Throw tinyformat::format_error on formatting error (random-zebra)
67eb699 util: Properly handle errors during log message formatting (random-zebra)
66ec97b Do not evaluate hidden LogPrint arguments (random-zebra)
2713458 util: Remove zero-argument versions of LogPrint and error (random-zebra)
500dfee util: Update tinyformat (random-zebra)
6837887 util: switch LogPrint and error to variadic templates (random-zebra)
0fa578f tinyformat: force USE_VARIADIC_TEMPLATES (random-zebra)

Pull request description:

  this backports the following pull requests from upstream bitcoin:
  - bitcoin#8000 (0fa578f, 6837887)
  > Now that we started using c++11, force use of variadic templates.
  The autodetection may be wonky on some compilers, see discussion here and is unnecessary for us anyhow.

  - bitcoin#8274 (500dfee, 2713458)
  > Updates tinyformat.h to commit c42f/tinyformat@3a33bbf upstream.

  - bitcoin#9417 (66ec97b)
  > There are a few cases where hashes are computed inside LogPrint arguments - where they usually go unused. As LogPrint statements should never have side effects besides printing something, we can avoid the evaluation in this case.
  Advantage: perhaps a small performance improvement; I haven't benchmarked.
  Disadvantage: if we would have statements with side-effects, this could make this a bit harder to debug.

  - bitcoin#9963 (67eb699, 9fb0a43)
  > Instead of having an exception propagate into the program (which at worst causes a crash) when an error happens while formatting a log message, just print a message to the log. This message clearly indicates what log message was formatted wrongly, and what error happened during formatting it.

ACKs for top commit:
  Fuzzbawls:
    ACK 9fb0a43
  furszy:
    cool, utACK 9fb0a43 .

Tree-SHA512: 29a902bb712612ca093a8fe863e9eff1c17d40955bbc37a5ceb6f057068ac6150dfcffd6836a6b0bd6295aa9bffd0925eb69e50f82ccaa7d84215efc274a59a4
akshaynexus pushed a commit to ZENZO-Ecosystem/ZENZO-Core that referenced this pull request Mar 30, 2020
9fb0a43 util: Throw tinyformat::format_error on formatting error (random-zebra)
67eb699 util: Properly handle errors during log message formatting (random-zebra)
66ec97b Do not evaluate hidden LogPrint arguments (random-zebra)
2713458 util: Remove zero-argument versions of LogPrint and error (random-zebra)
500dfee util: Update tinyformat (random-zebra)
6837887 util: switch LogPrint and error to variadic templates (random-zebra)
0fa578f tinyformat: force USE_VARIADIC_TEMPLATES (random-zebra)

Pull request description:

  this backports the following pull requests from upstream bitcoin:
  - bitcoin#8000 (0fa578f, 6837887)
  > Now that we started using c++11, force use of variadic templates.
  The autodetection may be wonky on some compilers, see discussion here and is unnecessary for us anyhow.

  - bitcoin#8274 (500dfee, 2713458)
  > Updates tinyformat.h to commit c42f/tinyformat@3a33bbf upstream.

  - bitcoin#9417 (66ec97b)
  > There are a few cases where hashes are computed inside LogPrint arguments - where they usually go unused. As LogPrint statements should never have side effects besides printing something, we can avoid the evaluation in this case.
  Advantage: perhaps a small performance improvement; I haven't benchmarked.
  Disadvantage: if we would have statements with side-effects, this could make this a bit harder to debug.

  - bitcoin#9963 (67eb699, 9fb0a43)
  > Instead of having an exception propagate into the program (which at worst causes a crash) when an error happens while formatting a log message, just print a message to the log. This message clearly indicates what log message was formatted wrongly, and what error happened during formatting it.

ACKs for top commit:
  Fuzzbawls:
    ACK 9fb0a43
  furszy:
    cool, utACK 9fb0a43 .

Tree-SHA512: 29a902bb712612ca093a8fe863e9eff1c17d40955bbc37a5ceb6f057068ac6150dfcffd6836a6b0bd6295aa9bffd0925eb69e50f82ccaa7d84215efc274a59a4
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants