Skip to content

Conversation

@practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Aug 25, 2020

Prefix log messages with function name if -logfunctionnames is set.

Yes, exactly like -logthreadnames but for function names instead of thread names :)

This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function.

For me it beats the ordinary cycle of 1.) try to figure out a regexp matching the static part of the dynamic log message, 2.) git grep -E 'Using .* MiB out of .* requested for signature cache', 3.) mcedit filename.cpp (openemacs filename.cpp works too!) and 4.) search for log message and scroll up to find the function name :)

Without any logging parameters:

$ src/bitcoind -regtest
2020-08-25T03:29:04Z Using RdRand as an additional entropy source
2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2020-08-25T03:29:04Z Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000
2020-08-25T03:29:04Z block tree size = 1
2020-08-25T03:29:04Z nBestHeight = 0
2020-08-25T03:29:04Z Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2020-08-25T03:29:04Z 0 addresses found from DNS seeds

With -logthreadnames and -logfunctionnames:

$ src/bitcoind -regtest -logthreadnames -logfunctionnames
2020-08-25T03:29:04Z [init] [ReportHardwareRand] Using RdRand as an additional entropy source
2020-08-25T03:29:04Z [init] [InitSignatureCache] Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2020-08-25T03:29:04Z [init] [InitScriptExecutionCache] Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
2020-08-25T03:29:04Z [init] [LoadChainTip] Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000
2020-08-25T03:29:04Z [init] [AppInitMain] block tree size = 1
2020-08-25T03:29:04Z [init] [AppInitMain] nBestHeight = 0
2020-08-25T03:29:04Z [loadblk] [LoadMempool] Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
2020-08-25T03:29:04Z [dnsseed] [ThreadDNSAddressSeed] 0 addresses found from DNS seeds

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 26, 2020

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

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.

@theStack
Copy link
Contributor

Concept ACK
(although I would suggest improving the PR description with s/mcedit/vim 😜)
Nice time-saving idea -- I think many core devs are familiar with your described cycle and could benefit from this.
After this gets into master, I suggest also passing it by default to nodes in the test framework.

@laanwj
Copy link
Member

laanwj commented Aug 27, 2020

Concept ACK. After this we can start removing the explicit __func__ in logging calls, I guess, which is very inconsistently used.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

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

Concept ACK.

Use in test framework?

@jamesob
Copy link
Contributor

jamesob commented Sep 1, 2020

Concept ACK, will review in the next few days

@laanwj
Copy link
Member

laanwj commented Sep 1, 2020

Code review and tested ACK bd71653bfc312b10ca6b578b7c3accd0155b1a20
I think this is great for developers, but for the sake of not making default logging too wide / spammy I would prefer not to enable it by default (but it isn't in this PR).

Edit: I somewhat wonder how much larger this makes the executable. This adds a string for every function that has logging. I don't think it's too bad, but will test.
edit.2: the difference is negligible, these are stripped sizes:

-rwxrwxr-x 1 7.3M Sep  1 15:15 src/bitcoind
-rwxrwxr-x 1 7.4M Sep  1 14:59 src/bitcoind.19809

@instagibbs
Copy link
Member

Ooh, concept ACK

@instagibbs
Copy link
Member

Wouldn't hurt I think to have this active for a single test at least?

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Tested ACK bd71653bfc312b10ca6b578b7c3accd0155b1a20 🧪
Note that in some cases the showed function name is quite generic and doesn't prevent the need for grepping contents of the message (as elaborated in the PR description), that is when LogPrint{f} is called in another logging wrapper function (e.g. WalletLogPrintf(...), Timer::Log(...) or ArgsManager::logArgsPrefix(...)).
Still an incredibly useful feature though that will save devs plenty of time in the future for sure!

@sipa
Copy link
Member

sipa commented Sep 15, 2020

It seems I'll be in a minority here, but I don't think this is very useful in the current form.

If the goal is to avoid the need for searching the source code to find where a message originated, it would be better to include file/line number instead of (or at least in addition to) the function name. Function names can be ambiguous, and do occasionally move from one file to another, so just that still means you need to grep the source code to find where the function is.

@theStack
Copy link
Contributor

theStack commented Sep 15, 2020

@sipa: Agreed that the PR doesn't eliminate the need to grep completely, but I think the main focus is to get rid of the burdensome part of figuring out what to grep in the first place (as expressed in the PR description "try to figure out a regexp matching the static part of the dynamic log message"). That can sometimes take quite some iterations or is not even possible at all of the log message is assembled in a highly dynamic way (i.e. lots of %s in the log format string).

Also, the function name grep is more effective as it not only shows you in which function the log is printed (single result), but also already at which places this function is declared/defined and called. For my purposes thats quite a lot of useful information about the causes of a log message after a single no-brainer grep considering I didn't even need to open a file yet at this point :)

That said, also showing file and line number can for sure also be helpful. Maybe via verbosity levels?

@sipa
Copy link
Member

sipa commented Sep 16, 2020

"try to figure out a regexp matching the static part of the dynamic log message"

That's a good point, I had missed this. I think it can be partially addressed with just better log messages, but this PR is a useful catch-all improvement with little code changes.

@practicalswift
Copy link
Contributor Author

@instagibbs

Wouldn't hurt I think to have this active for a single test at least?

Now enabled for the unit tests and the functional tests.

@theStack

That said, also showing file and line number can for sure also be helpful. Maybe via verbosity levels?

I think that should be a separate option (-logsourcelocation perhaps?). However, that is out of scope for this PR.

@practicalswift practicalswift force-pushed the logfunctionnames branch 2 times, most recently from b9a2c64 to b0287dc Compare October 13, 2020 13:31
Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

ACK b0287dc4fa83e5d3c95edbf9bc120e222f9e1a22
Verified that since my last tested ACK, another commit has been added that activates the feature for unit tests, and for functional tests starting with 0.21.0 (minimum is set to 0.20.99 to also enable it on the master branch before the 0.21.0 release) 👌

@practicalswift
Copy link
Contributor Author

Anything left to do here? :)

@practicalswift practicalswift force-pushed the logfunctionnames branch 2 times, most recently from 49487d3 to 8cbeb9e Compare December 1, 2020 14:57
@practicalswift
Copy link
Contributor Author

Rebased and bumped version check :)

@jonasschnelli
Copy link
Contributor

Concept ACK

Copy link
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

re-ACK 8cbeb9ec8650d6fe25afaae8b2a5f91aab00f2f7 🏞️

@jnewbery
Copy link
Contributor

Concept ACK, if only to remove the __func__ and explicit function names in existing logs.

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

ACK 8cbeb9ec8650d6fe25afaae8b2a5f91aab00f2f7

@ajtowns
Copy link
Contributor

ajtowns commented Jan 14, 2021

This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function.

Wouldn't it be slightly better to also include the line number (or filename and line) as well then?

@maflcko
Copy link
Member

maflcko commented Jan 14, 2021

@practicalswift Mind replying to the open questions in any way? Would be good to make progress here.

@practicalswift
Copy link
Contributor Author

practicalswift commented Jan 14, 2021

This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function.

Wouldn't it be slightly better to also include the line number (or filename and line) as well then?

Sure, that's a good idea but that would be out of scope for -logfunctionnames which this PR adds.

A follow-up PR can add say -logsourcelocation: I'd be glad to Concept ACK and review that :)

@ajtowns
Copy link
Contributor

ajtowns commented Jan 14, 2021

This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function.

Wouldn't it be slightly better to also include the line number (or filename and line) as well then?

Sure, that's a good idea but that would be out of scope for -logfunctionnames which this PR adds.

A follow-up PR can add say -logsourcelocation: I'd be glad to Concept ACK and review that :)

Having two options seems like overkill; why not make this one introduce -logsourcelocation which adds file/line/func to the logs? Don't feel particularly strongly either way, but grepping for "foo.cpp:123" does seem super easy.

@maflcko
Copy link
Member

maflcko commented Jan 14, 2021

review ACK 9f5fc7eb55f8a19f6002f4cfe92e76c574ee8749

I am also ok with printing the file:line as well when -logfunctionnames is set.

@practicalswift
Copy link
Contributor Author

practicalswift commented Jan 14, 2021

@ajtowns

Having two options seems like overkill; why not make this one introduce -logsourcelocation which adds file/line/func to the logs

@MarcoFalke

I am also ok with printing the file:line as well when -logfunctionnames is set.

Being the very flexible person I am I have now added the file:line feature to satisfy the overwhelming public demand :)

Live demo:

$ src/bitcoind -regtest -logthreadnames -logfunctionnames
…
2021-01-14T12:34:56Z [init] [validation.cpp:4174] [LoadChainTip] Loaded best chain: hashBestChain=0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206 height=0 date=2011-02-02T23:16:42Z progress=1.000000

Please re-review! :)

@maflcko
Copy link
Member

maflcko commented Jan 14, 2021

review ACK 09b974587ff90834c3559c15b3ab7d9489c65d65

Copy link
Contributor

@ajtowns ajtowns left a comment

Choose a reason for hiding this comment

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

ACK 09b974587ff90834c3559c15b3ab7d9489c65d65 -- minimal testing only

For functions defined in header files, I'm seeing these logged as

2021-01-15T06:44:32Z [./util/system.h:449] [TraceThread] torcontrol thread start

with a leading "./" -- might be due to us using #include <> which implies an include path search?

Copy link

@btcslade btcslade left a comment

Choose a reason for hiding this comment

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

ACK 09b974587ff90834c3559c15b3ab7d9489c65d65

@practicalswift
Copy link
Contributor Author

@ajtowns

Thanks for reviewing!

2021-01-15T06:44:32Z [./util/system.h:449] [TraceThread] torcontrol thread start

with a leading "./" -- might be due to us using #include <> which implies an include path search?

Now removing leading ./ which gives us:

2021-01-15T06:44:32Z [util/system.h:449] [TraceThread] torcontrol thread start

Please re-review :)

@practicalswift practicalswift changed the title log: Prefix log messages with function name if -logfunctionnames is set log: Prefix log messages with function name and source code location if -logsourcelocations is set Jan 15, 2021
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

review ACK b4511e2 🌃

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

review ACK b4511e2e2ed1a6077ae6826a9ee6b7a311293d08 🌃
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUgUpQv+KjsWdQmlTRct27nvZGqCCHtsB6PCiZiAO9uywglBEBu6cTsDnGsVnH3h
EyJYPc2a19PaY8MH8uocTrR7owPl7TgqaufvhFPWd63pkg6WLk3W9R+eot//mOFV
za90hjk8oexnnPxaREWqXLJh7qQxNwYhGirpOdQ+gId35a5y/qYPumaPN97oZTQg
7xU85Ik2V+kAdD7y/6qUfB82ZRlwjfirSl9MdctznaXsaQLuQoGbkSdoliZNdl8e
D6xCplsSq7Sxy8cLnzpKHeX/7ouZTv/yeeLRD874kJX/CLzij2x5Pg7Hs2dnCT9f
zoq8Nu1BR9O2hoRf7rKbJva4tZBOHUKdmDOeUX8ozkwmcA8U2YMBr0Y6QvZZJX8H
QvDpkV513nUAj2BjjLxQOAkJAwi+hCcQu8LWJVN+BMD6iTZ6IJ0zJws8bdXCq4Fk
ernWM0gjqDfpaDy4O3Ita+MQsKS9ylM5ojjMKBKG+XJdZiC8vqjbDq4cXPtJ9E1x
HLVdMr8S
=ezwm
-----END PGP SIGNATURE-----

Timestamp of file with hash 1aea386739b0674d3af7837475dd6afb7545a269fc384634b14a41a5d3a0d80d -

@laanwj
Copy link
Member

laanwj commented Feb 18, 2021

Code review ACK b4511e2

@laanwj laanwj merged commit b805dbb into bitcoin:master Feb 18, 2021
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Feb 18, 2021
…source code location if -logsourcelocations is set

b4511e2 log: Prefix log messages with function name if -logsourcelocations is set (practicalswift)

Pull request description:

  Prefix log messages with function name if `-logfunctionnames` is set.

  Yes, exactly like `-logthreadnames` but for function names instead of thread names :)

  This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function.

  For me it beats the ordinary cycle of 1.) try to figure out a regexp matching the static part of the dynamic log message, 2.) `git grep -E 'Using .* MiB out of .* requested for signature cache'`, 3.) `mcedit filename.cpp` (`openemacs filename.cpp` works too!) and 4.) search for log message and scroll up to find the function name :)

  Without any logging parameters:

  ```
  $ src/bitcoind -regtest
  2020-08-25T03:29:04Z Using RdRand as an additional entropy source
  2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
  2020-08-25T03:29:04Z Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
  2020-08-25T03:29:04Z Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000
  2020-08-25T03:29:04Z block tree size = 1
  2020-08-25T03:29:04Z nBestHeight = 0
  2020-08-25T03:29:04Z Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
  2020-08-25T03:29:04Z 0 addresses found from DNS seeds
  ```

  With `-logthreadnames` and `-logfunctionnames`:

  ```
  $ src/bitcoind -regtest -logthreadnames -logfunctionnames
  2020-08-25T03:29:04Z [init] [ReportHardwareRand] Using RdRand as an additional entropy source
  2020-08-25T03:29:04Z [init] [InitSignatureCache] Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
  2020-08-25T03:29:04Z [init] [InitScriptExecutionCache] Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
  2020-08-25T03:29:04Z [init] [LoadChainTip] Loaded best chain: hashBestChain=0fff88f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e22ff height=0 date=2011-02-02T23:16:42Z progress=1.000000
  2020-08-25T03:29:04Z [init] [AppInitMain] block tree size = 1
  2020-08-25T03:29:04Z [init] [AppInitMain] nBestHeight = 0
  2020-08-25T03:29:04Z [loadblk] [LoadMempool] Imported mempool transactions from disk: 0 succeeded, 0 failed, 0 expired, 0 already there, 0 waiting for initial broadcast
  2020-08-25T03:29:04Z [dnsseed] [ThreadDNSAddressSeed] 0 addresses found from DNS seeds
  ```

ACKs for top commit:
  laanwj:
    Code review ACK b4511e2
  MarcoFalke:
    review ACK b4511e2 🌃

Tree-SHA512: d100f5364630c323f31d275259864c597f7725e462d5f4bdedcc7033ea616d7fc0d16ef1b2af557e692f4deea73c6773ccfc681589e7bf6ba970b9ec169040c7
@practicalswift practicalswift deleted the logfunctionnames branch April 10, 2021 19:45
Fabcien pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Feb 2, 2022
… set

Summary:
> Prefix log messages with function name if `-logfunctionnames` is set,  exactly like `-logthreadnames but for function names instead of thread names.
>
> This is a small developer ergonomics improvement: I've found this to be a cheap/simple way to correlate log output and originating function.

This is a backport of [[bitcoin/bitcoin#19809 | core#19809]]

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10951
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
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.