自学内容网 自学内容网

QT log日志

#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QTextStream>

void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QFile logFile("log.txt");
    if (!logFile.isOpen())
        logFile.open(QIODevice::WriteOnly | QIODevice::Append);

    QTextStream ts(&logFile);
    switch (type) {
    case QtDebugMsg:
        ts << "Debug: ";
        break;
    case QtInfoMsg:
        ts << "Info: ";
        break;
    case QtWarningMsg:
        ts << "Warning: ";
        break;
    case QtCriticalMsg:
        ts << "Critical: ";
        break;
    case QtFatalMsg:
        ts << "Fatal: ";
        abort();
    }
    ts << context.file << "(" << context.line << "): " << msg << endl;
}

int main(int argc, char *argv[])
{
    qInstallMessageHandler(myMessageHandler);

    QCoreApplication a(argc, argv);

    qDebug() << "This is a debug message.";
    qInfo() << "This is an info message.";
    qWarning() << "This is a warning message.";
    qCritical() << "This is a critical message.";

    return a.exec();
}

Qt::Qt Log日志模块_何其不顾四月天-开放原子开发者工作坊


原文地址:https://blog.csdn.net/sinat_20962951/article/details/140274898

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!