Display the QElapsedTimer reference time in %{time}

This allows the time to be synchronized between different programs, as
opposed to how long it has been since the message pattern was first
parsed...

Change-Id: If8bdfa0d997ca418a5fcae40f8c34fb77f90d2aa
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
This commit is contained in:
Thiago Macieira 2014-07-30 19:57:06 -07:00
parent 71d576abc8
commit 5d366f7e42
2 changed files with 16 additions and 7 deletions

View File

@ -828,6 +828,9 @@ struct QMessagePattern {
const char **literals; const char **literals;
const char **tokens; const char **tokens;
QString timeFormat; QString timeFormat;
#ifndef QT_BOOTSTRAPPED
QElapsedTimer timer;
#endif
#ifdef QLOGGING_HAVE_BACKTRACE #ifdef QLOGGING_HAVE_BACKTRACE
int backtraceDepth; int backtraceDepth;
QString backtraceSeparator; QString backtraceSeparator;
@ -835,10 +838,6 @@ struct QMessagePattern {
bool fromEnvironment; bool fromEnvironment;
static QBasicMutex mutex; static QBasicMutex mutex;
#ifndef QT_BOOTSTRAPPED
QElapsedTimer timer;
QDateTime startTime;
#endif
}; };
QBasicMutex QMessagePattern::mutex; QBasicMutex QMessagePattern::mutex;
@ -851,9 +850,6 @@ QMessagePattern::QMessagePattern()
, backtraceSeparator(QLatin1Char('|')) , backtraceSeparator(QLatin1Char('|'))
#endif #endif
, fromEnvironment(false) , fromEnvironment(false)
#ifndef QT_BOOTSTRAPPED
, startTime(QDateTime::currentDateTime())
#endif
{ {
#ifndef QT_BOOTSTRAPPED #ifndef QT_BOOTSTRAPPED
timer.start(); timer.start();
@ -1204,6 +1200,12 @@ QString qFormatLogMessage(QtMsgType type, const QMessageLogContext &context, con
if (pattern->timeFormat == QLatin1String("process")) { if (pattern->timeFormat == QLatin1String("process")) {
quint64 ms = pattern->timer.elapsed(); quint64 ms = pattern->timer.elapsed();
message.append(QString().sprintf("%6d.%03d", uint(ms / 1000), uint(ms % 1000))); message.append(QString().sprintf("%6d.%03d", uint(ms / 1000), uint(ms % 1000)));
} else if (pattern->timeFormat == QLatin1String("boot")) {
// just print the milliseconds since the elapsed timer reference
// like the Linux kernel does
pattern->timer.elapsed();
uint ms = pattern->timer.msecsSinceReference();
message.append(QString().sprintf("%6d.%03d", uint(ms / 1000), uint(ms % 1000)));
} else if (pattern->timeFormat.isEmpty()) { } else if (pattern->timeFormat.isEmpty()) {
message.append(QDateTime::currentDateTime().toString(Qt::ISODate)); message.append(QDateTime::currentDateTime().toString(Qt::ISODate));
} else { } else {
@ -1554,6 +1556,9 @@ void qErrnoWarning(int code, const char *msg, ...)
\row \li \c %{threadid} \li ID of current thread \row \li \c %{threadid} \li ID of current thread
\row \li \c %{type} \li "debug", "warning", "critical" or "fatal" \row \li \c %{type} \li "debug", "warning", "critical" or "fatal"
\row \li \c %{time process} \li time of the message, in seconds since the process started (the token "process" is literal) \row \li \c %{time process} \li time of the message, in seconds since the process started (the token "process" is literal)
\row \li \c %{time boot} \li the time of the message, in seconds since the system boot if that
can be determined (the token "boot" is literal). If the time since boot could not be obtained,
the output is indeterminate (see QElapsedTimer::msecsSinceReference()).
\row \li \c %{time [format]} \li system time when the message occurred, formatted by \row \li \c %{time [format]} \li system time when the message occurred, formatted by
passing the \c format to \l QDateTime::toString(). If the format is passing the \c format to \l QDateTime::toString(). If the format is
not specified, the format of Qt::ISODate is used. not specified, the format of Qt::ISODate is used.

View File

@ -134,6 +134,10 @@ qint64 QElapsedTimer::elapsed() const Q_DECL_NOTHROW
number of milliseconds since January 1st, 1970 at 0:00 UTC (that is, it number of milliseconds since January 1st, 1970 at 0:00 UTC (that is, it
is the Unix time expressed in milliseconds). is the Unix time expressed in milliseconds).
On Linux, Windows and OS X/iOS systems, this value is usually the time
since the system boot, though it usually does not include the time the
system has spent in sleep states.
\sa clockType(), elapsed() \sa clockType(), elapsed()
*/ */
qint64 QElapsedTimer::msecsSinceReference() const Q_DECL_NOTHROW qint64 QElapsedTimer::msecsSinceReference() const Q_DECL_NOTHROW