Skip to content

Commit

Permalink
feat: Add console logging (this aids in solving bug reports, no loggi…
Browse files Browse the repository at this point in the history
…ng data is saved to disk or is shared)
  • Loading branch information
SebiAi committed Jan 25, 2024
1 parent 8afe3b3 commit 68ada66
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 9 deletions.
17 changes: 15 additions & 2 deletions src/composition_manager.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "composition_manager.h"

// Logging
Q_LOGGING_CATEGORY(compositionManager, "CompositionManager")
Q_LOGGING_CATEGORY(compositionManagerVerbose, "CompositionManager.Verbose")

const QList<QString> CompositionManager::phoneModelStrings = {
"Phone1",
"Phone2"
Expand Down Expand Up @@ -139,9 +143,15 @@ void CompositionManager::loadComposition(const QString &filepathAudio, const QSt
QFileInfo audioFileInfo = QFileInfo(filepathAudio);
QFileInfo lightDataFileInfo = QFileInfo(filepathLightData);
if (!audioFileInfo.exists() || !audioFileInfo.isFile())
throw std::invalid_argument(std::string("The audio file '").append(filepathAudio.toStdString()).append("' could not be found!"));
{
qCWarning(compositionManager) << "The file" << filepathAudio << "could not be found";
throw std::invalid_argument(std::string("The file '").append(filepathAudio.toStdString()).append("' could not be found!"));
}
if (!lightDataFileInfo.exists() || !lightDataFileInfo.isFile())
{
qCWarning(compositionManager) << "The file" << lightDataFileInfo << "could not be found";
throw std::invalid_argument(std::string("The light data file '").append(filepathLightData.toStdString()).append("' could not be found!"));
}

// Parse light data
parseLightData(filepathLightData);
Expand All @@ -165,6 +175,8 @@ void CompositionManager::skipToPercentage(const qreal& percentage)

void CompositionManager::playerInit(const QString &filepathAudio)
{
qCInfo(compositionManagerVerbose) << "Setting up the player";

// connect(this->player, SIGNAL(metaDataChanged()), this, SLOT(player_onMetaDataChanged()), Qt::UniqueConnection);
connect(this->player, SIGNAL(positionChanged(qint64)), this, SLOT(player_onPositionChanged(qint64)), Qt::UniqueConnection);
connect(this->player, SIGNAL(playingChanged(bool)), this, SLOT(player_onPlayingChanged(bool)), Qt::UniqueConnection);
Expand Down Expand Up @@ -244,6 +256,8 @@ void CompositionManager::parseLightData(const QString &filepathLightData)
return;
}

qCInfo(compositionManagerVerbose) << "Detected" << currentGlyphMode << "mode";

firstLine = false;
}

Expand Down Expand Up @@ -309,7 +323,6 @@ void CompositionManager::player_onPlayingChanged(bool playing)

void CompositionManager::player_onMediaStatusChanged(QMediaPlayer::MediaStatus status)
{
qDebug().nospace() << "State: " << status;
switch (status)
{
case QMediaPlayer::StalledMedia:
Expand Down
5 changes: 5 additions & 0 deletions src/composition_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include <QList>
#include "helper.h"

// Logging
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(compositionManager)
Q_DECLARE_LOGGING_CATEGORY(compositionManagerVerbose)

// TODO: [Next] Use QTimer to create a signal at 15ms and call GlyphWidget::update() with that
// TODO: To fix the low "refresh" issue: Make per Glyph glyphdata + spawn a thread for each glyph which waits and then changes the glyph/start a timer for each glyph

Expand Down
7 changes: 6 additions & 1 deletion src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ Config::Config(const QString &fileName, QObject *parent, bool clearConfig)

// Get config version
int configVersion = getInt(Config::Setting::ConfigVersion_Int);
qCInfo(configVerbose) << "Supported config version:" << getDefaultValue(Config::Setting::ConfigVersion_Int).toInt();
qCInfo(configVerbose) << "Read config version:" << configVersion;

// Check if the config version is below minimum
if (configVersion < minConfigVersion)
{
// We don't know what config this is so we set it to the minConfigVersion
// This should cause an upgrade - see below
qCInfo(configVerbose) << "The config version is below minimum, setting to minimum";
configVersion = minConfigVersion;
settings->setValue(getKey(Config::Setting::ConfigVersion_Int), configVersion);
}
Expand All @@ -69,6 +72,7 @@ Config::Config(const QString &fileName, QObject *parent, bool clearConfig)
// Upgrade the config if the config version is lower than the current default value
if (configVersion < getDefaultValue(Config::Setting::ConfigVersion_Int).toInt())
{
qCInfo(config) << "The config version is lower than the currently supported version => upgrading config";
upgradeConfig(configVersion, getDefaultValue(Config::Setting::ConfigVersion_Int).toInt());
}
}
Expand Down Expand Up @@ -152,6 +156,7 @@ void Config::backupConfig() const
fileDifferentiator++;
}

qCInfo(config) << "Creating backup of the config to:" << backupFileName;
if (!QFile::copy(this->settings->fileName(), backupFileName))
{
qCFatal(config) << "Backup of config failed! Copy from" << this->settings->fileName() << "to" << backupFileName;
Expand All @@ -175,5 +180,5 @@ void Config::upgradeConfig(int oldVersion, int newVersion)
// }

if (oldVersion < newVersion)
qCWarning(config) << "[Development Error] Config upgrade step(s) from" << oldVersion << "to" << newVersion << "is/are missing!";
qCFatal(config) << "[Development Error] Config upgrade step(s) from" << oldVersion << "to" << newVersion << "is/are missing!";
}
4 changes: 4 additions & 0 deletions src/glyph.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "glyph.h"

// Logging
Q_LOGGING_CATEGORY(glyph, "Glyph")
Q_LOGGING_CATEGORY(glyphVerbose, "Glyph.Verbose")

const QRegularExpression Glyph::svgFillColorRegex = QRegularExpression("(?<=fill=\")[^\"]+(?=\")", QRegularExpression::PatternOption::MultilineOption);

Glyph::Glyph(const QString &filename, const Glyph::Reference& reference, const QPointF& referenceOffset, const QString& id, QObject *parent)
Expand Down
5 changes: 5 additions & 0 deletions src/glyph.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include <QFile>
#include <QRegularExpression>

// Logging
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(glyph)
Q_DECLARE_LOGGING_CATEGORY(glyphVerbose)

class Glyph : private QSvgRenderer
{
Q_OBJECT
Expand Down
4 changes: 4 additions & 0 deletions src/glyph_widget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "glyph_widget.h"

// Logging
Q_LOGGING_CATEGORY(glyphWidget, "GlyphWidget")
Q_LOGGING_CATEGORY(glyphWidgetVerbose, "GlyphWidget.Verbose")

GlyphWidget::GlyphWidget(QWidget *parent)
: QWidget{parent}
{
Expand Down
5 changes: 5 additions & 0 deletions src/glyph_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include "glyph.h"
#include "composition_manager.h"

// Logging
#include <QLoggingCategory>
Q_DECLARE_LOGGING_CATEGORY(glyphWidget)
Q_DECLARE_LOGGING_CATEGORY(glyphWidgetVerbose)

class GlyphWidget : public QWidget
{
Q_OBJECT
Expand Down
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QApplication>
#include <QCommandLineParser>
#include <QCommandLineParser>
#include <QSysInfo>

#ifdef Q_OS_WIN
#include <Windows.h>
Expand Down Expand Up @@ -54,9 +55,16 @@ int main(int argc, char *argv[])
}
else QLoggingCategory::setFilterRules("*.Verbose=false");

qCInfo(mainFunctionVerbose) << "#### Software Information #####";
qCInfo(mainFunctionVerbose) << "Current software version:" << APPLICATION_VERSION;
qCInfo(mainFunctionVerbose) << "Current software git hash:" << APPLICATION_GIT_COMMIT_HASH;

qCInfo(mainFunctionVerbose) << "#### System Information #####";
qCInfo(mainFunctionVerbose) << "Product name and version:" << QSysInfo::prettyProductName();
qCInfo(mainFunctionVerbose) << "Kernel type and version:" << QSysInfo::kernelType() << QSysInfo::kernelVersion();

qCInfo(mainFunctionVerbose) << "#### Logging Start #####";

// Set up MainWindow
MainWindow w;
w.show();
Expand Down
49 changes: 44 additions & 5 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Logging
Q_LOGGING_CATEGORY(mainWindow, "MainWindow")
Q_LOGGING_CATEGORY(mainWindowVerbose, "MainWindow.Verbose")
// TODO: Actually implement proper loggin in MainWindow

// TODO: Set the application icon: https://doc.qt.io/qt-6/appicon.html

Expand Down Expand Up @@ -137,7 +136,7 @@ void MainWindow::showEvent(QShowEvent *event)
}
catch (Config::ConfigVersionTooHighError e)
{
qCInfo(mainWindowVerbose) << "ConfigVersionTooHighError occurred";
qCCritical(mainWindow) << "ConfigVersionTooHighError occurred";
QMessageBox mb = QMessageBox(QMessageBox::Icon::Critical,"Config Error",
QString("The configuration file in '").append(e.fileName)
.append("' is intended to be used with a higher version of this software.\n\nThis usually happens when you either downgrade your software or manually modify the config.\n\nDo you want to clear the config (Recommended) or exit the application?")
Expand All @@ -150,11 +149,11 @@ void MainWindow::showEvent(QShowEvent *event)
switch (answer)
{
case QDialog::DialogCode::Accepted: // Clear
qCInfo(mainWindowVerbose) << "ConfigVersionTooHighError - User decided to exit";
qCWarning(mainWindow) << "ConfigVersionTooHighError - User decided to clear";
clearConfig = true;
break;
case QDialog::DialogCode::Rejected: // Exit
qCInfo(mainWindowVerbose) << "ConfigVersionTooHighError - User decided to exit";
qCWarning(mainWindow) << "ConfigVersionTooHighError - User decided to exit";
this->earlyQuit = true;
return;
break;
Expand Down Expand Up @@ -201,19 +200,26 @@ void MainWindow::showEvent(QShowEvent *event)

void MainWindow::processOpenCompositionDialogAccepted(const OpenCompositionDialog::CompositionOpenModeResult& openMode, const QString& str0, const QString& str1)
{
qCInfo(mainWindowVerbose).nospace() << "OpenCompositionDialog accepted with openMode: " << openMode
<< ", str0: " << str0
<< ", str1: " << str1;

// Load the composition
try
{
// Load the composition with the right function
switch (openMode)
{
case OpenCompositionDialog::CompositionOpenModeResult::AUDIO_ONLY:
qCInfo(mainWindow) << "Loading composition (Audio Only)";
throw std::logic_error(std::string("[Development Error] Audio only not implemented yet: '").append(__FUNCTION__).append("'!"));
break;
case OpenCompositionDialog::CompositionOpenModeResult::AUDIO_LIGHT_DATA:
qCInfo(mainWindow) << "Loading composition (Audio + Light data)";
this->glyphWidget->compositionManager->loadComposition(str0, str1);
break;
case OpenCompositionDialog::CompositionOpenModeResult::AUDIO_AUDACITY_LABELS:
qCInfo(mainWindow) << "Loading composition (Audio + Audacity Labels)";
throw std::logic_error(std::string("[Development Error] Audio only not implemented yet: '").append(__FUNCTION__).append("'!"));
break;
case OpenCompositionDialog::CompositionOpenModeResult::None:
Expand All @@ -227,6 +233,8 @@ void MainWindow::processOpenCompositionDialogAccepted(const OpenCompositionDialo
}
catch (const std::invalid_argument& e)
{
qCWarning(mainWindow) << "Critical Error:" << e.what();

// Display the error
QMessageBox* msg = new QMessageBox(QMessageBox::Critical, "Critical Error", e.what(), QMessageBox::StandardButton::Ok, this->window());
connect(msg, &QMessageBox::finished, msg, [=](){
Expand All @@ -240,6 +248,8 @@ void MainWindow::processOpenCompositionDialogAccepted(const OpenCompositionDialo
}
catch (const CompositionManager::InvalidLightDataContentException& e)
{
qCWarning(mainWindow) << "InvalidLightDataContentException:" << e.what();

QMessageBox* msg = new QMessageBox(QMessageBox::Critical, "Critical Error", e.what(), QMessageBox::StandardButton::Ok, this->window());
connect(msg, &QDialog::finished, msg, &QWidget::deleteLater); // Delete the dialog after it finished
msg->open();
Expand All @@ -250,7 +260,11 @@ void MainWindow::processOpenCompositionDialogAccepted(const OpenCompositionDialo
// TODO: Maybe move this into the GlyphWidget class?
// Set the proper visual depending on the loaded light data
if (this->glyphWidget->compositionManager->getGlyphMode() == CompositionManager::GlyphMode::Phone2 && this->glyphWidget->getVisualMode() == CompositionManager::PhoneModel::Phone1)
{
qCInfo(mainWindowVerbose) << "Force changing visual to"
<< CompositionManager::getPhoneModelString(CompositionManager::PhoneModel::Phone2);
this->glyphWidget->setVisual(CompositionManager::PhoneModel::Phone2);
}

// Play the composition
this->glyphWidget->compositionManager->player->setPosition(0);
Expand Down Expand Up @@ -279,12 +293,18 @@ void MainWindow::button_onClicked(bool checked)
switch (glyphWidget->getVisualMode())
{
case CompositionManager::PhoneModel::Phone1:
qCInfo(mainWindowVerbose) << "Changing visual to"
<< CompositionManager::getPhoneModelString(CompositionManager::PhoneModel::Phone2);
glyphWidget->setVisual(CompositionManager::PhoneModel::Phone2);
break;
case CompositionManager::PhoneModel::Phone2:
// Only switch to Phone (1) visual if the currently loaded light data is compatible with it.
if (glyphWidget->compositionManager->getGlyphMode() != CompositionManager::GlyphMode::Phone2)
{
qCInfo(mainWindowVerbose) << "Changing visual to"
<< CompositionManager::getPhoneModelString(CompositionManager::PhoneModel::Phone1);
glyphWidget->setVisual(CompositionManager::PhoneModel::Phone1);
}
break;
case CompositionManager::PhoneModel::None:
// This should never happen
Expand All @@ -298,6 +318,8 @@ void MainWindow::button_onClicked(bool checked)

void MainWindow::openFileAction_onTriggered(bool checked)
{
qCInfo(mainWindowVerbose) << "Opening the OpenCompositionDialog";

// Save the playing state
this->playerWasPlayingBefore = this->glyphWidget->compositionManager->player->isPlaying();

Expand All @@ -310,6 +332,8 @@ void MainWindow::openFileAction_onTriggered(bool checked)

void MainWindow::checkForUpdateAction_onTriggered(bool checked)
{
qCInfo(mainWindowVerbose) << "Manual update check triggered";

// Set the currentTime
this->config->setValue(Config::Setting::UpdateChecker_LastAutoUpdateCheck_QDateTime, QDateTime::currentDateTimeUtc());

Expand All @@ -319,6 +343,8 @@ void MainWindow::checkForUpdateAction_onTriggered(bool checked)

void MainWindow::aboutAction_onTriggered(bool checked)
{
qCInfo(mainWindowVerbose) << "Opening the about dialog";

// Display an about dialog
QMessageBox* mb = new QMessageBox(QMessageBox::Icon::NoIcon, "About GlyphVisualizer",
QString("# GlyphVisualizer\n**A Glyph composition player written with the Qt6 framework in C++ that plays Glyph compositions from Nothing Phones.**\n***\nVersion: *").append(APPLICATION_VERSION)
Expand All @@ -332,6 +358,8 @@ void MainWindow::aboutAction_onTriggered(bool checked)

void MainWindow::openCompositionDialog_onFinished(int result)
{
qCInfo(mainWindowVerbose) << "OpenCompositionDialog finished with result code:" << result;

// Respond to result code
switch (result)
{
Expand All @@ -341,7 +369,10 @@ void MainWindow::openCompositionDialog_onFinished(int result)
case QDialog::DialogCode::Rejected: // == 1
// Resume playing the composition if it was playing before
if (this->playerWasPlayingBefore)
{
qCInfo(mainWindowVerbose) << "Resuming playback of composition because it was playing before opening the OpenCompositionDialog";
this->glyphWidget->compositionManager->player->play();
}
break;
default:
// This should never happen
Expand All @@ -352,6 +383,8 @@ void MainWindow::openCompositionDialog_onFinished(int result)

void MainWindow::glyphWidget_onDurationChanged(qint64 duration)
{
qCInfo(mainWindowVerbose) << "Duration changed:" << duration << "ms";

// Set the lengthTimeLabel
this->lengthTimeLabel->setText(millisecondsToTimeRepresentation(duration));

Expand All @@ -365,7 +398,7 @@ void MainWindow::seekBar_onValueChanged(int value)
// Update currentTimeLabel
this->currentTimeLabel->setText(millisecondsToTimeRepresentation(value));

// Set player position if we have a position missmatch (the user moved the slider)
// Set player position if we have a position mismatch (the user moved the slider)
if (value != this->glyphWidget->compositionManager->player->position())
this->glyphWidget->compositionManager->player->setPosition(this->seekBar->value());
}
Expand All @@ -385,6 +418,8 @@ void MainWindow::seekBar_onSliderReleased()

void MainWindow::glyphWidget_onPlaybackStateChanged(QMediaPlayer::PlaybackState newState)
{
qCInfo(mainWindowVerbose) << "Playback state changed to:" << newState;

// Set the icon depending on the playback state
switch (newState)
{
Expand All @@ -406,10 +441,12 @@ void MainWindow::pausePlayButton_onClicked(bool checked)
switch (this->glyphWidget->compositionManager->player->playbackState())
{
case QMediaPlayer::PlaybackState::PlayingState:
qCInfo(mainWindowVerbose) << "Pausing playback";
this->glyphWidget->compositionManager->player->pause();
break;
case QMediaPlayer::PlaybackState::StoppedState:
case QMediaPlayer::PlaybackState::PausedState:
qCInfo(mainWindowVerbose) << "Resuming/Starting playback";
this->glyphWidget->compositionManager->player->play();
break;
}
Expand Down Expand Up @@ -460,6 +497,8 @@ void MainWindow::updateChecker_onNoUpdateAvailable()

MainWindow::~MainWindow()
{
qCInfo(mainWindow) << "Bye, bye!";

delete this->ui;

//delete this->seekBarStyle; // TODO: This delete causes invalid reads??? See valgrind memcheck.
Expand Down
Loading

0 comments on commit 68ada66

Please sign in to comment.