__LINE__ __FILE__ OR similar function in qml

I am trying to print the caller function, line number and file name without throwing an error for the usual debugging purpose in QML. I can print the caller function name as follows

console.log("Caller Function Name"+arguments.callee.caller.name); 
+4
source share
1 answer

You can override qInstallMessageHandlerthe default function and provide your custom function, which also prints the line / caller number. You can find an example in the related documentation. Another partial example:

void loggingMessageHandler(QtMsgType type, const QMessageLogContext & context, const QString & msg)
{
    QString timeStr(QDateTime::currentDateTime().toString("dd-MM-yy HH:mm:ss:zzz"));
    QString contextString(QString("[%1  %2]").arg(context.file).arg(context.line));

    mutex.lock();

    QString level;
    if(logFile.isOpen())
    {
        switch (type) {
        case QtInfoMsg:     level = QString("INF"); break;
        case QtDebugMsg:    level = QString("DEB"); break;
        case QtWarningMsg:  level = QString("WAR"); break;
        case QtCriticalMsg: level = QString("CRT"); break;
        case QtFatalMsg:    level = QString("FTL"); break;
        }
        QTextStream stream(&logFile);
        stream << timeStr << " " << contextString << "\t"  << level << "\t"  << msg << endl;
        stream.flush();
    }

#if defined(Q_OS_WIN)
    OutputDebugString(reinterpret_cast<const wchar_t *>(level.append(' ' + msg + '\n').utf16()));
#elif defined(Q_OS_ANDROID)
    android_default_message_handler(type, context, level.append(" " + msg));
#else   // MACX || IOS || LINUX
    fprintf(stderr, "%s\n", level.append(" " + msg).toLocal8Bit().constData());
#endif
      mutex.unlock();
}

logFile , , QMutex, .

, , ( Qt 5.2), , . , , qCDebug, qCInfo(), qCWarning() . ( setFilterRules() QLoggingCategory) .

, Qt 5.8 . QML, console , .

function myFancyFunction() {
    // foo code
    console.log(myFancyCategory, "message");
    // bar code
}

, QML LoggingCategory.

ADDENDUM (Qt < 5.0)

Qt 5.0+ , Qt 5.3+ QML , Qt 5.8+; Qt 4.x qInstallMsgHandler, QMessageLogContext. , / , . Q_FUNC_INFO __FILE__ __LINE__ ++ ( , 5.x, ).

+7

Source: https://habr.com/ru/post/1664971/


All Articles