Qt contains debugging information of five levels: Debug, Warning, Info, Critical, and Fatal. QDebug: debugging information qWarning: warning information

QInfo warning information qCritical qFatal Qt4 provides qInstallMsgHandler (Qt5: QInstallMessageHandler) redirects the output information of qDebug, qWarning, qCritical, and qFatal functions. QInstallMsgHandler is a callback function triggered by qDebug, qWarnng, qCritical, qFatal, Message text processed by qDebug, qWarnng, qCritical, and qFatal is intercepted by the callback function pointed to by qInstallMsgHandler, allowing users to process the output message text themselves.

 

A, look at the official example:

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QString text; switch(type) { case QtDebugMsg: text = QString("Debug:"); break; case QtWarningMsg: text = QString("Warning:"); break; case QtCriticalMsg: text = QString("Critical:"); break; case QtFatalMsg: text = QString("Fatal:"); } QDateTime current_date_time = QDateTime::currentDateTime(); QString current_date = current_date_time.toString("yyyy-MM-dd hh:mm:ss ddd"); QString message = text.append(msg).append("(").append(current_date).append(")"); QFile file("log.txt"); file.open(QIODevice::WriteOnly | QIODevice::Append); QTextStream text_stream(&file); text_stream<<message<<"\r\n"; file.close(); } int main(int argc, char *argv[]) { QApplication app(argc, argv); QInstallMessageHandler (outputMessage); QDebug ("This is a debug message"); qWarning("This is a warning message"); qCritical("This is a critical message"); qFatal("This is a fatal message"); return app.exec(); }Copy the code

Qt comes with five methods for writing warnings and debug text. They can be used for the following purposes:

  • QDebug () : used to write the output of customized debugging information.
  • QWarning () : Used to report warnings and recoverable errors in the program;
  • QCritical () : used to write critical error messages and report system errors.
  • QFatal () : Used to briefly describe fatal error messages before exiting;
  • QInfo () : Used to write the output of customized debugging information.

If the <QtDebug> header is included, qDebug() can be used as an output stream. Such as:

qDebug() << "Widget" << widget << "at position" << widget->pos();
Copy the code

 

Open source projects

www.qtcn.org/bbs/read-ht… The qInstallMessageHandler mechanism is used to encapsulate the log library.

I have modified it slightly to encapsulate a more powerful and easy-to-use log library.

void Log(QtMsgType type, const QMessageLogContext &context, Const QString & MSG) #endif {// add a lock to prevent multiple threads from breaking QMutex mutex; QMutexLocker locker(&mutex); // The Release version does not contain these information by default: file name, function name, line number, you need to add the following code to the.pro project file, after which it is best to rebuild the project to make it effective: // In addition, the following macros are defined in the. Pro document: They can block the output of logs that characterize characterize their relationship between men and their relationship localMsg = msg.toLocal8Bit(); QString content; switch (type) { case QtDebugMsg: content = QString("Debug: %1 (%2:%3, %4)\n").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); break; case QtInfoMsg: content = QString("Info: %1 (%2:%3, %4)\n").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); break; case QtWarningMsg: content = QString("Warning: %1 (%2:%3, %4)\n").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); break; case QtCriticalMsg: content = QString("Critical: %1 (%2:%3, %4)\n").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); break; case QtFatalMsg: content = QString("Fatal: %1 (%2:%3, %4)\n").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); break; default: content = QString("Default: %1 (%2:%3, %4)\n").arg(localMsg.constData()).arg(context.file).arg(context.line).arg(context.function); break; } content = QDateTime::currentDateTime().toString("yyyy-MM-dd hh.mm.ss.zzz ") + content; SaveLog::Instance()->save(content); }Copy the code

Complete project source code download: download.csdn.net/download/li…

 

Three, notes:

1. The Release version does not contain such information by default: file name, function name, line number, you need to add the following code to the. Pro project file, after which you’d better rebuild the project to make it effective: 2. In addition, the following macros are defined in the. Pro document: They can block the corresponding log outputs of both men’s relationship and their relationship

Remember to clean up the project and then rebuild it.

You can also be flexible:

“Pro” is used to characterize characterize characterize characterize characterize characterize characterize characterize characterize characterize characterize characterize characterize characterize characterize characterize characterize

3. If the IDE is Visual Studio, then

C/C++ /Preprocessor /Preprocessor Definitions for VC project configuration

QT_MESSAGELOGCONTEXT

QT_NO_DEBUG_OUTPUT

QT_NO_WARNING_OUTPUT

 

Another open source project

Github.com/francescmm/…

www.francescmm.com/

 

Five, sister

Use of Qt log library Log4Qt, support file name/line number/function name print-out

Spdlog log library use, support file name/line number/function name printout

Qt Custom Logging Tool