-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathLogHandler.cpp
279 lines (236 loc) · 8.62 KB
/
LogHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// LogHandler.cpp
// libraries/shared/src
//
// Created by Stephen Birarda on 2014-10-28.
// Migrated from Logging.cpp created on 6/11/13
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "LogHandler.h"
#include <mutex>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
#ifdef Q_OS_UNIX
#include <stdio.h>
#include <unistd.h>
#endif
#include <QtCore/QCoreApplication>
#include <QtCore/QDateTime>
#include <QtCore/QDebug>
#include <QtCore/QMutexLocker>
#include <QtCore/QThread>
#include <QtCore/QTimer>
#include "ScriptContextHelper.h"
QMutex LogHandler::_mutex(QMutex::Recursive);
LogHandler& LogHandler::getInstance() {
static LogHandler staticInstance;
return staticInstance;
}
LogHandler::LogHandler() {
QString logOptions = qgetenv("VIRCADIA_LOG_OPTIONS").toLower();
#ifdef Q_OS_UNIX
// Enable color by default if we're on Unix, and output is a tty (so we're not being piped into something)
//
// On Windows the situation is more complex, and color is supported or not depending on version and
// registry settings, so for now it's off by default and it's up to the user to do what's required.
if (isatty(fileno(stdout))) {
_useColor = true;
}
#endif
auto optionList = logOptions.split(",");
for (auto option : optionList) {
option = option.trimmed();
if (option == "color") {
_useColor = true;
} else if (option == "nocolor") {
_useColor = false;
} else if (option == "process_id") {
_shouldOutputProcessID = true;
} else if (option == "thread_id") {
_shouldOutputThreadID = true;
} else if (option == "milliseconds") {
_shouldDisplayMilliseconds = true;
} else if (option == "keep_repeats") {
_keepRepeats = true;
} else if (option != "") {
fprintf(stdout, "Unrecognized option in VIRCADIA_LOG_OPTIONS: '%s'\n", option.toUtf8().constData());
}
}
}
const char* stringForLogType(LogMsgType msgType) {
switch (msgType) {
case LogInfo:
return "INFO";
case LogDebug:
return "DEBUG";
case LogWarning:
return "WARNING";
case LogCritical:
return "CRITICAL";
case LogFatal:
return "FATAL";
case LogSuppressed:
return "SUPPRESS";
default:
return "UNKNOWN";
}
}
const char* colorForLogType(LogMsgType msgType) {
switch (msgType) {
case LogInfo:
return "\u001b[37;1m"; // Bold white
case LogDebug:
return "";
case LogWarning:
return "\u001b[35;1m"; // Bright magenta
case LogCritical:
return "\u001b[31;1m"; // Bright red
case LogFatal:
return "\u001b[31;1m"; // Bright red
case LogSuppressed:
return "";
default:
return "";
}
}
const char* colorReset() {
return "\u001b[0m";
}
// the following will produce 11/18 13:55:36
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::setTargetName(const QString& targetName) {
QMutexLocker lock(&_mutex);
_targetName = targetName;
}
void LogHandler::setShouldOutputProcessID(bool shouldOutputProcessID) {
QMutexLocker lock(&_mutex);
_shouldOutputProcessID = shouldOutputProcessID;
}
void LogHandler::setShouldOutputThreadID(bool shouldOutputThreadID) {
QMutexLocker lock(&_mutex);
_shouldOutputThreadID = shouldOutputThreadID;
}
void LogHandler::setShouldDisplayMilliseconds(bool shouldDisplayMilliseconds) {
QMutexLocker lock(&_mutex);
_shouldDisplayMilliseconds = shouldDisplayMilliseconds;
}
void LogHandler::flushRepeatedMessages() {
QMutexLocker lock(&_mutex);
// New repeat-suppress scheme:
for (int m = 0; m < (int)_repeatedMessageRecords.size(); ++m) {
int repeatCount = _repeatedMessageRecords[m].repeatCount;
if (repeatCount > 1) {
QString repeatLogMessage = QString().setNum(repeatCount) + " repeated log entries - Last entry: \""
+ _repeatedMessageRecords[m].repeatString + "\"";
printMessage(LogSuppressed, QMessageLogContext(), repeatLogMessage);
_repeatedMessageRecords[m].repeatCount = 0;
_repeatedMessageRecords[m].repeatString = QString();
}
}
}
QString LogHandler::printMessage(LogMsgType type, const QMessageLogContext& context, const QString& message) {
if (message.isEmpty()) {
return QString();
}
QMutexLocker lock(&_mutex);
// log prefix is in the following format
// [TIMESTAMP] [DEBUG] [PID] [TID] [TARGET] logged string
const QString* dateFormatPtr = &DATE_STRING_FORMAT;
if (_shouldDisplayMilliseconds) {
dateFormatPtr = &DATE_STRING_FORMAT_WITH_MILLISECONDS;
}
QString prefixString = QString("[%1] [%2] [%3]").arg(QDateTime::currentDateTime().toString(*dateFormatPtr),
stringForLogType(type), context.category);
if (_shouldOutputProcessID) {
prefixString.append(QString(" [%1]").arg(QCoreApplication::applicationPid()));
}
if (_shouldOutputThreadID) {
size_t threadID = (size_t)QThread::currentThreadId();
prefixString.append(QString(" [%1]").arg(threadID));
}
if (!_targetName.isEmpty()) {
prefixString.append(QString(" [%1]").arg(_targetName));
}
// for [qml] console.* messages include an abbreviated source filename
if (context.category && context.file && !strcmp("qml", context.category)) {
if (const char* basename = strrchr(context.file, '/')) {
prefixString.append(QString(" [%1]").arg(basename+1));
}
}
QString logMessage = QString("%1 %2\n").arg(prefixString, message.split('\n').join('\n' + prefixString + " "));
const char* color = "";
const char* resetColor = "";
if (_useColor) {
color = colorForLogType(type);
resetColor = colorReset();
}
if (_keepRepeats || _previousMessage != message) {
if (_repeatCount > 0) {
fprintf(stdout, "[Previous message was repeated %i times]\n", _repeatCount);
}
if (context.category && strcmp(context.category, "vircadia.script-engine.logging-agent") != 0 ) {
QStringList context = ScriptContextHelper::get();
if ( !context.isEmpty()) {
#ifdef Q_OS_WIN
OutputDebugStringA(qPrintable(QString("Script context:\n")));
#endif
fprintf(stdout, "Script context:\n");
for( auto line : context) {
fprintf(stdout, "\t%s\n", line.toStdString().c_str());
#ifdef Q_OS_WIN
OutputDebugStringA(qPrintable(line));
#endif
}
}
}
fprintf(stdout, "%s%s%s", color, qPrintable(logMessage), resetColor);
_repeatCount = 0;
} else {
_repeatCount++;
}
_previousMessage = message;
#ifdef Q_OS_WIN
// On windows, this will output log lines into the Visual Studio "output" tab
OutputDebugStringA(qPrintable(logMessage));
#endif
return logMessage;
}
void LogHandler::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
getInstance().printMessage((LogMsgType) type, context, message);
}
void LogHandler::setupRepeatedMessageFlusher() {
static std::once_flag once;
std::call_once(once, [&] {
// setup our timer to flush the verbose logs every 5 seconds
QTimer* logFlushTimer = new QTimer(this);
connect(logFlushTimer, &QTimer::timeout, this, &LogHandler::flushRepeatedMessages);
logFlushTimer->start(VERBOSE_LOG_INTERVAL_SECONDS * 1000);
});
}
int LogHandler::newRepeatedMessageID() {
QMutexLocker lock(&_mutex);
int newMessageId = _currentMessageID;
++_currentMessageID;
RepeatedMessageRecord newRecord { 0, QString() };
_repeatedMessageRecords.push_back(newRecord);
return newMessageId;
}
void LogHandler::printRepeatedMessage(int messageID, LogMsgType type, const QMessageLogContext& context,
const QString& message) {
QMutexLocker lock(&_mutex);
if (messageID >= _currentMessageID) {
return;
}
if (_repeatedMessageRecords[messageID].repeatCount == 0) {
printMessage(type, context, message);
} else {
_repeatedMessageRecords[messageID].repeatString = message;
}
++_repeatedMessageRecords[messageID].repeatCount;
}