Creating an instance of Qt File-Based Logger for debugging in the C ++ library

The following page provides a good simple solution for file-based writing in Qt for debugging without using a larger logging structure, like many of the other SO questions suggested.

I am writing a library and would like to create an instance of a registrar that classes in the library can use (mainly for debugging purposes). There is no function int main()since it is a library. So the best thing would be to add an instance to the type file logger.hand to include any classes logger.hif it would like to do qDebug() << PREFIX << "Bla", as the link suggests above?

+3
source share
3 answers

OrcunC, ofstream Qt.

:

:

// setup the global logger somewhere appropriate
QFile *file = new QFile("your.log");
file->open(QIODevice::ReadOnly);
QTextStream *qlogger = new QTextStream(file);

, :

#include "qlogger.h"
//... and within some method
*qlogger << "your log" << aQtValueType;

:

#include "qlogger.h"
// lower number = higher priority
void setCurrentLogLevel(int level) {
   globalLogLevel = level;
}
QTextStream* qLog(int level) {
   if (level <= globalLogLevel) {
       return qlogger;
   }
   return getNullLogger(); // implementation left to reader
}

, , , LogLevel, - :

#include "qlogger.h"
//...
setCurrentLogLevel(LogLevel::Warning);
*qLog(LogLevel::Debug) << "this will be filtered" << yourQMap;
*qLog(LogLevel::Critical) << "not filtered" << yourQString;

, .

+3

, qCritical(), qDebug(), qFatal() qWarning() .

! , QT. , , .

, * * .

+2

, ++ c, .h define / a. cpp/.c . .

  • The .h file must be used to compile a third-party application using your library, and the library itself is used during the connection.
  • A developer who uses your library can use the .h file as a link to your library because it contains all the declarations.

So yes, you need to declare methods in the .h file and include other classes logger.h.

+1
source

All Articles