72 lines
2.5 KiB
C++
72 lines
2.5 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
#include <QRegularExpression>
|
||
|
|
#include <QString>
|
||
|
|
#include <QJsonArray>
|
||
|
|
#include <QJsonObject>
|
||
|
|
#include <QJsonValue>
|
||
|
|
#include <VisionGuideNames.h>
|
||
|
|
#include <cstdio>
|
||
|
|
#include <cstdlib>
|
||
|
|
|
||
|
|
// Keep third-party engine names out of every user-visible log while preserving
|
||
|
|
// the useful error number and description supplied by the underlying library.
|
||
|
|
inline QString sanitizeUserVisibleLog(QString text)
|
||
|
|
{
|
||
|
|
static const QRegularExpression engineError(
|
||
|
|
QRegularExpression::escape(QString::fromLatin1(visionGuideBackendName())) + QStringLiteral("\\s+error\\b"),
|
||
|
|
QRegularExpression::CaseInsensitiveOption);
|
||
|
|
static const QRegularExpression engineName(
|
||
|
|
QRegularExpression::escape(QString::fromLatin1(visionGuideBackendName())),
|
||
|
|
QRegularExpression::CaseInsensitiveOption);
|
||
|
|
|
||
|
|
text.replace(engineError, QString::fromUtf8("视觉引导错误"));
|
||
|
|
text.replace(engineName, QString::fromUtf8("视觉引导"));
|
||
|
|
return text;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Normalize only textual JSON values. Numeric poses, scores and keys are preserved.
|
||
|
|
inline QJsonValue sanitizeUserVisibleRecord(const QJsonValue& value)
|
||
|
|
{
|
||
|
|
if (value.isString()) return sanitizeUserVisibleLog(value.toString());
|
||
|
|
if (value.isArray()) {
|
||
|
|
QJsonArray result;
|
||
|
|
for (const auto& item : value.toArray()) result.append(sanitizeUserVisibleRecord(item));
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
if (value.isObject()) {
|
||
|
|
auto result = value.toObject();
|
||
|
|
for (auto it = result.begin(); it != result.end(); ++it)
|
||
|
|
it.value() = sanitizeUserVisibleRecord(it.value());
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
return value;
|
||
|
|
}
|
||
|
|
|
||
|
|
inline QtMessageHandler& previousVisionGuideMessageHandler()
|
||
|
|
{
|
||
|
|
static QtMessageHandler handler = nullptr;
|
||
|
|
return handler;
|
||
|
|
}
|
||
|
|
|
||
|
|
inline void visionGuideMessageFilter(QtMsgType type, const QMessageLogContext& context,
|
||
|
|
const QString& message)
|
||
|
|
{
|
||
|
|
const QString clean = sanitizeUserVisibleLog(message);
|
||
|
|
const auto previous = previousVisionGuideMessageHandler();
|
||
|
|
if (previous) {
|
||
|
|
previous(type, context, clean);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const auto formatted = sanitizeUserVisibleLog(qFormatLogMessage(type, context, clean)).toLocal8Bit();
|
||
|
|
std::fprintf(stderr, "%s\n", formatted.constData());
|
||
|
|
std::fflush(stderr);
|
||
|
|
if (type == QtFatalMsg) std::abort();
|
||
|
|
}
|
||
|
|
|
||
|
|
inline void installVisionGuideMessageFilter()
|
||
|
|
{
|
||
|
|
const auto previous = qInstallMessageHandler(visionGuideMessageFilter);
|
||
|
|
if (previous != visionGuideMessageFilter) previousVisionGuideMessageHandler() = previous;
|
||
|
|
}
|