JavaScript도 사용하는 C ++ QT 응용 프로그램으로 작업하고 있습니다. C ++에서는 qDebug 함수를 사용하고 qInstallMessageHandler로 모든 데이터를 캡처합니다.
이것은 stderr로 향하는 모든 것을 캡처합니다. JavaScript에서 stdout에 데이터를 쓰는 console.info를 사용하고 있습니다.
내가 원하는 것은 console.info에 의해 작성된 모든 메시지가 동일한 메시지 처리기로 들어가도록 stdout을 stderr로 리디렉션하는 것입니다.
void qDebugMsgHandler(QtMsgType type, const QMessageLogContext& context, const QString& strMsg) {
QString strOutput;
if ( context.file ) {
strOutput += QString(context.file);
if ( context.function ) {
if ( context.line > 0 ) {
strOutput += QString(" L%1").arg(context.line, 8, 10, QChar('0')) + QString(":");
}
strOutput += QString(context.function);
}
}
if ( strMsg.length() > 0 ) {
if ( strOutput.length() > 0 ) {
strOutput += ": ";
}
strOutput += strMsg;
}
switch( type ) {
case QtDebugMsg:
fprintf(stderr, " Debug:%s\n", strOutput.toLatin1().data());
break;
case QtInfoMsg:
fprintf(stderr, " Info:%s\n", strOutput.toLatin1().data());
break;
case QtWarningMsg:
fprintf(stderr, " Warning:%s\n", strOutput.toLatin1().data());
break;
case QtCriticalMsg:
fprintf(stderr, "Critical:%s\n", strOutput.toLatin1().data());
break;
case QtFatalMsg:
fprintf(stderr, " Fatal:%s\n", strOutput.toLatin1().data());
break;
}
fflush(stderr);
}
내 목적을 위해 JavaScript, C ++ 프로토 타입에서 호출 할 수있는 호출 가능한 루틴을 C ++로 만들었습니다.
Q_INVOKABLE void log(QString strMsg, QString strFile, long ulngLine);
C ++ 구현 :
void clsScriptHelper::log(QString strMsg, QString strFile, long ulngLine) {
QJsonObject json;
json["file"] = strFile;
json["line"] = QString("%1").arg(ulngLine);
json["msg"] = strMsg;
json["type"] = QString("%1").arg(QtDebugMsg);
qDebug() << json;
}
JavaScript에서 객체 참조 "api"를 통해 내 C ++ 계층을 노출 한 다음 로그 루틴을 호출합니다.
api.log("Testing", "SomeFile.js", 99);
이제 메시지 핸들러는 다음과 같습니다.
void qDebugMsgHandler(QtMsgType type, const QMessageLogContext& context, const QString& strMsg) {
static const QString scstrQJSONObject("QJsonObject(");
QString strFile, strFunction, strInfo, strOutput;
long lngLine = 0;
switch( type ) {
case QtDebugMsg:
strOutput = " Debug:";
break;
case QtInfoMsg:
strOutput = " Info:";
break;
case QtWarningMsg:
strOutput = " Warning:";
break;
case QtCriticalMsg:
strOutput = "Critical:";
break;
case QtFatalMsg:
strOutput = " Fatal:";
break;
}
if ( strMsg.startsWith(scstrQJSONObject) ) {
//Message contains a JSON object, extract the details
int intLength = strMsg.length() - (scstrQJSONObject.length() + 1);
QString strJSON = strMsg.mid(scstrQJSONObject.length(), intLength);
QJsonDocument objDoc = QJsonDocument::fromJson(strJSON.toUtf8());
if ( objDoc.isNull() ) {
return;
}
QJsonObject objJSON = objDoc.object();
strFile = objJSON.take("file").toString();
lngLine = static_cast<long>(objJSON.take("line").toDouble());
strInfo = objJSON.take("msg").toString();
type = static_cast<QtMsgType>(objJSON.take("type").toInt());
} else {
strFile = QString(context.file);
if ( context.function ) {
strFunction = QString(context.function);
}
if ( context.line > 0 ) {
lngLine = context.line;
}
strInfo = strMsg;
}
if ( strFile.length() > 0 ) {
strOutput += strFile;
}
if ( lngLine > 0 ) {
strOutput += QString(" L%1").arg(lngLine, 8, 10, QChar('0')) + QString(":");
}
if ( strFunction.length() > 0 ) {
strOutput += strFunction;
}
if ( strInfo.length() > 0 ) {
if ( strOutput.length() > 0 ) {
strOutput += ": ";
}
strOutput += strInfo;
}
std::cout << strOutput.toLatin1().data() << std::endl << std::flush;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다