GH-720 Add timestamps (since mmc start) to log
This commit is contained in:
parent
55e5322fbe
commit
0be0e822e4
@ -37,11 +37,7 @@ namespace QsLogging
|
|||||||
{
|
{
|
||||||
typedef QList<Destination *> DestinationList;
|
typedef QList<Destination *> DestinationList;
|
||||||
|
|
||||||
static const char *LevelStrings[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
|
static const char *LevelStrings[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "UNKNOWN"};
|
||||||
"UNKNOWN"};
|
|
||||||
|
|
||||||
// not using Qt::ISODate because we need the milliseconds too
|
|
||||||
static const QString fmtDateTime("hhhh:mm:ss.zzz");
|
|
||||||
|
|
||||||
static const char *LevelToText(Level theLevel)
|
static const char *LevelToText(Level theLevel)
|
||||||
{
|
{
|
||||||
@ -62,10 +58,12 @@ public:
|
|||||||
QMutex logMutex;
|
QMutex logMutex;
|
||||||
Level level;
|
Level level;
|
||||||
DestinationList destList;
|
DestinationList destList;
|
||||||
|
QDateTime startTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
Logger::Logger() : d(new LoggerImpl)
|
Logger::Logger() : d(new LoggerImpl)
|
||||||
{
|
{
|
||||||
|
d->startTime = QDateTime::currentDateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::~Logger()
|
Logger::~Logger()
|
||||||
@ -89,13 +87,32 @@ Level Logger::loggingLevel() const
|
|||||||
return d->level;
|
return d->level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QDateTime Logger::timeOfStart() const
|
||||||
|
{
|
||||||
|
return d->startTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
qint64 Logger::timeSinceStart() const
|
||||||
|
{
|
||||||
|
return d->startTime.msecsTo(QDateTime::currentDateTime());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//! creates the complete log message and passes it to the logger
|
//! creates the complete log message and passes it to the logger
|
||||||
void Logger::Helper::writeToLog()
|
void Logger::Helper::writeToLog()
|
||||||
{
|
{
|
||||||
const char *const levelName = LevelToText(level);
|
const char *const levelName = LevelToText(level);
|
||||||
const QString completeMessage(QString("%1\t%2").arg(levelName, 5).arg(buffer));
|
|
||||||
|
|
||||||
Logger &logger = Logger::instance();
|
Logger &logger = Logger::instance();
|
||||||
|
qint64 msecstotal = logger.timeSinceStart();
|
||||||
|
qint64 seconds = msecstotal / 1000;
|
||||||
|
qint64 msecs = msecstotal % 1000;
|
||||||
|
QString foo;
|
||||||
|
char buf[1024];
|
||||||
|
|
||||||
|
::snprintf(buf, 1024, "%5lld.%03lld", seconds, msecs);
|
||||||
|
|
||||||
|
const QString completeMessage(QString("%1\t%2\t%3").arg(buf).arg(levelName, 5).arg(buffer));
|
||||||
|
|
||||||
QMutexLocker lock(&logger.d->logMutex);
|
QMutexLocker lock(&logger.d->logMutex);
|
||||||
logger.write(completeMessage);
|
logger.write(completeMessage);
|
||||||
}
|
}
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QDateTime>
|
||||||
|
|
||||||
namespace QsLogging
|
namespace QsLogging
|
||||||
{
|
{
|
||||||
@ -60,6 +61,11 @@ public:
|
|||||||
void setLoggingLevel(Level newLevel);
|
void setLoggingLevel(Level newLevel);
|
||||||
//! The default level is INFO
|
//! The default level is INFO
|
||||||
Level loggingLevel() const;
|
Level loggingLevel() const;
|
||||||
|
//! msecs since the logger was initialized
|
||||||
|
qint64 timeSinceStart() const;
|
||||||
|
//! time when the logger was initialized
|
||||||
|
QDateTime timeOfStart() const;
|
||||||
|
|
||||||
|
|
||||||
//! The helper forwards the streaming to QDebug and builds the final
|
//! The helper forwards the streaming to QDebug and builds the final
|
||||||
//! log message.
|
//! log message.
|
||||||
|
Loading…
Reference in New Issue
Block a user