From e2d8474857e8b39efcdf21441c95ad51a8aef6e8 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Tue, 14 Feb 2023 16:25:45 +0100 Subject: Let +TIMESTAMP and +RELATIVETIMER use the same osl_getSystemTime ...so that the reported numbers add up if you specify both in SAL_LOG. Also make the code look more symmetric. Change-Id: I8b24dbe7cfa4d7aaebd2069db87a4e9d5fe6e3f0 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147017 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- sal/osl/all/log.cxx | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'sal') diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx index 235c7267d22e..e9895e14cb7b 100644 --- a/sal/osl/all/log.cxx +++ b/sal/osl/all/log.cxx @@ -207,13 +207,17 @@ void maybeOutputTimestamp(std::ostringstream &s) { static const std::pair aEnvFlags = getTimestampFlags(getLogLevelEnvVar()); const auto& [outputTimestamp, outputRelativeTimer] = (pLogSelector == nullptr ? aEnvFlags : getTimestampFlags(pLogSelector)); + if (!(outputTimestamp || outputRelativeTimer)) { + return; + } + TimeValue now; + osl_getSystemTime(&now); + if (outputTimestamp) { char ts[100]; - TimeValue systemTime; - osl_getSystemTime(&systemTime); TimeValue localTime; - osl_getLocalTimeFromSystemTime(&systemTime, &localTime); + osl_getLocalTimeFromSystemTime(&now, &localTime); oslDateTime dateTime; osl_getDateTimeFromTimeValue(&localTime, &dateTime); struct tm tm; @@ -231,23 +235,22 @@ void maybeOutputTimestamp(std::ostringstream &s) { static_cast(dateTime.NanoSeconds / 1000000)); s << ts << '.' << milliSecs << ':'; } - if (!outputRelativeTimer) - return; - TimeValue now; - osl_getSystemTime(&now); - int seconds = now.Seconds - aStartTime.aTime.Seconds; - int milliSeconds; - if (now.Nanosec < aStartTime.aTime.Nanosec) + if (outputRelativeTimer) { - seconds--; - milliSeconds = 1000 - (aStartTime.aTime.Nanosec - now.Nanosec) / 1000000; + int seconds = now.Seconds - aStartTime.aTime.Seconds; + int milliSeconds; + if (now.Nanosec < aStartTime.aTime.Nanosec) + { + seconds--; + milliSeconds = 1000 - (aStartTime.aTime.Nanosec - now.Nanosec) / 1000000; + } + else + milliSeconds = (now.Nanosec - aStartTime.aTime.Nanosec) / 1000000; + char relativeTimestamp[100]; + snprintf(relativeTimestamp, sizeof(relativeTimestamp), "%d.%03d", seconds, milliSeconds); + s << relativeTimestamp << ':'; } - else - milliSeconds = (now.Nanosec - aStartTime.aTime.Nanosec) / 1000000; - char relativeTimestamp[100]; - snprintf(relativeTimestamp, sizeof(relativeTimestamp), "%d.%03d", seconds, milliSeconds); - s << relativeTimestamp << ':'; } #endif -- cgit