From 73ba9a8f1495bf50369e73d8b6393e9f23cf52ca Mon Sep 17 00:00:00 2001 From: SergeyRyabinin Date: Fri, 15 Mar 2024 19:00:56 +0000 Subject: [PATCH] Write logging timestamp without copying --- .../utils/logging/FormattedLogSystem.cpp | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/src/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp b/src/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp index ee22b167a5d..16a1f40a45c 100644 --- a/src/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp +++ b/src/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp @@ -7,7 +7,6 @@ #include #include -#include #include #include @@ -40,6 +39,36 @@ static const char* GetLogPrefix(const LogLevel logLevel) return "[UNKNOWN] "; } +static void AppendTimeStamp(Aws::String& statement) +{ + static const size_t TS_LEN = sizeof("2000-01-01 00:00:00.000") - 1; + const size_t oldStatementSz = statement.size(); + const size_t newStatementSz = oldStatementSz + TS_LEN; + statement.resize(newStatementSz); + + auto now = std::chrono::system_clock::now(); + std::time_t time = std::chrono::system_clock::to_time_t(now); + struct tm gmtTimeStamp; + Aws::Time::GMTime(&gmtTimeStamp, time); + + auto len = std::strftime(&statement[oldStatementSz], TS_LEN, "%Y-%m-%d %H:%M:%S", &gmtTimeStamp); + if (len) + { + int64_t ms = std::chrono::duration_cast(now.time_since_epoch()).count(); + ms = ms - ms / 1000 * 1000; // calculate the milliseconds as fraction. + statement[oldStatementSz + len++] = '.'; + int divisor = 100; + while(divisor) + { + auto digit = ms / divisor; + statement[oldStatementSz + len++] = char('0' + digit); + ms = ms - divisor * digit; + divisor /= 10; + } + statement[oldStatementSz + len] = '\0'; + } +} + static Aws::String CreateLogPrefixLine(LogLevel logLevel, const char* tag, const size_t statementSize = 80) { Aws::String prefix; @@ -49,7 +78,7 @@ static Aws::String CreateLogPrefixLine(LogLevel logLevel, const char* tag, const prefix.reserve(statementLen); prefix = GetLogPrefix(logLevel); - prefix += DateTime::Now().CalculateGmtTimeWithMsPrecision(); + AppendTimeStamp(prefix); prefix += ' '; prefix += tag;