Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qappletestlogger.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QPair>
7
9
10#if defined(QT_USE_APPLE_UNIFIED_LOGGING)
11
12using namespace QTestPrivate;
13
22bool QAppleTestLogger::debugLoggingEnabled()
23{
24 // Debug-level messages are only captured in memory when debug logging is
25 // enabled through a configuration change, which can happen automatically
26 // when running inside Xcode, or with the Console application open.
27 return os_log_type_enabled(OS_LOG_DEFAULT, OS_LOG_TYPE_DEBUG);
28}
29
30QAppleTestLogger::QAppleTestLogger()
32{
33}
34
35static QAppleLogActivity testFunctionActivity;
36
37void QAppleTestLogger::enterTestFunction(const char *function)
38{
39 Q_UNUSED(function);
40
41 // Re-create activity each time
42 testFunctionActivity = QT_APPLE_LOG_ACTIVITY("Running test function").enter();
43
44 QTestCharBuffer testIdentifier;
46 QString identifier = QString::fromLatin1(testIdentifier.data());
47 QMessageLogContext context(nullptr, 0, nullptr, "qt.test.enter");
48 QString message = identifier;
49
51}
52
53void QAppleTestLogger::leaveTestFunction()
54{
55 testFunctionActivity.leave();
56}
57
58struct MessageData
59{
60 QtMsgType messageType = QtFatalMsg;
61 const char *categorySuffix = nullptr;
62
63 void generateCategory(QTestCharBuffer *category)
64 {
65 if (categorySuffix)
66 QTest::qt_asprintf(category, "qt.test.%s", categorySuffix);
67 else
68 QTest::qt_asprintf(category, "qt.test");
69 }
70};
71
72
73void QAppleTestLogger::addIncident(IncidentTypes type, const char *description,
74 const char *file, int line)
75{
76 MessageData messageData = [=]() {
77 switch (type) {
79 return MessageData{QtInfoMsg, "skip"};
81 return MessageData{QtInfoMsg, "pass"};
83 return MessageData{QtInfoMsg, "xfail"};
85 return MessageData{QtCriticalMsg, "fail"};
87 return MessageData{QtInfoMsg, "xpass"};
89 return MessageData{QtWarningMsg, "bpass"};
91 return MessageData{QtInfoMsg, "bfail"};
93 return MessageData{QtWarningMsg, "bxpass"};
95 return MessageData{QtInfoMsg, "bxfail"};
96 }
97 Q_UNREACHABLE();
98 }();
99
101 messageData.generateCategory(&category);
102
103 QMessageLogContext context(file, line, /* function = */ nullptr, category.data());
104
105 QString message = testIdentifier();
106 if (qstrlen(description))
107 message += u'\n' % QString::fromLatin1(description);
108
109 AppleUnifiedLogger::messageHandler(messageData.messageType, context, message, subsystem());
110}
111
112void QAppleTestLogger::addMessage(QtMsgType type, const QMessageLogContext &context, const QString &message)
113{
115}
116
117void QAppleTestLogger::addMessage(MessageTypes type, const QString &message, const char *file, int line)
118{
119 MessageData messageData = [=]() {
120 switch (type) {
123 return MessageData{QtWarningMsg, nullptr};
125 return MessageData{QtDebugMsg, nullptr};
127 return MessageData{QtWarningMsg, "critical"};
129 return MessageData{QtFatalMsg, nullptr};
132 return MessageData{QtInfoMsg, nullptr};
133 }
134 Q_UNREACHABLE();
135 }();
136
138 messageData.generateCategory(&category);
139
140 QMessageLogContext context(file, line, /* function = */ nullptr, category.data());
141
142 AppleUnifiedLogger::messageHandler(messageData.messageType, context, message, subsystem());
143}
144
145QString QAppleTestLogger::subsystem() const
146{
148 // It would be nice to have the data tag as part of the subsystem too, but that
149 // will for some tests result in hundreds of thousands of log objects being
150 // created, so we limit the subsystem to test functions, which we can hope
151 // are reasonably limited.
152 generateTestIdentifier(&buffer, TestObject | TestFunction);
153 return QString::fromLatin1(buffer.data());
154}
155
156QString QAppleTestLogger::testIdentifier() const
157{
160 return QString::fromLatin1(buffer.data());
161}
162
163#endif // QT_USE_APPLE_UNIFIED_LOGGING
164
static bool messageHandler(QtMsgType msgType, const QMessageLogContext &context, const QString &message, const QString &subsystem=QString())
Base class for test loggers.
\inmodule QtCore
Definition qlogging.h:39
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
const QLoggingCategory & category()
[1]
Combined button and popup list for selecting options.
void generateTestIdentifier(QTestCharBuffer *identifier, int parts)
int qt_asprintf(QTestCharBuffer *str, const char *format,...)
static void * context
size_t qstrlen(const char *str)
#define QT_APPLE_LOG_ACTIVITY(...)
QtMsgType
Definition qlogging.h:29
@ QtCriticalMsg
Definition qlogging.h:32
@ QtInfoMsg
Definition qlogging.h:34
@ QtWarningMsg
Definition qlogging.h:31
@ QtFatalMsg
Definition qlogging.h:33
@ QtDebugMsg
Definition qlogging.h:30
GLenum GLuint buffer
GLenum type
GLuint GLsizei const GLchar * message
#define Q_UNUSED(x)
QFile file
[0]
QObject::connect nullptr