Skip to content

Commit 8c2d695

Browse files
author
Jim Posen
committed
util: Store debug log file path in BCLog::Logger member.
This breaks the cyclic between logging and util.
1 parent 8e7b961 commit 8c2d695

File tree

5 files changed

+16
-21
lines changed

5 files changed

+16
-21
lines changed

src/bench/bench_bitcoin.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ main(int argc, char** argv)
4646
RandomInit();
4747
ECC_Start();
4848
SetupEnvironment();
49-
g_logger->m_print_to_file = false; // don't want to write to debug.log file
5049

5150
int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
5251
std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);

src/init.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,13 +826,15 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&
826826
*/
827827
void InitLogging()
828828
{
829+
g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
830+
g_logger->m_file_path = AbsPathForConfigVal(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
831+
829832
// Add newlines to the logfile to distinguish this execution from the last
830833
// one; called before console logging is set up, so this is only sent to
831834
// debug.log.
832835
LogPrintf("\n\n\n\n\n");
833836

834837
g_logger->m_print_to_console = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false));
835-
g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
836838
g_logger->m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
837839
g_logger->m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
838840

@@ -1233,7 +1235,7 @@ bool AppInitMain()
12331235
}
12341236
if (!g_logger->OpenDebugLog()) {
12351237
return InitError(strprintf("Could not open debug log file %s",
1236-
g_logger->GetDebugLogPath().string()));
1238+
g_logger->m_file_path.string()));
12371239
}
12381240
}
12391241

src/logging.cpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <logging.h>
7-
#include <util.h>
7+
#include <utiltime.h>
88

99
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
1010

@@ -30,20 +30,14 @@ static int FileWriteStr(const std::string &str, FILE *fp)
3030
return fwrite(str.data(), 1, str.size(), fp);
3131
}
3232

33-
fs::path BCLog::Logger::GetDebugLogPath() const
34-
{
35-
fs::path logfile(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
36-
return AbsPathForConfigVal(logfile);
37-
}
38-
3933
bool BCLog::Logger::OpenDebugLog()
4034
{
4135
std::lock_guard<std::mutex> scoped_lock(m_file_mutex);
4236

4337
assert(m_fileout == nullptr);
44-
fs::path pathDebug = GetDebugLogPath();
38+
assert(!m_file_path.empty());
4539

46-
m_fileout = fsbridge::fopen(pathDebug, "a");
40+
m_fileout = fsbridge::fopen(m_file_path, "a");
4741
if (!m_fileout) {
4842
return false;
4943
}
@@ -228,8 +222,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
228222
// reopen the log file, if requested
229223
if (m_reopen_file) {
230224
m_reopen_file = false;
231-
fs::path pathDebug = GetDebugLogPath();
232-
if (fsbridge::freopen(pathDebug,"a",m_fileout) != nullptr)
225+
if (fsbridge::freopen(m_file_path,"a",m_fileout) != nullptr)
233226
setbuf(m_fileout, nullptr); // unbuffered
234227
}
235228

@@ -243,14 +236,16 @@ void BCLog::Logger::ShrinkDebugFile()
243236
{
244237
// Amount of debug.log to save at end when shrinking (must fit in memory)
245238
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
239+
240+
assert(!m_file_path.empty());
241+
246242
// Scroll debug.log if it's getting too big
247-
fs::path pathLog = GetDebugLogPath();
248-
FILE* file = fsbridge::fopen(pathLog, "r");
243+
FILE* file = fsbridge::fopen(m_file_path, "r");
249244

250245
// Special files (e.g. device nodes) may not have a size.
251246
size_t log_size = 0;
252247
try {
253-
log_size = fs::file_size(pathLog);
248+
log_size = fs::file_size(m_file_path);
254249
} catch (boost::filesystem::filesystem_error &) {}
255250

256251
// If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
@@ -263,7 +258,7 @@ void BCLog::Logger::ShrinkDebugFile()
263258
int nBytes = fread(vch.data(), 1, vch.size(), file);
264259
fclose(file);
265260

266-
file = fsbridge::fopen(pathLog, "w");
261+
file = fsbridge::fopen(m_file_path, "w");
267262
if (file)
268263
{
269264
fwrite(vch.data(), 1, nBytes, file);

src/logging.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ namespace BCLog {
7777

7878
public:
7979
bool m_print_to_console = false;
80-
bool m_print_to_file = true;
80+
bool m_print_to_file = false;
8181

8282
bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS;
8383
bool m_log_time_micros = DEFAULT_LOGTIMEMICROS;
8484

85+
fs::path m_file_path;
8586
std::atomic<bool> m_reopen_file{false};
8687

8788
/** Send a string to the log output */
@@ -90,7 +91,6 @@ namespace BCLog {
9091
/** Returns whether logs will be written to any output */
9192
bool Enabled() const { return m_print_to_console || m_print_to_file; }
9293

93-
fs::path GetDebugLogPath() const;
9494
bool OpenDebugLog();
9595
void ShrinkDebugFile();
9696

src/test/test_bitcoin.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
4747
SetupNetworking();
4848
InitSignatureCache();
4949
InitScriptExecutionCache();
50-
g_logger->m_print_to_file = false; // don't want to write to debug.log file
5150
fCheckBlockIndex = true;
5251
SelectParams(chainName);
5352
noui_connect();

0 commit comments

Comments
 (0)