Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging #331

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Apt Dependencies
run: sudo apt update && sudo apt install ninja-build qtbase5-dev qttools5-dev libboost-dev libboost-date-time-dev libboost-iostreams-dev nlohmann-json3-dev libasound2-dev librtmidi-dev libminizip-dev doctest-dev
run: sudo apt update && sudo apt install ninja-build qtbase5-dev qttools5-dev libboost-dev libboost-date-time-dev libboost-iostreams-dev nlohmann-json3-dev libasound2-dev librtmidi-dev libminizip-dev doctest-dev libfmt-dev
- name: Install Other Dependencies
run: vcpkg install pugixml
- name: Create Build Directory
Expand All @@ -44,7 +44,7 @@ jobs:

- name: Install Dependencies
# CMake 3.17 is already installed
run: brew install boost doctest minizip ninja nlohmann-json pugixml qt5 pugixml rtmidi
run: brew install boost doctest minizip ninja nlohmann-json pugixml qt5 pugixml rtmidi fmt
- name: Generate Project
run: cmake -S ${GITHUB_WORKSPACE} -B ${{runner.workspace}}/build -G Ninja -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=/usr/local/opt/qt5/lib/cmake
- name: Build
Expand Down Expand Up @@ -98,7 +98,7 @@ jobs:
run: echo "::set-output name=VERSION_ID::$(git describe --tags --long --always)"

- name: Install Dependencies
run: vcpkg install --triplet ${{ matrix.arch }}-windows boost-algorithm boost-date-time boost-endian boost-functional boost-iostreams boost-range boost-rational boost-signals2 boost-stacktrace doctest minizip nlohmann-json pugixml
run: vcpkg install --triplet ${{ matrix.arch }}-windows boost-algorithm boost-date-time boost-endian boost-functional boost-iostreams boost-range boost-rational boost-signals2 boost-stacktrace doctest minizip nlohmann-json pugixml fmt

# Building Qt via vcpkg would take a while ...
- name: Install Qt
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ Power Tab Editor 2.0 - A powerful cross platform guitar tablature viewer and edi
* [pugixml](https://pugixml.org/)
* [minizip](https://github.com/madler/zlib)
* [doctest](https://github.com/onqtam/doctest)
* [fmtlib](https://github.com/fmtlib/fmt)
* (Linux only) - ALSA library (e.g. `libasound2-dev`)
* (Linux only) - MIDI sequencer (e.g. `timidity-daemon`)
* A compiler with C++17 support.
* A compiler with C++20 support.

### Building:
#### Windows:
Expand Down
1 change: 1 addition & 0 deletions cmake/PTE_ThirdParty.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ include ( third_party/nlohmann_json )
include ( third_party/pugixml )
include ( third_party/Qt )
include ( third_party/rtmidi )
include ( third_party/fmt )
1 change: 1 addition & 0 deletions cmake/third_party/fmt.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
find_package ( fmt 8.0.0 REQUIRED )
1 change: 1 addition & 0 deletions source/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ set( headers
command.h
documentmanager.h
paths.h
log.h
powertabeditor.h
recentfiles.h
scorearea.h
Expand Down
37 changes: 37 additions & 0 deletions source/app/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2024 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 <util/log.h>

#include <QString>
#include <fmt/core.h>
#include <fmt/format.h>
#include <string>

/* Adapted from: https://fmt.dev/latest/api.html#udt */
template <>
struct fmt::formatter<QString> : fmt::formatter<std::string>
{
auto format(const QString& qstr, fmt::format_context& ctx) const {
return fmt::formatter<std::string>::format(qstr.toStdString(), ctx);
}
};

#endif
5 changes: 5 additions & 0 deletions source/app/paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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
16 changes: 7 additions & 9 deletions source/app/powertabeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
#include <app/command.h>
#include <app/documentmanager.h>
#include <app/paths.h>
#include <app/log.h>
#include <app/recentfiles.h>
#include <app/scorearea.h>
#include <app/settings.h>
Expand Down Expand Up @@ -260,14 +261,14 @@ void PowerTabEditor::openFile(QString filename)
int validationResult = myDocumentManager->findDocument(path);
if (validationResult > -1)
{
qDebug() << "File: " << filename << " is already open";
Log::d("file: {} is already open", filename);
myTabWidget->setCurrentIndex(validationResult);
return;
}

auto start = std::chrono::high_resolution_clock::now();

qDebug() << "Opening file: " << filename;
Log::d("opening file: {}", filename);

QFileInfo fileInfo(filename);
std::optional<FileFormat> format = myFileFormatManager->findFormat(
Expand All @@ -285,9 +286,8 @@ void PowerTabEditor::openFile(QString filename)
Document &doc = myDocumentManager->addDocument(*mySettingsManager);
myFileFormatManager->importFile(doc.getScore(), path, *format);
auto end = std::chrono::high_resolution_clock::now();
qDebug() << "File loaded in"
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start) .count()
<< "ms";

Log::d("file loaded in: {}ms", std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count());

doc.setFilename(path);
setPreviousDirectory(filename);
Expand Down Expand Up @@ -3616,7 +3616,7 @@ void PowerTabEditor::setPreviousDirectory(const QString &fileName)
void PowerTabEditor::setupNewTab()
{
auto start = std::chrono::high_resolution_clock::now();
qDebug() << "Tab creation started ...";
Log::d("tab creation started...");

Q_ASSERT(myDocumentManager->hasOpenDocuments());
Document &doc = myDocumentManager->getCurrentDocument();
Expand Down Expand Up @@ -3788,9 +3788,7 @@ void PowerTabEditor::setupNewTab()
scorearea->setFocus();

auto end = std::chrono::high_resolution_clock::now();
qDebug() << "Tab opened in"
<< std::chrono::duration_cast<std::chrono::milliseconds>(
end - start).count() << "ms";
Log::d("tab opened in: {}ms", std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count());
}

namespace
Expand Down
16 changes: 10 additions & 6 deletions source/app/scorearea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* 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 "scorearea.h"

#include <app/documentmanager.h>
Expand All @@ -32,6 +32,7 @@
#include <QPrinter>
#include <QScrollBar>
#include <score/score.h>
#include <util/log.h>

void ScoreArea::Scene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
Expand Down Expand Up @@ -114,7 +115,7 @@ void ScoreArea::renderDocument(const Document &document)
#endif
std::vector<std::future<void>> tasks;
const int work_size = myRenderedSystems.size() / num_threads;
qDebug() << "Using" << num_threads << "worker thread(s)";
Log::d("using {} worker thread(s)", num_threads);

for (int i = 0; i < num_threads; ++i)
{
Expand Down Expand Up @@ -162,10 +163,13 @@ void ScoreArea::renderDocument(const Document &document)
myScene.setSceneRect(myScene.itemsBoundingRect());

auto end = std::chrono::high_resolution_clock::now();
qDebug() << "Score rendered in"
<< std::chrono::duration_cast<std::chrono::milliseconds>(
end - start).count() << "ms";
qDebug() << "Rendered " << myScene.items().size() << "items";

namespace sc = std::chrono;
const auto time_elapsed = static_cast<long long int>
(sc::duration_cast<sc::milliseconds>(end - start).count());

Log::d("score rendered in {} ms", time_elapsed);
Log::d("rendered {} items", myScene.items().size());
}

void ScoreArea::redrawSystem(int index)
Expand Down
4 changes: 3 additions & 1 deletion source/app/settingsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "settingsmanager.h"

#include <util/log.h>

#include <fstream>
#include <iostream>

Expand Down Expand Up @@ -47,7 +49,7 @@ void SettingsManager::load(const std::filesystem::path &dir)
}
catch (const std::exception &e)
{
std::cerr << "Error loading " << path << ": " << e.what() << std::endl;
Log::e("error loading {}: {}", path.generic_string(), e.what());
}
#endif
}
Expand Down
7 changes: 4 additions & 3 deletions source/app/tuningdictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <score/serialization.h>
#include <stdexcept>
#include <unordered_set>
#include <util/log.h>

static const char *theTuningDictFilename = "tunings.json";

Expand Down Expand Up @@ -70,11 +71,11 @@ TuningDictionary::load()

if (entries.empty())
{
std::cerr << "Could not locate tuning dictionary." << std::endl;
std::cerr << "Candidate paths:" << std::endl;
Log::e("could not locate tuning dictionary");
Log::e("candidate paths: ");

for (std::filesystem::path dir : Paths::getDataDirs())
std::cerr << (dir / theTuningDictFilename) << std::endl;
Log::e(" - {}", (dir / theTuningDictFilename).generic_string());
}

return entries;
Expand Down
5 changes: 3 additions & 2 deletions source/audio/midioutputdevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* 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 "midioutputdevice.h"

#include <RtMidi.h>
#include <exception>
#include <score/dynamic.h>
#include <score/generalmidi.h>
#include <cassert>
#include <util/log.h>

#ifdef __APPLE__
#include "midisoftwaresynth.h"
Expand All @@ -38,7 +39,7 @@ MidiOutputDevice::MidiOutputDevice() : myMidiOut(nullptr)
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
Log::e("could not start midisoftwaresynth: {}", e.what());
};
#endif

Expand Down
29 changes: 18 additions & 11 deletions 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 @@ -60,7 +61,7 @@ static void displayError(const std::string &reason)
// If there is no QApplication instance, something went seriously wrong
// during startup - just dump the error to the console.
if (!QApplication::instance())
std::cerr << message << std::endl;
Log::e("{}", message);
else
{
CrashDialog dialog(QString::fromStdString(message),
Expand Down Expand Up @@ -135,23 +136,25 @@ loadTranslations(QApplication &app, QTranslator &qt_translator,
QTranslator &ptb_translator)
{
QLocale locale;
qDebug() << "Finding translations for locale" << locale
<< "with UI languages" << locale.uiLanguages();

Log::d("finding translations for locale:");
for (const auto& loc : locale.uiLanguages())
Log::d(" locale: {}", loc);

for (auto &&path : Paths::getTranslationDirs())
{
QString dir = Paths::toQString(path);
qDebug() << " - Checking" << dir;
Log::d(" - checking: {}", path.generic_string());

if (ptb_translator.isEmpty() &&
ptb_translator.load(locale, QStringLiteral("powertabeditor"),
QStringLiteral("_"), dir))
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
qDebug() << "Loaded application translations from"
<< ptb_translator.filePath();
Log::d("loaded application translations from: {}",
ptb_translator.filePath());
#else
qDebug() << "Loaded application translations";
Log::d("loaded application translations");
#endif
app.installTranslator(&ptb_translator);
}
Expand All @@ -161,10 +164,10 @@ loadTranslations(QApplication &app, QTranslator &qt_translator,
QStringLiteral("_"), dir))
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
qDebug() << "Loaded Qt base translations from"
<< qt_translator.filePath();
Log::d("loaded qt base translations from {}",
qt_translator.filePath());
#else
qDebug() << "Loaded Qt base translations";
Log::d("loaded Qt base translations");
#endif
app.installTranslator(&qt_translator);
}
Expand All @@ -173,6 +176,10 @@ loadTranslations(QApplication &app, QTranslator &qt_translator,

int main(int argc, char *argv[])
{
Log::init(Log::Level::Debug, Paths::getLogPath());

Log::d("started powertab editor ({})", AppInfo::APPLICATION_VERSION);

// Register handlers for unhandled exceptions and segmentation faults.
std::set_terminate(terminateHandler);
std::signal(SIGSEGV, signalHandler);
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,9 @@
#include "ui_infodialog.h"

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

#include <util/log.h>

#include <QClipboard>

Expand Down Expand Up @@ -47,9 +50,15 @@ void InfoDialog::setInfo()
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 message = QString(
"%1\n\n"
"%2\n\n"
"===============================\n"
"Logs:\n%3"
).arg(
qname,
developmentBinaryLocation
developmentBinaryLocation,
QString::fromStdString(Log::all())
);

ui->appInfo->setText(message);
Expand Down
Loading
Loading