Skip to content

Instantly share code, notes, and snippets.

@ajithbh
Last active July 2, 2018 03:45
Show Gist options
  • Save ajithbh/9383850 to your computer and use it in GitHub Desktop.
Save ajithbh/9383850 to your computer and use it in GitHub Desktop.
Qt - Redirect qDebug output to file
#include <fstream>
using namespace std;
ofstream logfile;
void LogToFile(QtMsgType type, const char *msg) {
QString datetime = QDateTime::currentDateTime().toString("yyyy.MM.dd hh:mm:ss");
switch (type) {
case QtDebugMsg:
logfile << datetime.toString().toAscii().data() << " [Debug] " << msg << "\n";
break;
case QtCriticalMsg:
logfile << datetime.toString().toAscii().data() << " [Critical] " << msg << "\n";
break;
case QtWarningMsg:
logfile << datetime.toString().toAscii().data() << " [Warning] " << msg << "\n";
break;
case QtFatalMsg:
logfile << datetime.toString().toAscii().data() << " [Fatal] " << msg << "\n";
abort();
}
}
logfile.open("/tmp/log.txt", ofstream::out | ofstream::app);
qInstallMsgHandler(LogToFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment