diff options
author | Jarek Kobus <[email protected]> | 2023-05-14 16:50:15 +0200 |
---|---|---|
committer | Jarek Kobus <[email protected]> | 2023-05-17 13:43:32 +0000 |
commit | 5abb1959c5883ddd74c0ddbba477a4e22881b2a8 (patch) | |
tree | 0078e0cde074aa99e59df258444cc4924094e157 /src/libs/utils/scopedtimer.cpp | |
parent | 23b0082b20272847f9d764f05c7495b15e5e941e (diff) |
ScopedTimer: Make it possible to provide optional message string
When optional message argument is provided, both macros
print the message instead of __FILE__ and __LINE__ info.
It helps to ease the identification of the exact place in code
when many macros are added - custom message may be more
informative that the file and line location.
Change-Id: I7a3ccbdaca2858b44dcbd51a8f9330160dab73e9
Reviewed-by: Qt CI Bot <[email protected]>
Reviewed-by: hjk <[email protected]>
Diffstat (limited to 'src/libs/utils/scopedtimer.cpp')
-rw-r--r-- | src/libs/utils/scopedtimer.cpp | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/src/libs/utils/scopedtimer.cpp b/src/libs/utils/scopedtimer.cpp index 97b8beffdaa..6a4522e28d0 100644 --- a/src/libs/utils/scopedtimer.cpp +++ b/src/libs/utils/scopedtimer.cpp @@ -3,7 +3,6 @@ #include "scopedtimer.h" -#include <QByteArray> #include <QDebug> #include <QTime> @@ -15,25 +14,31 @@ static QString currentTime() { return QTime::currentTime().toString(Qt::ISODateW using namespace std::chrono; +static const char s_scoped[] = "SCOPED TIMER"; +static const char s_scopedCumulative[] = "STATIC SCOPED TIMER"; + class ScopedTimerPrivate { public: - const char *m_fileName = nullptr; - const int m_line = 0; - std::atomic<int64_t> *m_cumulative = nullptr; + QString header() const { + const char *scopedTimerType = m_data.m_cumulative ? s_scopedCumulative : s_scoped; + const QString prefix = QLatin1String(scopedTimerType) + " [" + currentTime() + "] "; + const QString infix = m_data.m_message.isEmpty() + ? QLatin1String(m_data.m_fileName) + ':' + QString::number(m_data.m_line) + : m_data.m_message; + return prefix + infix + ' '; + } + + const ScopedTimerData m_data; const time_point<system_clock, nanoseconds> m_start = system_clock::now(); }; -static const char s_scoped[] = "SCOPED TIMER"; -static const char s_scopedCumulative[] = "STATIC SCOPED TIMER"; - -ScopedTimer::ScopedTimer(const char *fileName, int line, std::atomic<int64_t> *cumulative) - : d(new ScopedTimerPrivate{fileName, line, cumulative}) +ScopedTimer::ScopedTimer(const ScopedTimerData &data) + : d(new ScopedTimerPrivate{data}) { - if (d->m_cumulative) + if (d->m_data.m_cumulative) return; - qDebug().noquote().nospace() << s_scoped << " [" << currentTime() << "] in " << d->m_fileName - << ':' << d->m_line << " started"; + qDebug().noquote().nospace() << d->header() << "started"; } static int64_t toMs(int64_t ns) { return ns / 1000000; } @@ -42,8 +47,8 @@ ScopedTimer::~ScopedTimer() { const auto elapsed = duration_cast<nanoseconds>(system_clock::now() - d->m_start); QString suffix; - if (d->m_cumulative) { - const int64_t nsOld = d->m_cumulative->fetch_add(elapsed.count()); + if (d->m_data.m_cumulative) { + const int64_t nsOld = d->m_data.m_cumulative->fetch_add(elapsed.count()); const int64_t msOld = toMs(nsOld); const int64_t nsNew = nsOld + elapsed.count(); const int64_t msNew = toMs(nsNew); @@ -52,13 +57,11 @@ ScopedTimer::~ScopedTimer() if (nsOld != 0 && msOld / 10 == msNew / 10) return; - suffix = " cumulative timeout: " + QString::number(msNew) + "ms"; + suffix = "cumulative timeout: " + QString::number(msNew) + "ms"; } else { - suffix = " stopped with timeout: " + QString::number(toMs(elapsed.count())) + "ms"; + suffix = "stopped with timeout: " + QString::number(toMs(elapsed.count())) + "ms"; } - const char *header = d->m_cumulative ? s_scopedCumulative : s_scoped; - qDebug().noquote().nospace() << header << " [" << currentTime() << "] in " << d->m_fileName - << ':' << d->m_line << suffix; + qDebug().noquote().nospace() << d->header() << suffix; } } // namespace Utils |