From 7c74cdc50bbabd8a73a0bcf3323d3641ac1fe333 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 25 Jul 2018 23:25:17 -0700 Subject: [PATCH] Add enhanced logging support, including log to file for release builds --- app/main.cpp | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/app/main.cpp b/app/main.cpp index 8709aaa5..23344f25 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include // Don't let SDL hook our main function, since Qt is already // doing the same thing. This needs to be before any headers @@ -14,8 +16,113 @@ #include "streaming/session.hpp" #include "settings/streamingpreferences.h" +#if !defined(QT_DEBUG) && defined(Q_OS_WIN32) +// Log to file for release Windows builds +#define USE_CUSTOM_LOGGER +#define LOG_TO_FILE +#elif defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN) +// Use stdout logger on all Linux/BSD builds +#define USE_CUSTOM_LOGGER +#elif !defined(QT_DEBUG) && defined(Q_OS_DARWIN) +// Log to file for release Mac builds +#define USE_CUSTOM_LOGGER +#define LOG_TO_FILE +#else +// For debug Windows and Mac builds, use default logger +#endif + +#ifdef USE_CUSTOM_LOGGER +static QTime s_LoggerTime; +static QTextStream s_LoggerStream(stdout); +static QMutex s_LoggerLock; +#ifdef LOG_TO_FILE +static QFile* s_LoggerFile; +#endif + +void sdlLogToDiskHandler(void*, int category, SDL_LogPriority priority, const char* message) +{ + QString priorityTxt; + + switch (priority) { + case SDL_LOG_PRIORITY_VERBOSE: + priorityTxt = "Verbose"; + break; + case SDL_LOG_PRIORITY_DEBUG: + priorityTxt = "Debug"; + break; + case SDL_LOG_PRIORITY_INFO: + priorityTxt = "Info"; + break; + case SDL_LOG_PRIORITY_WARN: + priorityTxt = "Warn"; + break; + case SDL_LOG_PRIORITY_ERROR: + priorityTxt = "Error"; + break; + case SDL_LOG_PRIORITY_CRITICAL: + priorityTxt = "Critical"; + break; + } + + QTime logTime = QTime::fromMSecsSinceStartOfDay(s_LoggerTime.elapsed()); + QString txt = QString("%1 - SDL %2 (%3): %4").arg(logTime.toString()).arg(priorityTxt).arg(category).arg(message); + + { + QMutexLocker lock(&s_LoggerLock); + s_LoggerStream << txt << endl; + } +} + +void qtLogToDiskHandler(QtMsgType type, const QMessageLogContext&, const QString& msg) +{ + QString typeTxt; + + switch (type) { + case QtDebugMsg: + typeTxt = "Debug"; + break; + case QtInfoMsg: + typeTxt = "Info"; + break; + case QtWarningMsg: + typeTxt = "Warning"; + break; + case QtCriticalMsg: + typeTxt = "Critical"; + break; + case QtFatalMsg: + typeTxt = "Fatal"; + break; + } + + QTime logTime = QTime::fromMSecsSinceStartOfDay(s_LoggerTime.elapsed()); + QString txt = QString("%1 - Qt %2: %3").arg(logTime.toString()).arg(typeTxt).arg(msg); + + { + QMutexLocker lock(&s_LoggerLock); + s_LoggerStream << txt << endl; + } +} + +#endif + int main(int argc, char *argv[]) { +#ifdef USE_CUSTOM_LOGGER +#ifdef LOG_TO_FILE + QDir tempDir(QDir::tempPath()); + s_LoggerFile = new QFile(tempDir.filePath(QString("Moonlight-%1.log").arg(QDateTime::currentSecsSinceEpoch()))); + if (s_LoggerFile->open(QIODevice::WriteOnly)) { + qInfo() << "Redirecting log output to " << s_LoggerFile->fileName(); + s_LoggerStream.setDevice(s_LoggerFile); + } +#endif + + s_LoggerTime.start(); + qInstallMessageHandler(qtLogToDiskHandler); + SDL_LogSetOutputFunction(sdlLogToDiskHandler, nullptr); +#endif + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // This avoids using the default keychain for SSL, which may cause