aboutsummaryrefslogtreecommitdiff
path: root/src/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp59
1 files changed, 32 insertions, 27 deletions
diff --git a/src/util.cpp b/src/util.cpp
index 08ee6b8b87..486df772fb 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -97,15 +97,15 @@ namespace boost {
} // namespace boost
-using namespace std;
+
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
const char * const BITCOIN_PID_FILENAME = "bitcoind.pid";
CCriticalSection cs_args;
-map<string, string> mapArgs;
-static map<string, vector<string> > _mapMultiArgs;
-const map<string, vector<string> >& mapMultiArgs = _mapMultiArgs;
+std::map<std::string, std::string> mapArgs;
+static std::map<std::string, std::vector<std::string> > _mapMultiArgs;
+const std::map<std::string, std::vector<std::string> >& mapMultiArgs = _mapMultiArgs;
bool fDebug = false;
bool fPrintToConsole = false;
bool fPrintToDebugLog = true;
@@ -191,7 +191,7 @@ static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
*/
static FILE* fileout = NULL;
static boost::mutex* mutexDebugLog = NULL;
-static list<string> *vMsgsBeforeOpenLog;
+static std::list<std::string>* vMsgsBeforeOpenLog;
static int FileWriteStr(const std::string &str, FILE *fp)
{
@@ -202,7 +202,7 @@ static void DebugPrintInit()
{
assert(mutexDebugLog == NULL);
mutexDebugLog = new boost::mutex();
- vMsgsBeforeOpenLog = new list<string>;
+ vMsgsBeforeOpenLog = new std::list<std::string>;
}
void OpenDebugLog()
@@ -214,12 +214,13 @@ void OpenDebugLog()
assert(vMsgsBeforeOpenLog);
boost::filesystem::path pathDebug = GetDataDir() / "debug.log";
fileout = fopen(pathDebug.string().c_str(), "a");
- if (fileout) setbuf(fileout, NULL); // unbuffered
-
- // dump buffered messages from before we opened the log
- while (!vMsgsBeforeOpenLog->empty()) {
- FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
- vMsgsBeforeOpenLog->pop_front();
+ if (fileout) {
+ setbuf(fileout, NULL); // unbuffered
+ // dump buffered messages from before we opened the log
+ while (!vMsgsBeforeOpenLog->empty()) {
+ FileWriteStr(vMsgsBeforeOpenLog->front(), fileout);
+ vMsgsBeforeOpenLog->pop_front();
+ }
}
delete vMsgsBeforeOpenLog;
@@ -237,22 +238,22 @@ bool LogAcceptCategory(const char* category)
// This helps prevent issues debugging global destructors,
// where mapMultiArgs might be deleted before another
// global destructor calls LogPrint()
- static boost::thread_specific_ptr<set<string> > ptrCategory;
+ static boost::thread_specific_ptr<std::set<std::string> > ptrCategory;
if (ptrCategory.get() == NULL)
{
if (mapMultiArgs.count("-debug")) {
- const vector<string>& categories = mapMultiArgs.at("-debug");
- ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
+ const std::vector<std::string>& categories = mapMultiArgs.at("-debug");
+ ptrCategory.reset(new std::set<std::string>(categories.begin(), categories.end()));
// thread_specific_ptr automatically deletes the set when the thread ends.
} else
- ptrCategory.reset(new set<string>());
+ ptrCategory.reset(new std::set<std::string>());
}
- const set<string>& setCategories = *ptrCategory.get();
+ const std::set<std::string>& setCategories = *ptrCategory;
// if not debugging everything and not debugging specific category, LogPrint does nothing.
- if (setCategories.count(string("")) == 0 &&
- setCategories.count(string("1")) == 0 &&
- setCategories.count(string(category)) == 0)
+ if (setCategories.count(std::string("")) == 0 &&
+ setCategories.count(std::string("1")) == 0 &&
+ setCategories.count(std::string(category)) == 0)
return false;
}
return true;
@@ -265,7 +266,7 @@ bool LogAcceptCategory(const char* category)
*/
static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fStartedNewLine)
{
- string strStamped;
+ std::string strStamped;
if (!fLogTimestamps)
return str;
@@ -292,7 +293,7 @@ int LogPrintStr(const std::string &str)
int ret = 0; // Returns total number of characters written
static std::atomic_bool fStartedNewLine(true);
- string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
+ std::string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
if (fPrintToConsole)
{
@@ -560,14 +561,14 @@ void ReadConfigFile(const std::string& confPath)
{
LOCK(cs_args);
- set<string> setOptions;
+ std::set<std::string> setOptions;
setOptions.insert("*");
for (boost::program_options::detail::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it)
{
// Don't overwrite existing settings so command line settings override bitcoin.conf
- string strKey = string("-") + it->string_key;
- string strValue = it->value[0];
+ std::string strKey = std::string("-") + it->string_key;
+ std::string strValue = it->value[0];
InterpretNegativeSetting(strKey, strValue);
if (mapArgs.count(strKey) == 0)
mapArgs[strKey] = strValue;
@@ -723,13 +724,17 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
void ShrinkDebugFile()
{
+ // Amount of debug.log to save at end when shrinking (must fit in memory)
+ constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;
// Scroll debug.log if it's getting too big
boost::filesystem::path pathLog = GetDataDir() / "debug.log";
FILE* file = fopen(pathLog.string().c_str(), "r");
- if (file && boost::filesystem::file_size(pathLog) > 10 * 1000000)
+ // If debug.log file is more than 10% bigger the RECENT_DEBUG_HISTORY_SIZE
+ // trim it down by saving only the last RECENT_DEBUG_HISTORY_SIZE bytes
+ if (file && boost::filesystem::file_size(pathLog) > 11 * (RECENT_DEBUG_HISTORY_SIZE / 10))
{
// Restart the file with some of the end
- std::vector <char> vch(200000,0);
+ std::vector<char> vch(RECENT_DEBUG_HISTORY_SIZE, 0);
fseek(file, -((long)vch.size()), SEEK_END);
int nBytes = fread(vch.data(), 1, vch.size(), file);
fclose(file);