16#include <unordered_map>
42 return fwrite(str.data(), 1, str.size(),
fp);
128#ifdef DEBUG_LOCKCONTENTION
129 {
"lock", BCLog::LOCK},
138static const std::unordered_map<BCLog::LogFlags, std::string>
141 [](
const std::map<std::string, BCLog::LogFlags> &
in) {
142 std::unordered_map<BCLog::LogFlags, std::string> out;
143 for (
const auto &[k, v] :
in) {
194 std::vector<LogCategory>
ret;
198 .active = WillLogCategory(flag)});
213 if (!m_log_timestamps) {
217 if (m_started_new_line) {
220 if (m_log_time_micros) {
247 for (
char ch_in : str) {
249 if ((
ch >= 32 ||
ch ==
'\n') &&
ch !=
'\x7f') {
269 m_started_new_line) {
289 if (m_log_sourcelocations && m_started_new_line) {
295 if (m_log_threadnames && m_started_new_line) {
301 m_started_new_line = !str.empty() && str[str.size() - 1] ==
'\n';
309 if (m_print_to_console) {
317 if (m_print_to_file) {
322 m_reopen_file =
false;
339 assert(!m_file_path.empty());
347 log_size = fs::file_size(m_file_path);
348 }
catch (
const fs::filesystem_error &) {
357 LogPrintf(
"Failed to shrink debug log file: fseek(...) failed\n");
361 int nBytes =
fread(vch.data(), 1, vch.size(), file);
369 }
else if (file !=
nullptr) {
375 m_categories |= category;
383 EnableCategory(flag);
396 DisableCategory(flag);
404 LogPrintf(
"Error trying to log using a category mask instead of an "
405 "explicit category.\n");
409 return (m_categories.load(std::memory_order_relaxed) & category) != 0;
bool WillLogCategory(LogFlags category) const
Return true if log accepts specified category.
std::string LogTimestampStr(const std::string &str)
void DisconnectTestLogger()
Only for testing.
bool DefaultShrinkDebugFile() const
Default for whether ShrinkDebugFile should be run.
void LogPrintStr(const std::string &str, const std::string &logging_function, const std::string &source_file, const int source_line, const BCLog::LogFlags category, const BCLog::Level level)
Send a string to the log output.
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
void DisableCategory(LogFlags category)
void EnableCategory(LogFlags category)
bool StartLogging()
Start logging (and flush all buffered messages)
std::string LogLevelToStr(BCLog::Level level)
std::string LogCategoryToStr(BCLog::LogFlags category)
static int FileWriteStr(const std::string &str, FILE *fp)
BCLog::Logger & LogInstance()
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str)
Return true if str parses as a log category and set the flag.
static const std::unordered_map< BCLog::LogFlags, std::string > LOG_CATEGORIES_BY_FLAG
const char *const DEFAULT_DEBUGLOGFILE
static const std::map< std::string, BCLog::LogFlags > LOG_CATEGORIES_BY_STR
static const bool DEFAULT_LOGIPS
std::string LogEscapeMessage(const std::string &str)
Belts and suspenders: make sure outgoing log messages don't contain potentially suspicious characters...
FILE * fopen(const fs::path &p, const char *mode)
const std::string & ThreadGetInternalName()
Get the thread's internal (in-memory) name; used e.g.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
static std::string ToString(const CService &ip)
std::string RemovePrefix(std::string_view str, std::string_view prefix)
int64_t GetTimeMicros()
Returns the system time (not mockable)
std::chrono::seconds GetMockTime()
For testing.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
constexpr int64_t count_seconds(std::chrono::seconds t)