From 03c4c0b165979f7986b4246d1fa6a8229715867a Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Tue, 24 Oct 2017 21:45:44 +0000 Subject: [PATCH] Use and propagate microsecond-precision timestamps to FormatTime This enables sub-second precision timestamp formatting for logs and clients without server-time. --- include/znc/User.h | 1 + modules/awaystore.cpp | 4 ++-- modules/listsockets.cpp | 6 ++++-- modules/log.cpp | 4 ++-- src/Buffer.cpp | 2 +- src/User.cpp | 14 +++++++++++--- 6 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/znc/User.h b/include/znc/User.h index d9bcca6e..63dea4bc 100644 --- a/include/znc/User.h +++ b/include/znc/User.h @@ -110,6 +110,7 @@ class CUser { CString AddTimestamp(const CString& sStr) const; CString AddTimestamp(time_t tm, const CString& sStr) const; + CString AddTimestamp(timeval tv, const CString& sStr) const; void CloneNetworks(const CUser& User); bool Clone(const CUser& User, CString& sErrorRet, diff --git a/modules/awaystore.cpp b/modules/awaystore.cpp index dc128551..b2a76b02 100644 --- a/modules/awaystore.cpp +++ b/modules/awaystore.cpp @@ -58,8 +58,8 @@ class CAwayJob : public CTimer { class CAway : public CModule { void AwayCommand(const CString& sCommand) { CString sReason; - time_t curtime; - time(&curtime); + timeval curtime; + gettimeofday(&curtime, nullptr); if (sCommand.Token(1) != "-quiet") { sReason = CUtils::FormatTime(curtime, sCommand.Token(1, true), diff --git a/modules/listsockets.cpp b/modules/listsockets.cpp index 55c67571..c35013c4 100644 --- a/modules/listsockets.cpp +++ b/modules/listsockets.cpp @@ -154,8 +154,10 @@ class CListSockets : public CModule { CString GetCreatedTime(Csock* pSocket) { unsigned long long iStartTime = pSocket->GetStartTime(); - time_t iTime = iStartTime / 1000; - return CUtils::FormatTime(iTime, "%Y-%m-%d %H:%M:%S", + timeval tv; + tv.tv_sec = iStartTime / 1000; + tv.tv_usec = iStartTime % 1000 * 1000; + return CUtils::FormatTime(tv, "%Y-%m-%d %H:%M:%S.%L", GetUser()->GetTimezone()); } diff --git a/modules/log.cpp b/modules/log.cpp index 05685593..f1ac9fd8 100644 --- a/modules/log.cpp +++ b/modules/log.cpp @@ -271,9 +271,9 @@ void CLogMod::PutLog(const CString& sLine, } CString sPath; - time_t curtime; + timeval curtime; - time(&curtime); + gettimeofday(&curtime, nullptr); // Generate file name sPath = CUtils::FormatTime(curtime, m_sLogPath, GetUser()->GetTimezone()); if (sPath.empty()) { diff --git a/src/Buffer.cpp b/src/Buffer.cpp index a95d5e0d..14ef8ffa 100644 --- a/src/Buffer.cpp +++ b/src/Buffer.cpp @@ -52,7 +52,7 @@ CMessage CBufLine::ToMessage(const CClient& Client, mssThisParams["text"] = m_sText; } else { mssThisParams["text"] = - Client.GetUser()->AddTimestamp(Line.GetTime().tv_sec, m_sText); + Client.GetUser()->AddTimestamp(Line.GetTime(), m_sText); } // make a copy of params, because the following loop modifies the original diff --git a/src/User.cpp b/src/User.cpp index c853968b..718ba84a 100644 --- a/src/User.cpp +++ b/src/User.cpp @@ -603,17 +603,25 @@ CString& CUser::ExpandString(const CString& sStr, CString& sRet) const { } CString CUser::AddTimestamp(const CString& sStr) const { - time_t tm; - return AddTimestamp(time(&tm), sStr); + timeval tv; + gettimeofday(&tv, nullptr); + return AddTimestamp(tv, sStr); } CString CUser::AddTimestamp(time_t tm, const CString& sStr) const { + timeval tv; + tv.tv_sec = tm; + tv.tv_usec = 0; + return AddTimestamp(tv, sStr); +} + +CString CUser::AddTimestamp(timeval tv, const CString& sStr) const { CString sRet = sStr; if (!GetTimestampFormat().empty() && (m_bAppendTimestamp || m_bPrependTimestamp)) { CString sTimestamp = - CUtils::FormatTime(tm, GetTimestampFormat(), m_sTimezone); + CUtils::FormatTime(tv, GetTimestampFormat(), m_sTimezone); if (sTimestamp.empty()) { return sRet; }