History

use the LogHandler in Interface

Stephen Birarda committed 12 years ago

Commit 8e310221 parent d3bbd254 files · +61 −28Browse files

Showing the changes to libraries/shared/src/LogHandler.cpp (1 of 4 files). Show all

Unified · Split

libraries/shared/src/LogHandler.cpp+46−20
@@ -17,6 +17,7 @@ · 16 unmodified lines
1717 #define pid_t int // hack to build
1818 #endif
1919
20+#include <qdebug.h>
2021 #include <qtimer.h>
2122
2223 #include "LogHandler.h"
@@ -26,7 +27,9 @@ LogHandler& LogHandler::getInstance() { · 3 unmodified lines
2627 return staticInstance;
2728 }
2829
29−LogHandler::LogHandler() {
30+LogHandler::LogHandler() :
31+ _shouldOutputPID(false)
32+{
3033 // setup our timer to flush the verbose logs every 5 seconds
3134 QTimer* logFlushTimer = new QTimer(this);
3235 connect(logFlushTimer, &QTimer::timeout, this, &LogHandler::flushRepeatedMessages);
@@ -51,9 +54,33 @@ const char* stringForLogType(QtMsgType msgType) { · 18 unmodified lines
5154 // the following will produce 2000-10-02 13:55:36 -0700
5255 const char DATE_STRING_FORMAT[] = "%Y-%m-%d %H:%M:%S %z";
5356
54−void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
57+void LogHandler::flushRepeatedMessages() {
58+ QHash<QString, int>::iterator message = _repeatMessageCountHash.begin();
59+ while (message != _repeatMessageCountHash.end()) {
60+ message = _repeatMessageCountHash.erase(message);
61+ }
62+}
63+
64+QString LogHandler::printMessage(QtMsgType type, const QMessageLogContext& context, const QString& message) {
65+
5566 if (message.isEmpty()) {
56− return;
67+ return QString();
68+ }
69+
70+ if (type == QtDebugMsg) {
71+ // for debug messages, check if this matches any of our regexes for repeated log messages
72+ foreach(const QString& regexString, getInstance()._repeatedMessageRegexes) {
73+ QRegExp repeatRegex(regexString);
74+ if (repeatRegex.indexIn(message) != -1) {
75+
76+ // we have a match - add 1 to the count of repeats for this message and set this as the last repeated message
77+ _repeatMessageCountHash[regexString] += 1;
78+ _lastRepeatedMessage[regexString] = message;
79+
80+ // return out, we're not printing this one
81+ return QString();
82+ }
83+ }
5784 }
5885
5986 // log prefix is in the following format
@@ -70,27 +97,26 @@ void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& · 10 unmodified lines
7097
7198 prefixString.append(QString(" [%1]").arg(dateString));
7299
73− prefixString.append(QString(" [%1").arg(getpid()));
74−
75− pid_t parentProcessID = getppid();
76− if (parentProcessID != 0) {
77− prefixString.append(QString(":%1]").arg(parentProcessID));
78− } else {
79− prefixString.append("]");
100+ if (_shouldOutputPID) {
101+ prefixString.append(QString(" [%1").arg(getpid()));
102+
103+ pid_t parentProcessID = getppid();
104+ if (parentProcessID != 0) {
105+ prefixString.append(QString(":%1]").arg(parentProcessID));
106+ } else {
107+ prefixString.append("]");
108+ }
80109 }
81110
82− if (!getInstance()._targetName.isEmpty()) {
83− prefixString.append(QString(" [%1]").arg(getInstance()._targetName));
111+ if (!_targetName.isEmpty()) {
112+ prefixString.append(QString(" [%1]").arg(_targetName));
84113 }
85114
86− fprintf(stdout, "%s %s\n", prefixString.toLocal8Bit().constData(), message.toLocal8Bit().constData());
115+ QString logMessage = QString("%1 %2").arg(prefixString, message);
116+ fprintf(stdout, "%s\n", qPrintable(logMessage));
117+ return logMessage;
87118 }
88119
89−void LogHandler::flushRepeatedMessages() {
90− QHash<QString, int>::iterator message = _repeatMessageCountHash.begin();
91− while (message != _repeatMessageCountHash.end()) {
92− message = _repeatMessageCountHash.erase(message);
93− }
120+void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
121+ getInstance().printMessage(type, context, message);
94122 }
95−
96−

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t