From 968eb3fc5d2a29420f21443a2acb6e65b1e4adea Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Thu, 3 Jan 2019 10:21:19 +0100 Subject: [PATCH] Add real timestamp to log output when mock time is enabled (#2604) * Implement IsMockTime to test for mock time * Add real timestamp to log output when mock time is enabled This makes debugging on Travis easier as it gives a hint about timing behavior. --- src/util.cpp | 6 +++++- src/utiltime.cpp | 5 +++++ src/utiltime.h | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/util.cpp b/src/util.cpp index a02417889..9a6f3b417 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -316,8 +316,12 @@ static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fSt return str; if (*fStartedNewLine) { + if (IsMockTime()) { + int64_t nRealTimeMicros = GetTimeMicros(); + strStamped = DateTimeStrFormat("(real %Y-%m-%d %H:%M:%S) ", nRealTimeMicros/1000000); + } int64_t nTimeMicros = GetLogTimeMicros(); - strStamped = DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000); + strStamped += DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nTimeMicros/1000000); if (fLogTimeMicros) strStamped += strprintf(".%06d", nTimeMicros%1000000); strStamped += ' ' + str; diff --git a/src/utiltime.cpp b/src/utiltime.cpp index aea047b65..41c4f641e 100644 --- a/src/utiltime.cpp +++ b/src/utiltime.cpp @@ -29,6 +29,11 @@ void SetMockTime(int64_t nMockTimeIn) nMockTime = nMockTimeIn; } +bool IsMockTime() +{ + return nMockTime != 0; +} + int64_t GetTimeMillis() { int64_t now = (boost::posix_time::microsec_clock::universal_time() - diff --git a/src/utiltime.h b/src/utiltime.h index cb2c0afb1..91df8e707 100644 --- a/src/utiltime.h +++ b/src/utiltime.h @@ -25,6 +25,7 @@ int64_t GetTimeMicros(); int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable int64_t GetLogTimeMicros(); void SetMockTime(int64_t nMockTimeIn); +bool IsMockTime(); void MilliSleep(int64_t n); std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);