Merge #10383: [logging] log system time and mock time

761392d [logging] log system time and mock time (John Newbery)

Tree-SHA512: 0a4b3ad74bcac201be490fe12e4b45adeabc39030ac46f40e1aeb2a20b2f3963e4468e65798d8aaeca1818759cab55ff2b2aa214500aa11571492c3301dd31c1
This commit is contained in:
Wladimir J. van der Laan 2017-05-16 08:06:26 +02:00
commit d0c37ee789
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
3 changed files with 11 additions and 11 deletions

View File

@ -306,10 +306,14 @@ static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fSt
return str; return str;
if (*fStartedNewLine) { if (*fStartedNewLine) {
int64_t nTimeMicros = GetLogTimeMicros(); int64_t nTimeMicros = GetTimeMicros();
strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000); strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000);
if (fLogTimeMicros) if (fLogTimeMicros)
strStamped += strprintf(".%06d", nTimeMicros%1000000); strStamped += strprintf(".%06d", nTimeMicros%1000000);
int64_t mocktime = GetMockTime();
if (mocktime) {
strStamped += " (mocktime: " + DateTimeStrFormat("%Y-%m-%d %H:%M:%S", mocktime) + ")";
}
strStamped += ' ' + str; strStamped += ' ' + str;
} else } else
strStamped = str; strStamped = str;

View File

@ -31,6 +31,11 @@ void SetMockTime(int64_t nMockTimeIn)
nMockTime.store(nMockTimeIn, std::memory_order_relaxed); nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
} }
int64_t GetMockTime()
{
return nMockTime.load(std::memory_order_relaxed);
}
int64_t GetTimeMillis() int64_t GetTimeMillis()
{ {
int64_t now = (boost::posix_time::microsec_clock::universal_time() - int64_t now = (boost::posix_time::microsec_clock::universal_time() -
@ -52,15 +57,6 @@ int64_t GetSystemTimeInSeconds()
return GetTimeMicros()/1000000; return GetTimeMicros()/1000000;
} }
/** Return a time useful for the debug log */
int64_t GetLogTimeMicros()
{
int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
if (mocktime) return mocktime*1000000;
return GetTimeMicros();
}
void MilliSleep(int64_t n) void MilliSleep(int64_t n)
{ {

View File

@ -23,8 +23,8 @@ int64_t GetTime();
int64_t GetTimeMillis(); int64_t GetTimeMillis();
int64_t GetTimeMicros(); int64_t GetTimeMicros();
int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable
int64_t GetLogTimeMicros();
void SetMockTime(int64_t nMockTimeIn); void SetMockTime(int64_t nMockTimeIn);
int64_t GetMockTime();
void MilliSleep(int64_t n); void MilliSleep(int64_t n);
std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime); std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);