From df3f04a951df91053269f00c2e07bbfaf37dabf0 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 30 Jun 2021 16:30:11 +0200 Subject: [PATCH] added to misc: timespec_to_stream_with_unit() for printing times either is seconds or miliseconds --- winixd/core/app.cpp | 8 ++++---- winixd/core/misc.cpp | 28 ++++++++++++++++++++++++++++ winixd/core/misc.h | 2 +- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/winixd/core/app.cpp b/winixd/core/app.cpp index d5fa67b..a713f48 100644 --- a/winixd/core/app.cpp +++ b/winixd/core/app.cpp @@ -848,9 +848,9 @@ void App::MakePage() calculate_timespec_diff(cur.request->timespec_ezc_engine_start, cur.request->timespec_ezc_engine_stop, diff); pt::TextStream str; - timespec_to_stream(diff, str); // IMPROVEME in the future Log can be used directly + timespec_to_stream_with_unit(diff, str); // IMPROVEME in the future Log can be used directly - log << log3 << "App: ezc engine took: " << str << "s" << logend; + log << log3 << "App: ezc engine took: " << str << logend; } @@ -2146,8 +2146,8 @@ void App::LogUserGroups() void App::LogRequestTime() { pt::TextStream str; - timespec_to_stream(cur.request->timespec_req_diff, str); - log << log2 << "App: request took: " << str << "s" << logend; + timespec_to_stream_with_unit(cur.request->timespec_req_diff, str); + log << log2 << "App: request took: " << str << logend; } diff --git a/winixd/core/misc.cpp b/winixd/core/misc.cpp index bf3a56a..9614320 100644 --- a/winixd/core/misc.cpp +++ b/winixd/core/misc.cpp @@ -1509,6 +1509,34 @@ void timespec_to_stream(timespec & val, pt::Stream & stream) } +void timespec_to_stream_with_unit(timespec & val, pt::Stream & stream) +{ + char buf[50]; + bool is_ms = false; + + double val_double = timespec_to_double(val); + + if( val_double < 0.1 ) + { + is_ms = true; + val_double = val_double * 1000; + } + + sprintf(buf, "%f", val_double); + SetNonZeroDigitsAfterComma(buf, 2); + + stream << buf; + + if( is_ms ) + { + stream << "ms"; + } + else + { + stream << "s"; + } +} + } // namespace Winix diff --git a/winixd/core/misc.h b/winixd/core/misc.h index e46dc98..04bb5db 100644 --- a/winixd/core/misc.h +++ b/winixd/core/misc.h @@ -1013,7 +1013,7 @@ double timespec_to_double(timespec & val); void timespec_to_stream(timespec & val, pt::Stream & stream); - +void timespec_to_stream_with_unit(timespec & val, pt::Stream & stream);