I am working with a C++ QT application that also uses JavaScript. In C++ I use the qDebug function and capture all data with qInstallMessageHandler.
This captures everything that is directed to stderr. In JavaScript I am using console.info which writes data to stdout.
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.
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);
}