Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't close old debug log file handle prematurely when trying to re-open (on SIGHUP) #307

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,5 @@ FILE *fopen(const fs::path& p, const char *mode)
return ::fopen(p.string().c_str(), mode);
}

FILE *freopen(const fs::path& p, const char *mode, FILE *stream)
{
return ::freopen(p.string().c_str(), mode, stream);
}

} // fsbridge
1 change: 0 additions & 1 deletion src/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace fs = boost::filesystem;
/** Bridge operations to C stdio */
namespace fsbridge {
FILE *fopen(const fs::path& p, const char *mode);
FILE *freopen(const fs::path& p, const char *mode, FILE *stream);
};

#endif // BITCOIN_FS_H
10 changes: 5 additions & 5 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ void BCLog::Logger::LogPrintStr(const std::string &str)
// reopen the log file, if requested
if (m_reopen_file) {
m_reopen_file = false;
m_fileout = fsbridge::freopen(m_file_path, "a", m_fileout);
if (!m_fileout) {
return;
FILE* new_fileout = fsbridge::fopen(m_file_path, "a");
if (new_fileout) {
setbuf(new_fileout, nullptr); // unbuffered
fclose(m_fileout);
m_fileout = new_fileout;
}
setbuf(m_fileout, nullptr); // unbuffered
}

FileWriteStr(strTimestamped, m_fileout);
}
}
Expand Down
6 changes: 5 additions & 1 deletion test/functional/feature_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test debug logging."""

import os
import os, signal

from test_framework.test_framework import BitcoinTestFramework
from test_framework.test_node import ErrorMatch
Expand All @@ -23,6 +23,10 @@ def run_test(self):
default_log_path = self.relative_log_path("debug.log")
assert os.path.isfile(default_log_path)

# send SIGHUP to verify that it causes the logger to reopen the log file
self.nodes[0].process.send_signal(signal.SIGHUP)
self.nodes[0].getblockchaininfo()

# test alternative log file name in datadir
self.restart_node(0, ["-debuglogfile=foo.log"])
assert os.path.isfile(self.relative_log_path("foo.log"))
Expand Down