1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

rework the logging and debug output

This commit is contained in:
sudden6 2015-05-10 16:08:09 +02:00 committed by tux3
parent ef780421f3
commit 0e91d59454
2 changed files with 34 additions and 11 deletions

View File

@ -51,6 +51,7 @@ DEFINES += GIT_VERSION=\"\\\"$$quote($$GIT_VERSION)\\\"\"
TIMESTAMP = $$system($1 2>null||echo 0||a;rm null;date +%s||echo 0) # I'm so sorry TIMESTAMP = $$system($1 2>null||echo 0||a;rm null;date +%s||echo 0) # I'm so sorry
DEFINES += TIMESTAMP=$$TIMESTAMP DEFINES += TIMESTAMP=$$TIMESTAMP
DEFINES += LOG_TO_FILE DEFINES += LOG_TO_FILE
DEFINES += QT_MESSAGELOGCONTEXT
android { android {
ANDROID_TOOLCHAIN=/opt/android/toolchain-r9d-17/ ANDROID_TOOLCHAIN=/opt/android/toolchain-r9d-17/

View File

@ -43,26 +43,49 @@
#define EXIT_UPDATE_MACX_FAIL 216 #define EXIT_UPDATE_MACX_FAIL 216
#ifdef LOG_TO_FILE #ifdef LOG_TO_FILE
static QtMessageHandler dflt;
static QTextStream* logFile {nullptr}; static QTextStream* logFile {nullptr};
static QMutex mutex; static QMutex mutex;
#endif
void myMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QString& msg) void logMessageHandler(QtMsgType type, const QMessageLogContext& ctxt, const QString& msg)
{ {
if (!logFile)
return;
// Silence qWarning spam due to bug in QTextBrowser (trying to open a file for base64 images) // Silence qWarning spam due to bug in QTextBrowser (trying to open a file for base64 images)
if (ctxt.function == QString("virtual bool QFSFileEngine::open(QIODevice::OpenMode)") if (ctxt.function == QString("virtual bool QFSFileEngine::open(QIODevice::OpenMode)")
&& msg == QString("QFSFileEngine::open: No file name specified")) && msg == QString("QFSFileEngine::open: No file name specified"))
return; return;
QString LogMsg = QString("[%1] %2:%3 : ")
.arg(QTime::currentTime().toString("HH:mm:ss.zzz")).arg(ctxt.file).arg(ctxt.line);
switch (type)
{
case QtDebugMsg:
LogMsg += "Debug";
break;
case QtWarningMsg:
LogMsg += "Warning";
break;
case QtCriticalMsg:
LogMsg += "Critical";
break;
case QtFatalMsg:
LogMsg += "Fatal";
break;
}
LogMsg += ": " + msg + "\n";
QTextStream out(stderr, QIODevice::WriteOnly);
out << LogMsg;
#ifdef LOG_TO_FILE
if (!logFile)
return;
QMutexLocker locker(&mutex); QMutexLocker locker(&mutex);
dflt(type, ctxt, msg); *logFile << LogMsg;
*logFile << QTime::currentTime().toString("HH:mm:ss' '") << msg << '\n';
logFile->flush(); logFile->flush();
}
#endif #endif
}
int opencvErrorHandler(int status, const char* func_name, const char* err_msg, int opencvErrorHandler(int status, const char* func_name, const char* err_msg,
const char* file_name, int line, void*) const char* file_name, int line, void*)
@ -74,6 +97,7 @@ int opencvErrorHandler(int status, const char* func_name, const char* err_msg,
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
qInstallMessageHandler(logMessageHandler); // Enable log as early as possible
QApplication a(argc, argv); QApplication a(argc, argv);
a.setApplicationName("qTox"); a.setApplicationName("qTox");
a.setOrganizationName("Tox"); a.setOrganizationName("Tox");
@ -118,17 +142,15 @@ int main(int argc, char *argv[])
#ifdef LOG_TO_FILE #ifdef LOG_TO_FILE
logFile = new QTextStream; logFile = new QTextStream;
dflt = qInstallMessageHandler(nullptr);
QFile logfile(QDir(Settings::getSettingsDirPath()).filePath("qtox.log")); QFile logfile(QDir(Settings::getSettingsDirPath()).filePath("qtox.log"));
if (logfile.open(QIODevice::Append)) if (logfile.open(QIODevice::Append))
{ {
logFile->setDevice(&logfile); logFile->setDevice(&logfile);
*logFile << QDateTime::currentDateTime().toString("\nyyyy-MM-dd HH:mm:ss' file logger starting\n'"); *logFile << QDateTime::currentDateTime().toString("\nyyyy-MM-dd HH:mm:ss' file logger starting\n'");
qInstallMessageHandler(myMessageHandler);
} }
else else
{ {
fprintf(stderr, "Couldn't open log file!\n"); qWarning() << "Couldn't open log file!\n";
delete logFile; delete logFile;
logFile = nullptr; logFile = nullptr;
} }