Buffer log messages and explicitly open logs #6149

pull ajweiss wants to merge 1 commits into bitcoin:master from ajweiss:ajw_1505_early_log_buffer changing 3 files +85 −28
  1. ajweiss commented at 0:53 am on May 16, 2015: contributor

    Prevents stomping on debug logs in datadirs that are locked by other instances and lost parameter interaction messages that can get wiped by ShrinkDebugFile().

    The log is now opened explicitly and all emitted messages are buffered until this open occurs. The version message and log cut have also been moved to the earliest possible sensible location to prevent messages from appearing “before the cut.”

    Thanks @jgarzik for setting me on the right track.

  2. laanwj added the label Improvement on May 16, 2015
  3. laanwj commented at 7:19 am on May 16, 2015: member

    Concept ACK; I like making opening the debug log explicit.

    This will correctly handle any sequence of Shutdown() being called after AppInit2 errored out somewhere along the way? (just asking - previous changes in this sequence have tended to cause unexpected problems there)

  4. ajweiss force-pushed on May 18, 2015
  5. ajweiss commented at 11:20 pm on May 18, 2015: contributor

    It will now. I wasn’t able to get it to crash in my testing, but I believe that it was possible if the list/buffer were left static.

    I believe the issue you’re bringing up is #1832 where the mutex that protects the log was being destroyed before the log print functions were being called resulting in a crash on shutdown. The fix for that was to explicitly initialize the mutex and put it on the heap. This generally results in a leak of the mutex and the output fd even on clean shutdown, but that’s just merely ugly and benign. (and to be clear, the way things are today)

    I now do the same with this buffer which will ensure that it is safe, but also leaked at exit along with the mutex and fd. I looked into cleaning up the shutdown stuff and explicitly destroying the mutex, closing the fd and deleting this buffer, but that’s more involved (things are slightly different under QT and bitcoind) and it seems like different, more complex and more difficult to test PR that should probably stand alone.

  6. ajweiss force-pushed on May 19, 2015
  7. in src/util.cpp: in cd5ef8342b outdated
    181  */
    182 static FILE* fileout = NULL;
    183 static boost::mutex* mutexDebugLog = NULL;
    184+static list<string> *vMsgsBeforeOpenLog;
    185+
    186+int LogWriteStr(const std::string &str)
    


    laanwj commented at 10:18 am on June 1, 2015:
    This function can be static

    laanwj commented at 10:22 am on June 1, 2015:
    Also I’d suggest to rename it to FileWriteStr, and pass in the file argument explicitly. This makes for a clearer utility function.
  8. in src/util.cpp: in cd5ef8342b outdated
    237@@ -214,6 +238,24 @@ bool LogAcceptCategory(const char* category)
    238     return true;
    239 }
    240 
    241+std::string LogTimestampStr(const std::string &str)
    


    laanwj commented at 10:19 am on June 1, 2015:
    Function can be static

    laanwj commented at 10:28 am on June 1, 2015:
    I’d suggest passing in bool& fStartedNewLine explicitly, so that it can be defined at the caller site. An utility function with internal state is impossible to use from different contexts (not that it matters at this point, but to avoid surprises in the future).
  9. in src/util.cpp: in cd5ef8342b outdated
    209-    mutexDebugLog = new boost::mutex();
    210+    // dump buffered messages from before we opened the log
    211+    while (!vMsgsBeforeOpenLog->empty()) {
    212+        LogWriteStr(vMsgsBeforeOpenLog->front());
    213+        vMsgsBeforeOpenLog->pop_front();
    214+    }
    


    laanwj commented at 10:23 am on June 1, 2015:
    Could delete vMsgsBeforeOpenLog here and set it to NULL.
  10. in src/util.cpp: in cd5ef8342b outdated
    290-            ret += fprintf(fileout, "%s ", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
    291-        if (!str.empty() && str[str.size()-1] == '\n')
    292-            fStartedNewLine = true;
    293+        // buffer if we haven't opened the log yet
    294+        if (fileout == NULL) {
    295+            ret = strTimestamped.length();
    


    laanwj commented at 10:25 am on June 1, 2015:
    Just to be sure: add assertion that vMsgsBeforeOpenLog is not null
  11. in src/util.cpp: in cd5ef8342b outdated
    306+                boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
    307+                if (freopen(pathDebug.string().c_str(),"a",fileout) != NULL)
    308+                    setbuf(fileout, NULL); // unbuffered
    309+            }
    310+
    311+            ret += LogWriteStr(strTimestamped);
    


    laanwj commented at 10:30 am on June 1, 2015:
    Any reason to use ret += here, and ret = in the other fork of the if()?
  12. laanwj commented at 5:42 pm on July 2, 2015: member
    Needs nits addressed, should otherwise be ready for merging.
  13. ajweiss force-pushed on Jul 7, 2015
  14. Buffer log messages and explicitly open logs
    Prevents stomping on debug logs in datadirs that are locked by other
    instances and lost parameter interaction messages that can get wiped by
    ShrinkDebugFile().
    
    The log is now opened explicitly and all emitted messages are buffered
    until this open occurs.  The version message and log cut have also been
    moved to the earliest possible sensible location.
    27d7605804
  15. ajweiss force-pushed on Jul 7, 2015
  16. ajweiss commented at 3:28 am on July 7, 2015: contributor
    Nits addressed and rebased. Waiting for Travis… (which appears to be attempting to build the whole depends tree?)
  17. jgarzik commented at 3:30 am on July 7, 2015: contributor
    lightly tested ACK
  18. laanwj commented at 4:20 pm on July 22, 2015: member
    Tested ACK
  19. laanwj merged this on Jul 22, 2015
  20. laanwj closed this on Jul 22, 2015

  21. laanwj referenced this in commit 633fe10869 on Jul 22, 2015
  22. zkbot referenced this in commit df07f9ad23 on Feb 15, 2017
  23. zkbot referenced this in commit ec069ce96c on Mar 3, 2017
  24. zkbot referenced this in commit 702eefc71a on Mar 3, 2017
  25. zkbot referenced this in commit 99c4c6de0c on Mar 3, 2017
  26. Fuzzbawls referenced this in commit cb6d15a6ac on Apr 6, 2020
  27. MarcoFalke locked this on Sep 8, 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: 2024-12-22 18:12 UTC

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