#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日志模块_何其不顾四月天-开放原子开发者工作坊

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部