Changeset View
Changeset View
Standalone View
Standalone View
src/logging.h
// 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-2018 The Bitcoin developers | // Copyright (c) 2017-2018 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. | ||||
#ifndef BITCOIN_LOGGING_H | #ifndef BITCOIN_LOGGING_H | ||||
#define BITCOIN_LOGGING_H | #define BITCOIN_LOGGING_H | ||||
#include <fs.h> | #include <fs.h> | ||||
#include <tinyformat.h> | #include <tinyformat.h> | ||||
#include <utilstrencodings.h> | |||||
#include <atomic> | #include <atomic> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <list> | #include <list> | ||||
#include <mutex> | #include <mutex> | ||||
#include <string> | #include <string> | ||||
static const bool DEFAULT_LOGTIMEMICROS = false; | static const bool DEFAULT_LOGTIMEMICROS = false; | ||||
static const bool DEFAULT_LOGIPS = false; | static const bool DEFAULT_LOGIPS = false; | ||||
static const bool DEFAULT_LOGTIMESTAMPS = true; | static const bool DEFAULT_LOGTIMESTAMPS = true; | ||||
extern bool fLogIPs; | extern bool fLogIPs; | ||||
extern const char *const DEFAULT_DEBUGLOGFILE; | extern const char *const DEFAULT_DEBUGLOGFILE; | ||||
struct CLogCategoryActive { | |||||
std::string category; | |||||
bool active; | |||||
}; | |||||
namespace BCLog { | namespace BCLog { | ||||
enum LogFlags : uint32_t { | enum LogFlags : uint32_t { | ||||
NONE = 0, | NONE = 0, | ||||
NET = (1 << 0), | NET = (1 << 0), | ||||
TOR = (1 << 1), | TOR = (1 << 1), | ||||
MEMPOOL = (1 << 2), | MEMPOOL = (1 << 2), | ||||
HTTP = (1 << 3), | HTTP = (1 << 3), | ||||
Show All 25 Lines | private: | ||||
/** | /** | ||||
* m_started_new_line is a state variable that will suppress printing of the | * m_started_new_line is a state variable that will suppress printing of the | ||||
* timestamp when multiple calls are made that don't end in a newline. | * timestamp when multiple calls are made that don't end in a newline. | ||||
*/ | */ | ||||
std::atomic_bool m_started_new_line{true}; | std::atomic_bool m_started_new_line{true}; | ||||
/** | /** | ||||
* Log categories bitfield. Leveldb/libevent need special handling if their | * Log categories bitfield. libevent needs special handling if their | ||||
* flags are changed at runtime. | * flags are changed at runtime. | ||||
*/ | */ | ||||
std::atomic<uint32_t> m_categories{0}; | std::atomic<uint32_t> m_categories{0}; | ||||
std::string LogTimestampStr(const std::string &str); | std::string LogTimestampStr(const std::string &str); | ||||
public: | public: | ||||
bool m_print_to_console = false; | bool m_print_to_console = false; | ||||
bool m_print_to_file = true; | bool m_print_to_file = true; | ||||
bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS; | bool m_log_timestamps = DEFAULT_LOGTIMESTAMPS; | ||||
bool m_log_time_micros = DEFAULT_LOGTIMEMICROS; | bool m_log_time_micros = DEFAULT_LOGTIMEMICROS; | ||||
std::atomic<bool> m_reopen_file{false}; | std::atomic<bool> m_reopen_file{false}; | ||||
~Logger(); | ~Logger(); | ||||
/** Send a string to the log output */ | /** Send a string to the log output */ | ||||
int LogPrintStr(const std::string &str); | int LogPrintStr(const std::string &str); | ||||
fs::path GetDebugLogPath(); | fs::path GetDebugLogPath(); | ||||
bool OpenDebugLog(); | bool OpenDebugLog(); | ||||
void ShrinkDebugFile(); | void ShrinkDebugFile(); | ||||
uint32_t GetCategoryMask() const { return m_categories.load(); } | |||||
void EnableCategory(LogFlags category); | void EnableCategory(LogFlags category); | ||||
bool EnableCategory(const std::string &str); | bool EnableCategory(const std::string &str); | ||||
void DisableCategory(LogFlags category); | void DisableCategory(LogFlags category); | ||||
bool DisableCategory(const std::string &str); | bool DisableCategory(const std::string &str); | ||||
/** Return true if log accepts specified category */ | /** Return true if log accepts specified category */ | ||||
bool WillLogCategory(LogFlags category) const; | bool WillLogCategory(LogFlags category) const; | ||||
/** Default for whether ShrinkDebugFile should be run */ | /** Default for whether ShrinkDebugFile should be run */ | ||||
bool DefaultShrinkDebugFile() const; | bool DefaultShrinkDebugFile() const; | ||||
}; | }; | ||||
} // namespace BCLog | } // namespace BCLog | ||||
BCLog::Logger &GetLogger(); | BCLog::Logger &GetLogger(); | ||||
/** Return true if log accepts specified category */ | /** Return true if log accepts specified category */ | ||||
static inline bool LogAcceptCategory(BCLog::LogFlags category) { | static inline bool LogAcceptCategory(BCLog::LogFlags category) { | ||||
return GetLogger().WillLogCategory(category); | return GetLogger().WillLogCategory(category); | ||||
} | } | ||||
/** Returns a string with the supported log categories */ | /** Returns a string with the log categories. */ | ||||
std::string ListLogCategories(); | std::string ListLogCategories(); | ||||
/** Returns a vector of the active log categories. */ | |||||
std::vector<CLogCategoryActive> ListActiveLogCategories(); | |||||
/** Return true if str parses as a log category and set the flag */ | /** Return true if str parses as a log category and set the flag */ | ||||
bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str); | bool GetLogCategory(BCLog::LogFlags &flag, const std::string &str); | ||||
// Be conservative when using LogPrintf/error or other things which | // Be conservative when using LogPrintf/error or other things which | ||||
// unconditionally log to debug.log! It should not be the case that an inbound | // unconditionally log to debug.log! It should not be the case that an inbound | ||||
// peer can fill up a users disk with debug.log entries. | // peer can fill up a users disk with debug.log entries. | ||||
#define LogPrint(category, ...) \ | #define LogPrint(category, ...) \ | ||||
Show All 12 Lines |