Use __func__ for log messages #15981

pull Bushstar wants to merge 1 commits into bitcoin:master from Bushstar:replace-all-with-func changing 7 files +52 −52
  1. Bushstar commented at 10:30 AM on May 8, 2019: contributor

    Remove literal function names and print them using func. There are a few updated log messages in this commit that have been moved to a new function but still refer to their previous function, printing the function name with func will resolve this in future. Using func also removes logging code from grep results when searching on function name.

    A previous PR to change a single mislabelled log entry was added here.

    #15979

    This PR includes the change from the previous one and adds every additional entry found by myself and practicalswift.

  2. fanquake added the label Refactoring on May 8, 2019
  3. practicalswift commented at 11:23 AM on May 8, 2019: contributor

    Concept ACK

  4. DrahtBot commented at 1:17 PM on May 8, 2019: member

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #15976 (refactor: move methods under CChainState (pt. 1) by jamesob)
    • #15921 (Tidy up ValidationState interface by jnewbery)
    • #15824 (docs: Improve netbase comments by dongcarl)

    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.

  5. in src/support/lockedpool.cpp:321 in eddd31cfba outdated
     317 | @@ -317,7 +318,7 @@ void LockedPool::free(void *ptr)
     318 |              return;
     319 |          }
     320 |      }
     321 | -    throw std::runtime_error("LockedPool: invalid address not pointing to any arena");
     322 | +    throw std::runtime_error(strprintf("%s: invalid address not pointing to any arena", __func__));
    


    jnewbery commented at 2:01 PM on May 8, 2019:

    I think this changes the log from LockedPool: to free:, which seems less useful


    promag commented at 2:07 PM on May 8, 2019:

    👀


    Bushstar commented at 2:13 PM on May 8, 2019:

    Should either be LockedPool::free or just LockedPool:, this was a mistake to change.


    Bushstar commented at 2:14 PM on May 8, 2019:

    This should not have been included. Will change this back.

  6. jnewbery commented at 2:03 PM on May 8, 2019: member

    Concept ACK for places where this doesn't change the log message.

    There are a few updated log messages in this commit that have been moved to a new function but still refer to their previous function

    There's an argument for not changing these, since any automated log parsing tools may be matching on the old log strings.

  7. in src/validation.cpp:4151 in eddd31cfba outdated
    4147 | @@ -4148,12 +4148,12 @@ bool CChainState::ReplayBlocks(const CChainParams& params, CCoinsView* view)
    4148 |          if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
    4149 |              CBlock block;
    4150 |              if (!ReadBlockFromDisk(block, pindexOld, params.GetConsensus())) {
    4151 | -                return error("RollbackBlock(): ReadBlockFromDisk() failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
    4152 | +                return error("%s: ReadBlockFromDisk() failed at %d, hash=%s", __func__, pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
    


    promag commented at 2:03 PM on May 8, 2019:

    This is different.


    Bushstar commented at 2:34 PM on May 8, 2019:

    That error was first added by sipa in the commit below and has always been RollbackBlock() even though in the ReplayBlocks function. There's a RollforwardBlock function added above ReplayBlocks so I suspect it was a WIP name that did not get updated in the error call.

    https://github.com/bitcoin/bitcoin/commit/013a56aa1af985894b3eaf7c325647b0b74e4456

  8. promag commented at 2:09 PM on May 8, 2019: member

    In some cases it's logged class::func, does it make sense to normalize - either always have the class or just the function?

  9. Bushstar force-pushed on May 8, 2019
  10. Bushstar commented at 2:42 PM on May 8, 2019: contributor

    I personally prefer class::func where applicable, I'll update this PR. Normalising all logging like this is a much bigger task and will conflict with a lot of other PRs, I'll stick to the ones in this PR for now.

  11. Use __func__ for log messages 332c1f25b7
  12. Bushstar force-pushed on May 8, 2019
  13. Bushstar commented at 3:50 PM on May 8, 2019: contributor

    Travis hit the time limit, seems less prone to this in the morning UK, I'll force push again tomorrow morning if it's not been manually run and succeeded by then.

  14. gmaxwell commented at 8:22 PM on May 8, 2019: contributor

    jnewbery: that same argument pretty much argues against doing this, since all these messages will change in refactors. I'm not sure how we ended up with so many function names inside log entries, it isn't generally a good idea: we shouldn't have our errors change due to refactoring. Not just log parsing tools, but also users googling messages.

  15. jnewbery commented at 9:04 PM on May 8, 2019: member

    that same argument pretty much argues against doing this, since all these messages will change in refactors.

    Right, so what do you suggest? Removing the function names from logs entirely?

  16. MarcoFalke commented at 9:13 PM on May 8, 2019: member

    Most of the logs are error conditions and there are two parts of the log message with different purpose:

    • The function name/class name to tell the developer where it happened
    • The verbose text to explain what happened

    I think search engines today can deal with a single word that is off. And including more information for developers can't hurt, as long as it is correct. Not sure how many automated log parsers parse for the error strings including the location...

  17. promag commented at 9:24 PM on May 8, 2019: member

    I think we should feel free to change logs, it isn't like an API, is it?

  18. gmaxwell commented at 2:55 AM on May 10, 2019: contributor

    Changing of the function name will reliably break log parsing. Rather than including potentially changing function names the messages should be descriptive enough that there isn't any trouble finding them (considering how often functions move from file to file it isn't like a search is avoidable!). Then both needs are satisfied.

    Macros like this are primarily useful for automatically generated error messages, e.g. asserts and the like.

  19. MarcoFalke commented at 11:39 AM on May 10, 2019: member

    Couldn't the logs use something like __FILE__ and __LINE__. In combination with the commit from the debug log, this should be sufficient to find the exact location without searching.

  20. sipa commented at 5:08 PM on May 10, 2019: member

    @gmaxwell For new code, with new messages, I'm very much in favor of using descriptive searchable and not-code-organization-dependent message texts. But what do you suggest we do with the code that is already there right now? Messages with function names in them have been in the codebase since before the project was on Github. We can replace them, and break the searchability property once, or we can hardcode them and remain searchable but confusing...

  21. gmaxwell commented at 7:41 PM on May 11, 2019: contributor

    @sipa I could go either way-- but using func is not either of those ways. I would personally prefer that we replace them with strings that don't need to be changed unless the underlying behaviour changes. I would rather that they be potentially misleading then generated by func. No one working on the code itself is going to be confused too long by a log entry with an incorrect function name in it: the first thing you do in the code when dealing with an error is find the line that generated it, and if it's not there then its somewhere else.

    As an aside, file line macros turn a much wider class of changes into binary changes, already when reviewing stuff for being cosmetic only I change macros, having them peppered all over the codebase would make that much harder. Also, it will further cause random failures in log file comparisons: joe user or operations guy doesn't realize that the number in the message is unstable and will differ from version to version.

  22. DrahtBot added the label Needs rebase on Jun 5, 2019
  23. DrahtBot commented at 10:20 AM on June 5, 2019: member

    <!--cf906140f33d8803c4a75a2196329ecb-->Needs rebase

  24. laanwj commented at 1:27 PM on June 13, 2019: member

    I think this is way too controversial for a simple logging change, so I'm going to close this, sorry.

    I'm not sure how we ended up with so many function names inside log entries, it isn't generally a good idea: we shouldn't have our errors change due to refactoring

    I tend to agree with this. It'd make much more sense to prefix messages with a broad category (net, rpc, validation, ...) than the function name which leaks a lot of implementation details for no good reason. This could even be done automatically by the logging API.

  25. laanwj closed this on Jun 13, 2019

  26. laanwj removed the label Needs rebase on Oct 24, 2019
  27. DrahtBot locked this on Dec 16, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 18:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me