本文介绍了__LINE__ __FILE__ 或 qml 中的类似函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印调用函数、行号和文件名,而不会在 QML 中为正常调试目的抛出错误.我可以按如下方式打印调用者函数名称

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

console.log("Caller Function Name"+arguments.callee.caller.name);

推荐答案

您可以覆盖 qInstallMessageHandler 默认函数并提供您的自定义函数,该函数还打印行号/调用者.您可以在链接的文档中找到示例.另一个部分示例:

You can override qInstallMessageHandler default function and provide your custom function which also prints line number / caller. You can find an example in the linked 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 分隔的临界区中,否则它只是简单地输出到每个平台的标准输出.

If logFile is open, logging data is wrote to that in a critical section delimited by a QMutex otherwise it is simply output to the standard output of each platform.

无论您定义什么处理程序,它都可以与分类日志记录(自 Qt 5.2 起可用)相结合,以轻松设置根据您的需要量身定制的自定义日志记录工具.您只需要定义您的日志记录类别,如 这篇博文,并调用qCDebug, qCInfo(), qCWarning() 等等.取决于活动类别(通过静态函数设置 setFilterRules()<QLoggingCategory的/code>) 可以打印或跳过不同的日志信息.

Whatever is the handler you define, it can be combined with categorized logging (available since Qt 5.2) to easily setup a custom logging facility tailored on your needs. You just need to define your logging categories, as described in this blog post, and call qCDebug, qCInfo(), qCWarning() and so on. Depending on the active categories (set via the static function setFilterRules() of QLoggingCategory) different logging info can be printed or skipped.

现在 Qt 5.8 可用,这尤其有趣.从这个版本开始,您可以在 QML 中使用类别,即您可以调用 console 函数并传递一个类别,例如

That's especially interesting now that Qt 5.8 is available. Since this release, you can use categories also in QML, i.e. you can call console functions and pass along a category, e.g.

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

此外,类别声明可以通过特殊类型在 QML 中完全完成 LoggingCategory.

Also, categories declaration can be done fully in QML via the ad hoc type LoggingCategory.

建议的解决方案适用于 Qt 5.0+ 环境,其中类别完全适用于 Qt 5.3+ 和 Qt 5.8+ 中可用的 QML 类别;在 Qt 4.x 环境中,您应该覆盖 qInstallMsgHandler 但您没有 QMessageLogContext.这意味着您应该在处理程序之外管理文件/行信息,例如你必须使用 Q_FUNC_INFO 或依赖在 C++ 中的 __FILE____LINE__ 上(请注意,后者已在最新的 5.x 版本中删除,例如讨论的 此处).

The proposed solution works in a Qt 5.0+ environment with categories fully usable with Qt 5.3+ and QML categories available in Qt 5.8+; in a Qt 4.x environment you should override qInstallMsgHandler but you do not have a QMessageLogContext. That means you should manage file/line info outside the handler, e.g. you have to use Q_FUNC_INFO or rely on __FILE__ and __LINE__ in C++ (note that the latters have been removed in latest 5.x releases as e.g. discussed here).

这篇关于__LINE__ __FILE__ 或 qml 中的类似函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:47