简体   繁体   English

如何将标准输出重定向到标准错误

[英]How to redirect stdout to stderr

I am working with a C++ QT application that also uses JavaScript.我正在使用 C++ QT 应用程序,该应用程序也使用 JavaScript。 In C++ I use the qDebug function and capture all data with qInstallMessageHandler.在 C++ 中,我使用 qDebug function 并使用 qInstallMessageHandler 捕获所有数据。

This captures everything that is directed to stderr.这会捕获指向 stderr 的所有内容。 In JavaScript I am using console.info which writes data to stdout.在 JavaScript 中,我使用 console.info 将数据写入标准输出。

What I want to do is redirect stdout to stderr so that all the messages written by console.info find they're way into the same message handler.我想要做的是将stdout重定向到stderr,以便console.info写入的所有消息都找到它们进入同一个消息处理程序的方式。

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);
}

You can use ios::rdbuf .您可以使用ios::rdbuf

Example:例子:

#include <iostream>

int main() {
    std::cout << "to stdout\n";
    std::cout.rdbuf(std::cerr.rdbuf());
    std::cout << "to stderr\n";
}

For my purpose, I ended up created an invokable routine in C++ which could be called from the JavaScript, C++ prototype:出于我的目的,我最终在 C++ 中创建了一个可调用例程,该例程可以从 JavaScript、C++ 原型中调用:

Q_INVOKABLE void log(QString strMsg, QString strFile, long ulngLine);

C++ implementation: 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;
}

In the JavaScript I expose my C++ layer through the object reference "api", then call the log routine:在 JavaScript 我通过 object 引用“api”公开我的 C++ 层,然后调用日志例程:

api.log("Testing", "SomeFile.js", 99);

The message handler now looks like this:消息处理程序现在看起来像这样:

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM