Create Logging file Using Qfile
-
@Mc6bullet said in Create Logging file Using Qfile:
all the application output
What exact output do you mean? std::cout? qDebug()? Something else?
-
@Mc6bullet said in Create Logging file Using Qfile:
Means whole console of the QT
What is Qt console? It is still not really clear what you mean.
I guess you want to write everything printed to stdout in a file?
There are different ways.
Simplest solution is to forward the stdout output to a file:./name_of_your_exe > log.txt
Or, you can do it internally in your app, see https://stackoverflow.com/questions/9211298/redirect-stdcout-to-qtextedit/45507241 for inspiration.
-
@Mc6bullet additionally to what @jsulm said, there's also
https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
that would allow you to redirect everything to a file
-
@Mc6bullet
Hi,
if you have several qDebug() that help you for diagnostic, you can introduce the following code:in main.cpp
////////////////////////////////////////////////////////////////////////////////////////////
///
void myMessageOutput(QtMsgType type, const char *msg)
{
...
// Here we can send text in the fileLog and/or console via application configuration
switch (type) {
case QtDebugMsg:
...
break;
case QtWarningMsg:
...
break;
case QtCriticalMsg:
....
break;
case QtFatalMsg:
if ( errConsoleEnable ) fprintf(stderr, "Fatal: %s\r\n", msg);
abort();
}if(errLogEnable) { outErrLog->write(text.toLatin1(), text.length()); outErrLog->flush(); }
}
in int main(int argc, char *argv[])
{
...
outErrLog = new QFile(F_GET_FILE_LOG);
outErrLog->open( QIODevice::WriteOnly | QIODevice::Append );qInstallMsgHandler(myMessageOutput); ...
}
outErrLog is QFile created once at startup
Be careful, above is Qt4 code, I think in Qt5 the called qInstallMsgHandler is obsolete, but you have to check.Another way is creating a log manager where you can store messages wherever you want (txt, csv, sql and so on). For me, for this object, I think is a good way a single instance object