diff --git a/debug.cpp b/debug.cpp index 666a6b203..ed43cea42 100644 --- a/debug.cpp +++ b/debug.cpp @@ -4,36 +4,63 @@ #include #include #include +#include #include "dialogs/debugdialog.h" QString debug_info; +QMutex debug_mutex; +QFile debug_file; +QTextStream debug_stream; + +void open_debug_file() { + QDir debug_dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); + debug_dir.mkpath("."); + if (debug_dir.exists()) { + debug_file.setFileName(debug_dir.path() + "/debug_log"); + if (debug_file.open(QFile::WriteOnly)) { + debug_stream.setDevice(&debug_file); + } else { + qWarning() << "Couldn't open debug log file, debug log will not be saved"; + } + } +} + +void close_debug_file() { + if (debug_file.isOpen()) debug_file.close(); +} void debug_message_handler(QtMsgType type, const QMessageLogContext &context, const QString &msg) { + debug_mutex.lock(); QByteArray localMsg = msg.toLocal8Bit(); switch (type) { case QtDebugMsg: fprintf(stderr, "[DEBUG] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + if (debug_file.isOpen()) debug_stream << QString("[DEBUG] %1 (%2:%3, %4)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function); debug_info.prepend(QString("[DEBUG] %1 (%2:%3, %4)
").arg(localMsg.constData(), context.file, QString::number(context.line), context.function)); fflush(stderr); break; case QtInfoMsg: fprintf(stderr, "[INFO] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + if (debug_file.isOpen()) debug_stream << QString("[INFO] %1 (%2:%3, %4)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function); debug_info.prepend(QString("[INFO] %1 (%2:%3, %4)
").arg(localMsg.constData(), context.file, QString::number(context.line), context.function)); fflush(stderr); break; case QtWarningMsg: fprintf(stderr, "[WARNING] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + if (debug_file.isOpen()) debug_stream << QString("[WARNING] %1 (%2:%3, %4)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function); debug_info.prepend(QString("[WARNING] %1 (%2:%3, %4)
").arg(localMsg.constData(), context.file, QString::number(context.line), context.function)); fflush(stderr); break; case QtCriticalMsg: fprintf(stderr, "[ERROR] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + if (debug_file.isOpen()) debug_stream << QString("[ERROR] %1 (%2:%3, %4)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function); debug_info.prepend(QString("[ERROR] %1 (%2:%3, %4)
").arg(localMsg.constData(), context.file, QString::number(context.line), context.function)); fflush(stderr); break; case QtFatalMsg: fprintf(stderr, "[FATAL] %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); + if (debug_file.isOpen()) debug_stream << QString("[FATAL] %1 (%2:%3, %4)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function); debug_info.prepend(QString("[FATAL] %1 (%2:%3, %4)
").arg(localMsg.constData(), context.file, QString::number(context.line), context.function)); fflush(stderr); abort(); @@ -41,6 +68,7 @@ void debug_message_handler(QtMsgType type, const QMessageLogContext &context, co if (debug_dialog->isVisible()) { QMetaObject::invokeMethod(debug_dialog, "update_log", Qt::QueuedConnection); } + debug_mutex.unlock(); } const QString &get_debug_str() { diff --git a/debug.h b/debug.h index bf741056d..0e75d54c4 100644 --- a/debug.h +++ b/debug.h @@ -5,6 +5,8 @@ void debug_message_handler(QtMsgType type, const QMessageLogContext &context, const QString &msg); const QString& get_debug_str(); +void open_debug_file(); +void close_debug_file(); #define dout qDebug() diff --git a/mainwindow.cpp b/mainwindow.cpp index 5260036a7..b43471c79 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -94,9 +94,11 @@ void MainWindow::setup_layout(bool reset) { MainWindow::MainWindow(QWidget *parent, const QString &an) : QMainWindow(parent), - appName(an), - enable_launch_with_project(false) + enable_launch_with_project(false), + appName(an) { + open_debug_file(); + debug_dialog = new DebugDialog(this); mainWindow = this; @@ -227,6 +229,7 @@ MainWindow::MainWindow(QWidget *parent, const QString &an) : MainWindow::~MainWindow() { free_panels(); + close_debug_file(); } void MainWindow::launch_with_project(const QString& s) {