1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtTest module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qappletestlogger_p.h"
41
42 #include <QPair>
43
44 QT_BEGIN_NAMESPACE
45
46 #if defined(QT_USE_APPLE_UNIFIED_LOGGING)
47
48 using namespace QTestPrivate;
49
debugLoggingEnabled()50 bool QAppleTestLogger::debugLoggingEnabled()
51 {
52 // Debug-level messages are only captured in memory when debug logging is
53 // enabled through a configuration change, which can happen automatically
54 // when running inside Xcode, or with the Console application open.
55 return os_log_type_enabled(OS_LOG_DEFAULT, OS_LOG_TYPE_DEBUG);
56 }
57
QAppleTestLogger()58 QAppleTestLogger::QAppleTestLogger()
59 : QAbstractTestLogger(nullptr)
60 {
61 }
62
63 static QAppleLogActivity testFunctionActivity;
64
enterTestFunction(const char * function)65 void QAppleTestLogger::enterTestFunction(const char *function)
66 {
67 Q_UNUSED(function);
68
69 // Re-create activity each time
70 testFunctionActivity = QT_APPLE_LOG_ACTIVITY("Running test function").enter();
71
72 QTestCharBuffer testIdentifier;
73 QTestPrivate::generateTestIdentifier(&testIdentifier);
74 QString identifier = QString::fromLatin1(testIdentifier.data());
75 QMessageLogContext context(nullptr, 0, nullptr, "qt.test.enter");
76 QString message = identifier;
77
78 AppleUnifiedLogger::messageHandler(QtDebugMsg, context, message, identifier);
79 }
80
leaveTestFunction()81 void QAppleTestLogger::leaveTestFunction()
82 {
83 testFunctionActivity.leave();
84 }
85
86 struct MessageData
87 {
88 QtMsgType messageType = QtFatalMsg;
89 const char *categorySuffix = nullptr;
90
generateCategoryMessageData91 void generateCategory(QTestCharBuffer *category)
92 {
93 if (categorySuffix)
94 QTest::qt_asprintf(category, "qt.test.%s", categorySuffix);
95 else
96 QTest::qt_asprintf(category, "qt.test");
97 }
98 };
99
100
addIncident(IncidentTypes type,const char * description,const char * file,int line)101 void QAppleTestLogger::addIncident(IncidentTypes type, const char *description,
102 const char *file, int line)
103 {
104 MessageData messageData = [=]() {
105 switch (type) {
106 case QAbstractTestLogger::Pass:
107 return MessageData{QtInfoMsg, "pass"};
108 case QAbstractTestLogger::XFail:
109 return MessageData{QtInfoMsg, "xfail"};
110 case QAbstractTestLogger::Fail:
111 return MessageData{QtCriticalMsg, "fail"};
112 case QAbstractTestLogger::XPass:
113 return MessageData{QtInfoMsg, "xpass"};
114 case QAbstractTestLogger::BlacklistedPass:
115 return MessageData{QtWarningMsg, "bpass"};
116 case QAbstractTestLogger::BlacklistedFail:
117 return MessageData{QtInfoMsg, "bfail"};
118 case QAbstractTestLogger::BlacklistedXPass:
119 return MessageData{QtWarningMsg, "bxpass"};
120 case QAbstractTestLogger::BlacklistedXFail:
121 return MessageData{QtInfoMsg, "bxfail"};
122 }
123 Q_UNREACHABLE();
124 }();
125
126 QTestCharBuffer category;
127 messageData.generateCategory(&category);
128
129 QMessageLogContext context(file, line, /* function = */ nullptr, category.data());
130
131 QString message = testIdentifier();
132 if (qstrlen(description))
133 message += QLatin1Char('\n') % QString::fromLatin1(description);
134
135 AppleUnifiedLogger::messageHandler(messageData.messageType, context, message, subsystem());
136 }
137
addMessage(QtMsgType type,const QMessageLogContext & context,const QString & message)138 void QAppleTestLogger::addMessage(QtMsgType type, const QMessageLogContext &context, const QString &message)
139 {
140 AppleUnifiedLogger::messageHandler(type, context, message);
141 }
142
addMessage(MessageTypes type,const QString & message,const char * file,int line)143 void QAppleTestLogger::addMessage(MessageTypes type, const QString &message, const char *file, int line)
144 {
145 MessageData messageData = [=]() {
146 switch (type) {
147 case QAbstractTestLogger::Warn:
148 case QAbstractTestLogger::QWarning:
149 return MessageData{QtWarningMsg, nullptr};
150 case QAbstractTestLogger::QDebug:
151 return MessageData{QtDebugMsg, nullptr};
152 case QAbstractTestLogger::QSystem:
153 return MessageData{QtWarningMsg, "system"};
154 case QAbstractTestLogger::QFatal:
155 return MessageData{QtFatalMsg, nullptr};
156 case QAbstractTestLogger::Skip:
157 return MessageData{QtInfoMsg, "skip"};
158 case QAbstractTestLogger::Info:
159 case QAbstractTestLogger::QInfo:
160 return MessageData{QtInfoMsg, nullptr};
161 }
162 Q_UNREACHABLE();
163 }();
164
165 QTestCharBuffer category;
166 messageData.generateCategory(&category);
167
168 QMessageLogContext context(file, line, /* function = */ nullptr, category.data());
169 QString msg = message;
170
171 if (type == Skip) {
172 if (!message.isNull())
173 msg.prepend(testIdentifier() + QLatin1Char('\n'));
174 else
175 msg = testIdentifier();
176 }
177
178 AppleUnifiedLogger::messageHandler(messageData.messageType, context, msg, subsystem());
179 }
180
subsystem() const181 QString QAppleTestLogger::subsystem() const
182 {
183 QTestCharBuffer buffer;
184 // It would be nice to have the data tag as part of the subsystem too, but that
185 // will for some tests result in hundreds of thousands of log objects being
186 // created, so we limit the subsystem to test functions, which we can hope
187 // are reasonably limited.
188 generateTestIdentifier(&buffer, TestObject | TestFunction);
189 return QString::fromLatin1(buffer.data());
190 }
191
testIdentifier() const192 QString QAppleTestLogger::testIdentifier() const
193 {
194 QTestCharBuffer buffer;
195 generateTestIdentifier(&buffer);
196 return QString::fromLatin1(buffer.data());
197 }
198
199 #endif // QT_USE_APPLE_UNIFIED_LOGGING
200
201 QT_END_NAMESPACE
202