include/sal/log.hxx | 21 +++++++++----- sal/osl/all/log.cxx | 73 +++++++++++++++++++++++++++++++++++----------------- 2 files changed, 64 insertions(+), 30 deletions(-)
New commits: commit ed815a242ef962afa52033a0d94ba6aa4539dd07 Author: Tor Lillqvist <t...@collabora.com> Date: Thu Aug 18 15:31:25 2016 +0300 Add handling of a +RELATIVETIMER flag in the SAL_LOG environment variable Outputs a timestamp in decimal seconds (with millisecond accuracy). Simplified the handling of SAL_LOG if no "level" is specified. Now just a totally unset (or empty) SAL_LOG causes the default of "+WARN" to be used. Given how the code works, it would have become too unwieldy to check for all combinations of TIMESTAMP and RELATIVETIMER but no WARN or INFO. Change-Id: I7bb5bb665d4e764e7eee447e93486f6467042e97 diff --git a/include/sal/log.hxx b/include/sal/log.hxx index 6ce3bfe..e879a2f 100644 --- a/include/sal/log.hxx +++ b/include/sal/log.hxx @@ -231,18 +231,25 @@ inline char const * unwrapStream(SAL_UNUSED_PARAMETER StreamIgnore const &) { <switch> ::= <sense><item> <sense> ::= "+"|"-" <item> ::= <flag>|<level>("."<area>)? - <flag> ::= "TIMESTAMP" + <flag> ::= "TIMESTAMP"|"RELATIVETIMER" <level> ::= "INFO"|"WARN" @endverbatim - If the environment variable is unset, or contains no level, the level - setting "+WARN" is assumed instead (which results in all warnings being - output but no infos). If the given value does not match the regular - expression, "+INFO+WARN" is used instead (which in turn results in - everything being output). + If the environment variable is unset, the setting "+WARN" is + assumed instead (which results in all warnings being output but no + infos). If the given value does not match the regular expression, + "+INFO+WARN" is used instead (which in turn results in everything + being output). The "+TIMESTAMP" flag causes each output line (as selected by the level - switch(es)) to be prefixed by a timestamp like 2016-08-18:14:04:43.. + switch(es)) to be prefixed by a timestamp like 2016-08-18:14:04:43. + + The "+RELATIVETIMER" flag causes each output line (as selected by + the level switch(es)) to be prefixed by a relative timestamp in + seconds since the first output line like 1.312. + + If both +TIMESTAMP and +RELATIVETIMER are specified, they are + output in that order. Specifying a flag with a negative sense has no effect. Specifying the same flag multiple times has no extra effect. diff --git a/sal/osl/all/log.cxx b/sal/osl/all/log.cxx index 276cbe4..7f865f6 100644 --- a/sal/osl/all/log.cxx +++ b/sal/osl/all/log.cxx @@ -109,9 +109,52 @@ void maybeOutputTimestamp(std::ostringstream &s) { char const * env = getEnvironmentVariable(); 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", 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 '+': { @@ -119,27 +162,10 @@ void maybeOutputTimestamp(std::ostringstream &s) { while (*p1 != '.' && *p1 != '+' && *p1 != '-' && *p1 != '\0') { ++p1; } - if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("TIMESTAMP"))) { - 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", dateTime.NanoSeconds/1000000); - s << ts << '.' << milliSecs << ':'; - return; - } + 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; @@ -166,7 +192,7 @@ bool report(sal_detail_LogLevel level, char const * area) { return true; assert(area != nullptr); char const * env = getEnvironmentVariable(); - if (env == nullptr || strcmp(env, "+TIMESTAMP") == 0) { + if (env == nullptr) { env = "+WARN"; } std::size_t areaLen = std::strlen(area); @@ -203,7 +229,8 @@ 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"))) + } else if (equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("TIMESTAMP")) || + equalStrings(p, p1 - p, RTL_CONSTASCII_STRINGPARAM("RELATIVETIMER"))) { // handled later match = false; _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits