-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.cc
108 lines (87 loc) · 3.09 KB
/
debug.cc
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
/*
Copyright (C) 2016 Alexandr Akulich <[email protected]>
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "debug.hh"
static DebugInterface *s_instance = nullptr;
void DebugInterface::outputHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_ASSERT(s_instance);
Tp::BaseDebug *interface = s_instance->m_debugInterfacePtr.data();
const QtMessageHandler defaultHandler = s_instance->m_defaultMessageHandler;
Q_ASSERT(interface);
QString domain = QStringLiteral("%1:%2, %3");
QByteArray fileName = QByteArray::fromRawData(context.file, qstrlen(context.file));
static const char *namesToWrap[] = {
"nonsense",
"telepathy-qt"
};
for (const char *name : namesToWrap) {
int index = fileName.indexOf(name);
if (index < 0) {
continue;
}
fileName = fileName.mid(index);
break;
}
domain = domain.arg(QString::fromLocal8Bit(fileName)).arg(context.line).arg(QString::fromLatin1(context.function));
switch (type) {
case QtDebugMsg:
interface->newDebugMessage(domain, Tp::DebugLevelDebug, msg);
break;
case QtInfoMsg:
interface->newDebugMessage(domain, Tp::DebugLevelInfo, msg);
break;
case QtWarningMsg:
interface->newDebugMessage(domain, Tp::DebugLevelWarning, msg);
break;
case QtCriticalMsg:
interface->newDebugMessage(domain, Tp::DebugLevelCritical, msg);
break;
case QtFatalMsg:
interface->newDebugMessage(domain, Tp::DebugLevelError, msg);
break;
}
if (defaultHandler) {
defaultHandler(type, context, msg);
return;
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
const QString logMessage = qFormatLogMessage(type, context, msg);
if (logMessage.isNull()) {
return;
}
#else
const QString logMessage = QString::number(type) + QLatin1Char('|') + msg;
#endif
fprintf(stderr, "%s\n", logMessage.toLocal8Bit().constData());
fflush(stderr);
}
DebugInterface::DebugInterface() :
m_debugInterfacePtr(new Tp::BaseDebug()),
m_defaultMessageHandler(nullptr)
{
m_debugInterfacePtr->setGetMessagesLimit(-1);
if (!m_debugInterfacePtr->registerObject(TP_QT_CONNECTION_MANAGER_BUS_NAME_BASE + QStringLiteral("nonsense"))) {
return;
}
s_instance = this;
m_defaultMessageHandler = qInstallMessageHandler(outputHandler);
}
DebugInterface::~DebugInterface()
{
qInstallMessageHandler(m_defaultMessageHandler);
if (s_instance == this) {
s_instance = nullptr;
}
}
bool DebugInterface::isActive()
{
return m_debugInterfacePtr->isRegistered();
}