LogHandler: add option to display milliseconds in timestamp

This commit is contained in:
Anthony J. Thibault 2016-02-25 10:02:17 -08:00
parent 91f6b7e80d
commit b5b3a40233
2 changed files with 14 additions and 6 deletions

View file

@ -26,9 +26,7 @@ LogHandler& LogHandler::getInstance() {
return staticInstance; return staticInstance;
} }
LogHandler::LogHandler() : LogHandler::LogHandler()
_shouldOutputProcessID(false),
_shouldOutputThreadID(false)
{ {
// setup our timer to flush the verbose logs every 5 seconds // setup our timer to flush the verbose logs every 5 seconds
QTimer* logFlushTimer = new QTimer(this); QTimer* logFlushTimer = new QTimer(this);
@ -62,6 +60,9 @@ const char* stringForLogType(LogMsgType msgType) {
// the following will produce 11/18 13:55:36 // the following will produce 11/18 13:55:36
const QString DATE_STRING_FORMAT = "MM/dd hh:mm:ss"; const QString DATE_STRING_FORMAT = "MM/dd hh:mm:ss";
// the following will produce 11/18 13:55:36.999
const QString DATE_STRING_FORMAT_WITH_MILLISECONDS = "MM/dd hh:mm:ss.zzz";
void LogHandler::flushRepeatedMessages() { void LogHandler::flushRepeatedMessages() {
QMutexLocker locker(&_repeatedMessageLock); QMutexLocker locker(&_repeatedMessageLock);
QHash<QString, int>::iterator message = _repeatMessageCountHash.begin(); QHash<QString, int>::iterator message = _repeatMessageCountHash.begin();
@ -132,7 +133,12 @@ QString LogHandler::printMessage(LogMsgType type, const QMessageLogContext& cont
// log prefix is in the following format // log prefix is in the following format
// [TIMESTAMP] [DEBUG] [PID] [TID] [TARGET] logged string // [TIMESTAMP] [DEBUG] [PID] [TID] [TARGET] logged string
QString prefixString = QString("[%1]").arg(QDateTime::currentDateTime().toString(DATE_STRING_FORMAT)); const QString* dateFormatPtr = &DATE_STRING_FORMAT;
if (_shouldDisplayMilliseconds) {
dateFormatPtr = &DATE_STRING_FORMAT_WITH_MILLISECONDS;
}
QString prefixString = QString("[%1]").arg(QDateTime::currentDateTime().toString(*dateFormatPtr));
prefixString.append(QString(" [%1]").arg(stringForLogType(type))); prefixString.append(QString(" [%1]").arg(stringForLogType(type)));

View file

@ -42,6 +42,7 @@ public:
void setShouldOutputProcessID(bool shouldOutputProcessID) { _shouldOutputProcessID = shouldOutputProcessID; } void setShouldOutputProcessID(bool shouldOutputProcessID) { _shouldOutputProcessID = shouldOutputProcessID; }
void setShouldOutputThreadID(bool shouldOutputThreadID) { _shouldOutputThreadID = shouldOutputThreadID; } void setShouldOutputThreadID(bool shouldOutputThreadID) { _shouldOutputThreadID = shouldOutputThreadID; }
void setShouldDisplayMilliseconds(bool shouldDisplayMilliseconds) { _shouldDisplayMilliseconds = shouldDisplayMilliseconds; }
QString printMessage(LogMsgType type, const QMessageLogContext& context, const QString &message); QString printMessage(LogMsgType type, const QMessageLogContext& context, const QString &message);
@ -57,8 +58,9 @@ private:
void flushRepeatedMessages(); void flushRepeatedMessages();
QString _targetName; QString _targetName;
bool _shouldOutputProcessID; bool _shouldOutputProcessID { false };
bool _shouldOutputThreadID; bool _shouldOutputThreadID { false };
bool _shouldDisplayMilliseconds { false };
QSet<QString> _repeatedMessageRegexes; QSet<QString> _repeatedMessageRegexes;
QHash<QString, int> _repeatMessageCountHash; QHash<QString, int> _repeatMessageCountHash;
QHash<QString, QString> _lastRepeatedMessage; QHash<QString, QString> _lastRepeatedMessage;