Last active
July 2, 2018 03:45
-
-
Save ajithbh/9383850 to your computer and use it in GitHub Desktop.
Qt - Redirect qDebug output to file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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