Skip to content

Commit

Permalink
Add primitive logging mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
psyomn committed Apr 24, 2021
1 parent 32f639e commit 650c887
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 1 deletion.
2 changes: 2 additions & 0 deletions source/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set( srcs
clipboard.cpp
command.cpp
documentmanager.cpp
log.cpp
paths.cpp
powertabeditor.cpp
recentfiles.cpp
Expand All @@ -22,6 +23,7 @@ set( headers
clipboard.h
command.h
documentmanager.h
log.h
paths.h
powertabeditor.h
recentfiles.h
Expand Down
88 changes: 88 additions & 0 deletions source/app/log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2021 Simon Symeonidis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "log.h"
#include "paths.h"

#include <cstdio>
#include <ctime>
#include <fstream>
#include <sstream>
#include <iostream>

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);

void Output(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
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};
const auto now = std::time(0);
const auto *tmp = localtime(&now);
strftime(output, sizeof(output), "%Y/%m/%dT%H:%M:%S", tmp);
timestamp = std::string(output);
}

auto logFn = [&](std::string label,
std::ostream& ofile,
std::ostream& oterminal)-> void
{
// std::format when? :(
ofile
<< timestamp << " "
<< label << ": "
<< localMsg.constData() << " "
<< file << " "
<< context.line << " "
<< function << std::endl;

oterminal
<< timestamp << " "
<< label << ": "
<< localMsg.constData() << " "
<< file << " "
<< context.line << " "
<< function << std::endl;
};

switch (type) {
case QtDebugMsg:
logFn("debug", logFile, std::cerr);
break;
case QtInfoMsg:
logFn("debug", logFile, std::cerr);
break;
case QtWarningMsg:
logFn("warning", logFile, std::cerr);
break;
case QtCriticalMsg:
logFn("critical", logFile, std::cerr);
break;
case QtFatalMsg:
logFn("fatal", logFile, std::cerr);
break;
}
}
}
26 changes: 26 additions & 0 deletions source/app/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2021 Simon Symeonidis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef APP_LOG_H
#define APP_LOG_H

#include <QString>
#include <QtGlobal>

namespace Log {
void Output(QtMsgType type, const QMessageLogContext &context, const QString &msg);
}
#endif
5 changes: 5 additions & 0 deletions source/app/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ path getConfigDir()
#endif
}

path getLogPath()
{
return getConfigDir() / "log.txt";
}

path getUserDataDir()
{
return fromQString(
Expand Down
3 changes: 3 additions & 0 deletions source/app/paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ namespace Paths {
/// Return a path to a directory where config files should be written to.
path getConfigDir();

/// Return a path to the log file
path getLogPath();

/// Return a path to a directory where persistent application data should
/// be written to.
path getUserDataDir();
Expand Down
5 changes: 4 additions & 1 deletion source/build/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <app/appinfo.h>
#include <app/log.h>
#include <app/paths.h>
#include <app/powertabeditor.h>
#include <app/settings.h>
Expand Down Expand Up @@ -130,6 +131,8 @@ class Application : public QApplication

int main(int argc, char *argv[])
{
qInstallMessageHandler(Log::Output);

// Register handlers for unhandled exceptions and segmentation faults.
std::set_terminate(terminateHandler);
std::signal(SIGSEGV, signalHandler);
Expand Down

0 comments on commit 650c887

Please sign in to comment.