Detect when Xcode is presenting os_log as structured log messages

In that case, just like when os_log mirrors to stderr by itself, we
want to disable Qt's fallback stderr handler.

Change-Id: Ia373b19788edbce616d4f0d3d9f0b217ddc1e5c0
Reviewed-by: Alexandru Croitor <alexandru.croitor@qt.io>
(cherry picked from commit 366923b597)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2023-06-06 17:57:01 +02:00 committed by Qt Cherry-pick Bot
parent 54208c3a80
commit aae0cc0702
3 changed files with 31 additions and 13 deletions

View File

@ -86,17 +86,35 @@ QCFString::operator CFStringRef() const
#if defined(QT_USE_APPLE_UNIFIED_LOGGING)
bool AppleUnifiedLogger::willMirrorToStderr()
bool AppleUnifiedLogger::preventsStderrLogging()
{
// When running under Xcode or LLDB, one or more of these variables will
// be set, which triggers libsystem_trace.dyld to log messages to stderr
// as well, via_os_log_impl_mirror_to_stderr. Un-setting these variables
// is not an option, as that would silence normal NSLog or os_log calls,
// so instead we skip our own stderr output. See rdar://36919139.
// os_log will mirror to stderr if OS_ACTIVITY_DT_MODE is set,
// regardless of its value. OS_ACTIVITY_MODE then controls whether
// to include info and/or debug messages in this mirroring.
// For some reason, when launched under lldb (via Xcode or not),
// all levels are included.
// CFLog will normally log to both stderr, and via os_log.
// Setting CFLOG_FORCE_DISABLE_STDERR disables the stderr
// logging. Setting CFLOG_FORCE_STDERR will both duplicate
// CFLog's output to stderr, and trigger OS_ACTIVITY_DT_MODE,
// resulting in os_log calls also being mirrored to stderr.
// Setting ACTIVITY_LOG_STDERR has the same effect.
// NSLog is plumbed to CFLog, and will respond to the same
// environment variables as CFLog.
// We want to disable Qt's default stderr log handler when
// os_log has already mirrored to stderr.
static bool willMirror = qEnvironmentVariableIsSet("OS_ACTIVITY_DT_MODE")
|| qEnvironmentVariableIsSet("ACTIVITY_LOG_STDERR")
|| qEnvironmentVariableIsSet("CFLOG_FORCE_STDERR");
return willMirror;
|| qEnvironmentVariableIsSet("ACTIVITY_LOG_STDERR")
|| qEnvironmentVariableIsSet("CFLOG_FORCE_STDERR");
// As well as when we suspect that Xcode is going to present os_log
// as structured log messages.
static bool disableStderr = qEnvironmentVariableIsSet("CFLOG_FORCE_DISABLE_STDERR");
return willMirror || disableStderr;
}
QT_MAC_WEAK_IMPORT(_os_log_default);
@ -137,7 +155,7 @@ bool AppleUnifiedLogger::messageHandler(QtMsgType msgType, const QMessageLogCont
// system from redacting our log message.
os_log_with_type(log, logType, "%{public}s", qPrintable(message));
return willMirrorToStderr();
return preventsStderrLogging();
}
os_log_type_t AppleUnifiedLogger::logTypeForMessageType(QtMsgType msgType)

View File

@ -235,7 +235,7 @@ class Q_CORE_EXPORT AppleUnifiedLogger
public:
static bool messageHandler(QtMsgType msgType, const QMessageLogContext &context, const QString &message,
const QString &subsystem = QString());
static bool willMirrorToStderr();
static bool preventsStderrLogging();
private:
static os_log_type_t logTypeForMessageType(QtMsgType msgType);
static os_log_t cachedLog(const QString &subsystem, const QString &category);

View File

@ -1050,10 +1050,10 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, const char *const argv[], bool
#if defined(QT_USE_APPLE_UNIFIED_LOGGING)
// Any explicitly requested loggers will be added by now, so we can check if they use stdout
const bool safeToAddAppleLogger = !AppleUnifiedLogger::willMirrorToStderr() || !QTestLog::loggerUsingStdout();
const bool safeToAddAppleLogger = !AppleUnifiedLogger::preventsStderrLogging() || !QTestLog::loggerUsingStdout();
if (safeToAddAppleLogger && QAppleTestLogger::debugLoggingEnabled()) {
QTestLog::addLogger(QTestLog::Apple, nullptr);
if (AppleUnifiedLogger::willMirrorToStderr() && !logFilename)
if (AppleUnifiedLogger::preventsStderrLogging() && !logFilename)
addFallbackLogger = false; // Prevent plain test logger fallback below
}
#endif