Skip to content

Commit

Permalink
Add log contents in the info dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
psyomn committed Apr 24, 2021
1 parent 650c887 commit 16f68c1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
24 changes: 20 additions & 4 deletions source/app/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,40 @@
#include <fstream>
#include <sstream>
#include <iostream>
#include <mutex>

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);
Expand All @@ -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 << ": "
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions source/app/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <QtGlobal>

namespace Log {
std::string logContents();
void Output(QtMsgType type, const QMessageLogContext &context, const QString &msg);
}
#endif
2 changes: 2 additions & 0 deletions source/build/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 11 additions & 2 deletions source/dialogs/infodialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "ui_infodialog.h"

#include <app/appinfo.h>
#include <app/log.h>

#include <QClipboard>

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 16f68c1

Please sign in to comment.