Merge #9230: Fix some benign races in timestamp logging

8b22efb Make fStartedNewLine an std::atomic_bool (Matt Corallo)
507145d Fix race when accessing std::locale::classic() (Matt Corallo)
This commit is contained in:
Wladimir J. van der Laan 2016-12-01 11:47:39 +01:00 committed by Alexander Block
parent d54ef6d7e3
commit 3b2fd85b59
2 changed files with 5 additions and 4 deletions

View File

@ -296,7 +296,7 @@ bool LogAcceptCategory(const char* category)
* suppress printing of the timestamp when multiple calls are made that don't
* end in a newline. Initialize it to true, and hold/manage it, in the calling context.
*/
static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine)
static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fStartedNewLine)
{
string strStamped;
@ -320,7 +320,7 @@ static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine
* suppress printing of the thread name when multiple calls are made that don't
* end in a newline. Initialize it to true, and hold/manage it, in the calling context.
*/
static std::string LogThreadNameStr(const std::string &str, bool *fStartedNewLine)
static std::string LogThreadNameStr(const std::string &str, std::atomic_bool *fStartedNewLine)
{
string strThreadLogged;
@ -340,7 +340,7 @@ static std::string LogThreadNameStr(const std::string &str, bool *fStartedNewLin
int LogPrintStr(const std::string &str)
{
int ret = 0; // Returns total number of characters written
static bool fStartedNewLine = true;
static std::atomic_bool fStartedNewLine(true);
std::string strThreadLogged = LogThreadNameStr(str, &fStartedNewLine);
std::string strTimestamped = LogTimestampStr(strThreadLogged, &fStartedNewLine);

View File

@ -80,8 +80,9 @@ void MilliSleep(int64_t n)
std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
{
static std::locale classic(std::locale::classic());
// std::locale takes ownership of the pointer
std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
std::locale loc(classic, new boost::posix_time::time_facet(pszFormat));
std::stringstream ss;
ss.imbue(loc);
ss << boost::posix_time::from_time_t(nTime);