Changeset View
Changeset View
Standalone View
Standalone View
src/logging.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Copyright (c) 2017-2019 The Bitcoin developers | // Copyright (c) 2017-2019 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <logging.h> | #include <logging.h> | ||||
#include <util/threadnames.h> | |||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <mutex> | |||||
bool fLogIPs = DEFAULT_LOGIPS; | bool fLogIPs = DEFAULT_LOGIPS; | ||||
const char *const DEFAULT_DEBUGLOGFILE = "debug.log"; | const char *const DEFAULT_DEBUGLOGFILE = "debug.log"; | ||||
BCLog::Logger &LogInstance() { | BCLog::Logger &LogInstance() { | ||||
/** | /** | ||||
* NOTE: the logger instance is leaked on exit. This is ugly, but will be | * NOTE: the logger instance is leaked on exit. This is ugly, but will be | ||||
* cleaned up by the OS/libc. Defining a logger as a global object doesn't | * cleaned up by the OS/libc. Defining a logger as a global object doesn't | ||||
* work since the order of destruction of static/global objects is | * work since the order of destruction of static/global objects is | ||||
▲ Show 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | if (m_started_new_line) { | ||||
strStamped += | strStamped += | ||||
" (mocktime: " + FormatISO8601DateTime(mocktime) + ")"; | " (mocktime: " + FormatISO8601DateTime(mocktime) + ")"; | ||||
} | } | ||||
strStamped += ' ' + str; | strStamped += ' ' + str; | ||||
} else { | } else { | ||||
strStamped = str; | strStamped = str; | ||||
} | } | ||||
if (!str.empty() && str[str.size() - 1] == '\n') { | |||||
m_started_new_line = true; | |||||
} else { | |||||
m_started_new_line = false; | |||||
} | |||||
return strStamped; | return strStamped; | ||||
} | } | ||||
void BCLog::Logger::LogPrintStr(const std::string &str) { | void BCLog::Logger::LogPrintStr(const std::string &str) { | ||||
std::string strTimestamped = LogTimestampStr(str); | std::string str_prefixed = str; | ||||
if (m_log_threadnames && m_started_new_line) { | |||||
str_prefixed.insert(0, "[" + util::ThreadGetInternalName() + "] "); | |||||
} | |||||
str_prefixed = LogTimestampStr(str_prefixed); | |||||
m_started_new_line = !str.empty() && str[str.size() - 1] == '\n'; | |||||
if (m_print_to_console) { | if (m_print_to_console) { | ||||
// Print to console. | // Print to console. | ||||
fwrite(strTimestamped.data(), 1, strTimestamped.size(), stdout); | fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout); | ||||
fflush(stdout); | fflush(stdout); | ||||
} | } | ||||
if (m_print_to_file) { | if (m_print_to_file) { | ||||
std::lock_guard<std::mutex> scoped_lock(m_file_mutex); | std::lock_guard<std::mutex> scoped_lock(m_file_mutex); | ||||
// Buffer if we haven't opened the log yet. | // Buffer if we haven't opened the log yet. | ||||
if (m_fileout == nullptr) { | if (m_fileout == nullptr) { | ||||
m_msgs_before_open.push_back(strTimestamped); | m_msgs_before_open.push_back(str_prefixed); | ||||
} else { | } else { | ||||
// Reopen the log file, if requested. | // Reopen the log file, if requested. | ||||
if (m_reopen_file) { | if (m_reopen_file) { | ||||
m_reopen_file = false; | m_reopen_file = false; | ||||
FILE *new_fileout = fsbridge::fopen(m_file_path, "a"); | FILE *new_fileout = fsbridge::fopen(m_file_path, "a"); | ||||
if (new_fileout) { | if (new_fileout) { | ||||
// unbuffered. | // unbuffered. | ||||
setbuf(m_fileout, nullptr); | setbuf(m_fileout, nullptr); | ||||
fclose(m_fileout); | fclose(m_fileout); | ||||
m_fileout = new_fileout; | m_fileout = new_fileout; | ||||
} | } | ||||
} | } | ||||
FileWriteStr(strTimestamped, m_fileout); | FileWriteStr(str_prefixed, m_fileout); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
void BCLog::Logger::ShrinkDebugFile() { | void BCLog::Logger::ShrinkDebugFile() { | ||||
// Amount of debug.log to save at end when shrinking (must fit in memory) | // Amount of debug.log to save at end when shrinking (must fit in memory) | ||||
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; | constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000; | ||||
▲ Show 20 Lines • Show All 76 Lines • Show Last 20 Lines |