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
17#define pid_t int // hack to build17#define pid_t int // hack to build
18#endif18#endif
19 19
20#include <qdebug.h>
20#include <qtimer.h>21#include <qtimer.h>
21 22
22#include "LogHandler.h"23#include "LogHandler.h"
@@ -26,7 +27,9 @@ LogHandler& LogHandler::getInstance() { · 3 unmodified lines
26 return staticInstance;27 return staticInstance;
27}28}
28 29
29LogHandler::LogHandler() {30LogHandler::LogHandler() :
31 _shouldOutputPID(false)
32{
30 // setup our timer to flush the verbose logs every 5 seconds33 // setup our timer to flush the verbose logs every 5 seconds
31 QTimer* logFlushTimer = new QTimer(this);34 QTimer* logFlushTimer = new QTimer(this);
32 connect(logFlushTimer, &QTimer::timeout, this, &LogHandler::flushRepeatedMessages);35 connect(logFlushTimer, &QTimer::timeout, this, &LogHandler::flushRepeatedMessages);
@@ -51,9 +54,33 @@ const char* stringForLogType(QtMsgType msgType) { · 18 unmodified lines
51// the following will produce 2000-10-02 13:55:36 -070054// the following will produce 2000-10-02 13:55:36 -0700
52const char DATE_STRING_FORMAT[] = "%Y-%m-%d %H:%M:%S %z";55const char DATE_STRING_FORMAT[] = "%Y-%m-%d %H:%M:%S %z";
53 56
54void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {57void LogHandler::flushRepeatedMessages() {
58 QHash<QString, int>::iterator message = _repeatMessageCountHash.begin();
59 while (message != _repeatMessageCountHash.end()) {
60 message = _repeatMessageCountHash.erase(message);
61 }
62}
63
64QString LogHandler::printMessage(QtMsgType type, const QMessageLogContext& context, const QString& message) {
65
55 if (message.isEmpty()) {66 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 }
57 }84 }
58 85
59 // log prefix is in the following format86 // log prefix is in the following format
@@ -70,27 +97,26 @@ void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& · 10 unmodified lines
70 97
71 prefixString.append(QString(" [%1]").arg(dateString));98 prefixString.append(QString(" [%1]").arg(dateString));
72 99
73 prefixString.append(QString(" [%1").arg(getpid()));100 if (_shouldOutputPID) {
74 101 prefixString.append(QString(" [%1").arg(getpid()));
75 pid_t parentProcessID = getppid();102
76 if (parentProcessID != 0) {103 pid_t parentProcessID = getppid();
77 prefixString.append(QString(":%1]").arg(parentProcessID));104 if (parentProcessID != 0) {
78 } else {105 prefixString.append(QString(":%1]").arg(parentProcessID));
79 prefixString.append("]");106 } else {
107 prefixString.append("]");
108 }
80 }109 }
81 110
82 if (!getInstance()._targetName.isEmpty()) {111 if (!_targetName.isEmpty()) {
83 prefixString.append(QString(" [%1]").arg(getInstance()._targetName));112 prefixString.append(QString(" [%1]").arg(_targetName));
84 }113 }
85 114
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;
87}118}
88 119
89void LogHandler::flushRepeatedMessages() {120void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
90 QHash<QString, int>::iterator message = _repeatMessageCountHash.begin();121 getInstance().printMessage(type, context, message);
91 while (message != _repeatMessageCountHash.end()) {
92 message = _repeatMessageCountHash.erase(message);
93 }
94}122}
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