sal/osl/all/log.cxx | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 2 deletions(-)
New commits: commit c4df5aa553f49b06f47614c53824726d8ce2f0ee Author: Samuel Mehrbrodt <samuel.mehrbr...@cib.de> Date: Tue Jan 17 09:40:15 2017 +0100 Optionally include timestamp in the log Change-Id: Ia9f162b704b6e16c84f259e9548e91b7bcd6a378 diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx index 0d920ba..777d22c 100644 --- a/sal/osl/all/log.cxx +++ b/sal/osl/all/log.cxx @@ -15,6 +15,7 @@ #include <cstdio> #include <cstdlib> #include <cstring> +#include <ctime> #include <sstream> #include <stdio.h> @@ -165,6 +166,81 @@ std::ofstream * getLogFile() { return &file; } +void maybeOutputTimestamp(std::ostringstream &s) { + char const * env = getLogLevel(); + if (env == nullptr) + return; + bool outputTimestamp = false; + bool outputRelativeTimer = false; + for (char const * p = env;;) { + switch (*p++) { + case '\0': + if (outputTimestamp) { + char ts[100]; + TimeValue systemTime; + osl_getSystemTime(&systemTime); + TimeValue localTime; + osl_getLocalTimeFromSystemTime(&systemTime, &localTime); + oslDateTime dateTime; + osl_getDateTimeFromTimeValue(&localTime, &dateTime); + struct tm tm; + tm.tm_sec = dateTime.Seconds; + tm.tm_min = dateTime.Minutes; + tm.tm_hour = dateTime.Hours; + tm.tm_mday = dateTime.Day; + tm.tm_mon = dateTime.Month - 1; + tm.tm_year = dateTime.Year - 1900; + strftime(ts, sizeof(ts), "%Y-%m-%d:%H:%M:%S", &tm); + char milliSecs[10]; + sprintf(milliSecs, "%03d", static_cast<int>(dateTime.NanoSeconds/1000000)); + s << ts << '.' << milliSecs << ':'; + } + if (outputRelativeTimer) { + static bool beenHere = false; + static TimeValue first; + if (!beenHere) { + osl_getSystemTime(&first); + beenHere = true; + } + TimeValue now; + osl_getSystemTime(&now); + int seconds = now.Seconds - first.Seconds; + int milliSeconds; + if (now.Nanosec < first.Nanosec) { + seconds--; + milliSeconds = 1000-(first.Nanosec-now.Nanosec)/1000000; + } + else + milliSeconds = (now.Nanosec-first.Nanosec)/1000000; + char relativeTimestamp[100]; + sprintf(relativeTimestamp, "%d.%03d", seconds, milliSeconds); + s << relativeTimestamp << ':'; + } + return; + case '+': + { + char const * p1 = p; + while (*p1 != '.' && *p1 != '+' && *p1 != '-' && *p1 != '\0') { + ++p1; + } + if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("TIMESTAMP"))) + outputTimestamp = true; + else if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("RELATIVETIMER"))) + outputRelativeTimer = true; + char const * p2 = p1; + while (*p2 != '+' && *p2 != '-' && *p2 != '\0') { + ++p2; + } + p = p2; + } + break; + default: + ; // nothing + } + } + return; +} + #endif bool report(sal_detail_LogLevel level, char const * area) { @@ -172,7 +248,7 @@ bool report(sal_detail_LogLevel level, char const * area) { return true; assert(area != 0); static char const * env = getLogLevel(); - if (env == 0) { + if (env == 0 || strcmp(env, "+TIMESTAMP") == 0) { env = "+WARN"; } std::size_t areaLen = std::strlen(area); @@ -209,6 +285,10 @@ bool report(sal_detail_LogLevel level, char const * area) { } else if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("WARN"))) { match = level == SAL_DETAIL_LOG_LEVEL_WARN; + } else if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("TIMESTAMP"))) + { + // handled later + match = false; } else { return true; // upon an illegal SAL_LOG value, everything is considered @@ -243,8 +323,9 @@ void log( std::ostringstream s; #if !defined ANDROID // On Android, the area will be used as the "tag," and log info already - // contains the PID + // contains timestamp and PID. if (!sal_use_syslog) { + maybeOutputTimestamp(s); s << toString(level) << ':'; } if (level != SAL_DETAIL_LOG_LEVEL_DEBUG) { _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits