Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
7
8#if defined(QT_USE_APPLE_UNIFIED_LOGGING)
9
10using namespace QTestPrivate;
11
20bool QAppleTestLogger::debugLoggingEnabled()
21{
22 // Debug-level messages are only captured in memory when debug logging is
23 // enabled through a configuration change, which can happen automatically
24 // when running inside Xcode, or with the Console application open.
25 return os_log_type_enabled(OS_LOG_DEFAULT, OS_LOG_TYPE_DEBUG);
26}
27
28QAppleTestLogger::QAppleTestLogger()
30{
31}
32
33static QAppleLogActivity testFunctionActivity;
34
35void QAppleTestLogger::enterTestFunction(const char *function)
36{
37 Q_UNUSED(function);
38
39 // Re-create activity each time
40 testFunctionActivity = QT_APPLE_LOG_ACTIVITY("Running test function").enter();
41
42 QTestCharBuffer testIdentifier;
44 QString identifier = QString::fromLatin1(testIdentifier.data());
45 QMessageLogContext context(nullptr, 0, nullptr, "qt.test.enter");
46 QString message = identifier;
47
49}
50
51void QAppleTestLogger::leaveTestFunction()
52{
53 testFunctionActivity.leave();
54}
55
56struct MessageData
57{
58 QtMsgType messageType = QtFatalMsg;
59 const char *categorySuffix = nullptr;
60
61 void generateCategory(QTestCharBuffer *category)
62 {
63 if (categorySuffix)
64 QTest::qt_asprintf(category, "qt.test.%s", categorySuffix);
65 else
66 QTest::qt_asprintf(category, "qt.test");
67 }
68};
69
70
71void QAppleTestLogger::addIncident(IncidentTypes type, const char *description,
72 const char *file, int line)
73{
74 MessageData messageData = [=]() {
75 switch (type) {
77 return MessageData{QtInfoMsg, "skip"};
79 return MessageData{QtInfoMsg, "pass"};
81 return MessageData{QtInfoMsg, "xfail"};
83 return MessageData{QtCriticalMsg, "fail"};
85 return MessageData{QtInfoMsg, "xpass"};
87 return MessageData{QtWarningMsg, "bpass"};
89 return MessageData{QtInfoMsg, "bfail"};
91 return MessageData{QtWarningMsg, "bxpass"};
93 return MessageData{QtInfoMsg, "bxfail"};
94 }
95 Q_UNREACHABLE();
96 }();
97
99 messageData.generateCategory(&category);
100
101 QMessageLogContext context(file, line, /* function = */ nullptr, category.data());
102
103 QString message = testIdentifier();
104 if (qstrlen(description))
105 message += u'\n' % QString::fromLatin1(description);
106
107 // As long as the Apple logger doesn't propagate the context's file and
108 // line number we need to manually print it.
109 if (context.line && context.file) {
111 QTest::qt_asprintf(&line, "\n [Loc: %s:%d]", context.file, context.line);
113 }
114
115 AppleUnifiedLogger::messageHandler(messageData.messageType, context, message, subsystem());
116}
117
118void QAppleTestLogger::addMessage(QtMsgType type, const QMessageLogContext &context, const QString &message)
119{
121}
122
123void QAppleTestLogger::addMessage(MessageTypes type, const QString &message, const char *file, int line)
124{
125 MessageData messageData = [=]() {
126 switch (type) {
129 return MessageData{QtWarningMsg, nullptr};
131 return MessageData{QtDebugMsg, nullptr};
133 return MessageData{QtWarningMsg, "critical"};
135 return MessageData{QtFatalMsg, nullptr};
138 return MessageData{QtInfoMsg, nullptr};
139 }
140 Q_UNREACHABLE();
141 }();
142
144 messageData.generateCategory(&category);
145
146 QMessageLogContext context(file, line, /* function = */ nullptr, category.data());
147
148 AppleUnifiedLogger::messageHandler(messageData.messageType, context, message, subsystem());
149}
150
151QString QAppleTestLogger::subsystem() const
152{
154 // It would be nice to have the data tag as part of the subsystem too, but that
155 // will for some tests result in hundreds of thousands of log objects being
156 // created, so we limit the subsystem to test functions, which we can hope
157 // are reasonably limited.
158 generateTestIdentifier(&buffer, TestObject | TestFunction);
159 return QString::fromLatin1(buffer.data());
160}
161
162QString QAppleTestLogger::testIdentifier() const
163{
166 return QString::fromLatin1(buffer.data());
167}
168
169#endif // QT_USE_APPLE_UNIFIED_LOGGING
170
static bool messageHandler(QtMsgType msgType, const QMessageLogContext &context, const QString &message)
Base class for test loggers.
\inmodule QtCore
Definition qlogging.h:42
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
QChar * data()
Returns a pointer to the data stored in the QString.
Definition qstring.h:1240
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
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define Q_UNUSED(x)
QFile file
[0]
QObject::connect nullptr