From 16f68c16b469aab10ef468a41e3f219afbcea6d1 Mon Sep 17 00:00:00 2001 From: Simon Symeonidis Date: Sat, 24 Apr 2021 14:44:42 -0400 Subject: [PATCH] Add log contents in the info dialog --- source/app/log.cpp | 24 ++++++++++++++++++++---- source/app/log.h | 1 + source/build/main.cpp | 2 ++ source/dialogs/infodialog.cpp | 13 +++++++++++-- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/source/app/log.cpp b/source/app/log.cpp index 424f57de..5b915cc0 100644 --- a/source/app/log.cpp +++ b/source/app/log.cpp @@ -22,23 +22,40 @@ #include #include #include +#include namespace Log { // destructor should close the file when main exits // note: might not capture critical errors, but this should be a good // enought start... std::ofstream logFile(Paths::getLogPath().string(), - std::ofstream::out); + std::ofstream::app); + +// since Log::Output will capture anything that is coming in from +// qDebug and friends, we might intercept messages asynchronously and +// may result in data races around logFile +std::mutex logFileMutex; + +std::string logContents() { + std::ifstream file(Paths::getLogPath().string()); + std::cout << std::string(Paths::getLogPath().string()) << std::endl; + std::stringstream ss; + ss << file.rdbuf(); + return ss.str(); +} void Output(QtMsgType type, const QMessageLogContext &context, const QString &msg) { + // Since this intercepts + std::lock_guard lock(logFileMutex); + QByteArray localMsg = msg.toLocal8Bit(); const char *file = context.file ? context.file : ""; const char *function = context.function ? context.function : ""; std::string timestamp; { - char output[128] = {0}; + char output[64] = {0}; const auto now = std::time(0); const auto *tmp = localtime(&now); strftime(output, sizeof(output), "%Y/%m/%dT%H:%M:%S", tmp); @@ -49,7 +66,6 @@ void Output(QtMsgType type, const QMessageLogContext &context, const QString &ms std::ostream& ofile, std::ostream& oterminal)-> void { - // std::format when? :( ofile << timestamp << " " << label << ": " @@ -72,7 +88,7 @@ void Output(QtMsgType type, const QMessageLogContext &context, const QString &ms logFn("debug", logFile, std::cerr); break; case QtInfoMsg: - logFn("debug", logFile, std::cerr); + logFn("info", logFile, std::cout); break; case QtWarningMsg: logFn("warning", logFile, std::cerr); diff --git a/source/app/log.h b/source/app/log.h index 3eb6b897..f67e83a4 100644 --- a/source/app/log.h +++ b/source/app/log.h @@ -21,6 +21,7 @@ #include namespace Log { +std::string logContents(); void Output(QtMsgType type, const QMessageLogContext &context, const QString &msg); } #endif diff --git a/source/build/main.cpp b/source/build/main.cpp index 79e97768..83efa9f0 100644 --- a/source/build/main.cpp +++ b/source/build/main.cpp @@ -131,6 +131,8 @@ class Application : public QApplication int main(int argc, char *argv[]) { + // This sets up our custom logging mechanism, that can print and + // save into a text file for future debugging qInstallMessageHandler(Log::Output); // Register handlers for unhandled exceptions and segmentation faults. diff --git a/source/dialogs/infodialog.cpp b/source/dialogs/infodialog.cpp index 9583932f..9e674ad4 100644 --- a/source/dialogs/infodialog.cpp +++ b/source/dialogs/infodialog.cpp @@ -19,6 +19,7 @@ #include "ui_infodialog.h" #include +#include #include @@ -47,9 +48,17 @@ void InfoDialog::setInfo() QString(QObject::tr("You can grab development binaries here:\n" " https://github.com/powertab/powertabeditor/actions")); - const auto message = QString("%1\n\n%2").arg( + const auto contents = QString::fromStdString(Log::logContents()); + + const auto message = QString( + "%1\n\n" + "%2\n\n" + "===============================\n" + "Logs:\n%3" + ).arg( qname, - developmentBinaryLocation + developmentBinaryLocation, + contents ); ui->appInfo->setText(message);