Skip to content

Commit

Permalink
feat(verbose): verbose mode will now also log to files for better cra…
Browse files Browse the repository at this point in the history
…sh trace
  • Loading branch information
theophanemayaud committed Oct 20, 2024
1 parent 45fcf7e commit 11aa13f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
72 changes: 72 additions & 0 deletions QtProject/app/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,32 @@ void MainWindow::videoSummary()
void MainWindow::addStatusMessage(const QString &message) const
{
ui->statusBox->append(message);

if(this->_prefs.isVerbose()){
QDir cacheFolder = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
if(!cacheFolder.exists())
QDir().mkpath(cacheFolder.path());
static QFile logFile = QStringLiteral("%1/%2_%3.vsdc.logs.txt")
.arg(cacheFolder.path(),
this->_prefs.appVersion.trimmed(),
QDateTime::currentDateTime().toString("yyyy-MM-dd hh_mm"));
static QTextStream logStream;
if (!logFile.isOpen()) {
// Open the file in Append mode and keep it open
if (!logFile.open(QIODevice::Append | QIODevice::Text)) {
ui->statusBox->append("Failed to open log file");
}
else {
// Initialize the QTextStream using the open file
logStream.setDevice(&logFile);
}
}
else {
logStream << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm")
<< ": " << message << Qt::endl;
}
}

ui->statusBox->repaint();
#ifdef VID_SIMILI_IN_TESTS
qDebug() << message;
Expand Down Expand Up @@ -526,3 +552,49 @@ void MainWindow::on_actionRestoreMoveToTrash_triggered()
}
// ------------------- END: File deletion configuration methods -----------
// ----------------------------------------------------------------------------

void MainWindow::on_actionDelete_log_files_triggered()
{
QDir logsFolder = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
// Check if the directory exists
if (!logsFolder.exists()) {
addStatusMessage(QString("Logs folder does not exist: %1").arg(logsFolder.absolutePath()));
return;
}

// List all .txt files in the cache folder
QStringList txtFiles = logsFolder.entryList(QStringList() << "*.vsdc.logs.txt", QDir::Files);

if(txtFiles.isEmpty())
addStatusMessage(QString("No log files in: %1").arg(logsFolder.absolutePath()));

// Loop through the list of .txt files and delete them
for (const QString &fileName : txtFiles) {
QString filePath = logsFolder.absoluteFilePath(fileName); // Get the full file path
if (QFile::remove(filePath)) {
addStatusMessage(QString("Deleted log file: %1").arg(filePath));
} else {
addStatusMessage(QString("Failed to delete log file: %1").arg(filePath));
}
}
}


void MainWindow::on_actionOpen_logs_folder_triggered()
{
QDir logsFolder = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
if (!logsFolder.exists()) {
addStatusMessage(QString("Logs folder does not exist: %1").arg(logsFolder.absolutePath()));
return;
}

#ifdef Q_OS_WIN
QProcess::startDetached("explorer", {"/select,", logsFolder.absolutePath()});
#elif defined(Q_OS_MACOS)
QProcess::startDetached("open", QStringList() << logsFolder.absolutePath());
#elif defined(Q_OS_X11)
QProcess::startDetached(QStringLiteral("xdg-open \"%1\"").arg(logsFolder.absolutePath()));
#endif

}

2 changes: 2 additions & 0 deletions QtProject/app/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private slots:
void on_radio_UseCacheNo_clicked() {_useCacheOption = Video::NO_CACHE;};
void on_radio_UseCacheYes_clicked() {_useCacheOption = Video::WITH_CACHE;};
void on_radio_UseCacheOnly_clicked() {_useCacheOption = Video::CACHE_ONLY;};
void on_actionDelete_log_files_triggered();
void on_actionOpen_logs_folder_triggered();
};

#endif // MAINWINDOW_H
20 changes: 16 additions & 4 deletions QtProject/app/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@
<item>
<widget class="QCheckBox" name="verboseCheckbox">
<property name="toolTip">
<string>List all processed video names.
Errors will be listed in any case.</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;List all processed video names. &lt;/p&gt;&lt;p&gt;Errors will be listed in any case.&lt;/p&gt;&lt;p&gt;All logs will be kept in log files.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="toolTipDuration">
<number>-1</number>
Expand All @@ -132,7 +131,7 @@ Errors will be listed in any case.</string>
<locale language="English" country="UnitedStates"/>
</property>
<property name="text">
<string>Verbose</string>
<string>Verbose logs</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -529,7 +528,7 @@ Errors will be listed in any case.</string>
<x>0</x>
<y>0</y>
<width>859</width>
<height>24</height>
<height>37</height>
</rect>
</property>
<widget class="QMenu" name="menuGeneral">
Expand All @@ -550,6 +549,9 @@ Errors will be listed in any case.</string>
<addaction name="actionEnable_direct_deletion_instead_of_trash"/>
<addaction name="actionRestoreMoveToTrash"/>
<addaction name="separator"/>
<addaction name="actionOpen_logs_folder"/>
<addaction name="actionDelete_log_files"/>
<addaction name="separator"/>
<addaction name="actionRestore_all_settings"/>
</widget>
<widget class="QMenu" name="menuInfo">
Expand Down Expand Up @@ -634,6 +636,16 @@ Errors will be listed in any case.</string>
<string>Restore all settings</string>
</property>
</action>
<action name="actionOpen_logs_folder">
<property name="text">
<string>Open logs folder</string>
</property>
</action>
<action name="actionDelete_log_files">
<property name="text">
<string>Delete log files</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
Expand Down
2 changes: 1 addition & 1 deletion QtProject/app/prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Prefs
QString browseFoldersLastPath() const {return QSettings(APP_NAME, APP_NAME).value("browse_folders_last_path").toString();}
void browseFoldersLastPath(const QString dirPath) {QSettings(APP_NAME, APP_NAME).setValue("browse_folders_last_path", dirPath);}

bool isVerbose() {return QSettings(APP_NAME, APP_NAME).value("verbose_logging").toBool();}
bool isVerbose() const {return QSettings(APP_NAME, APP_NAME).value("verbose_logging").toBool();}
void setVerbose(const bool verbose) {QSettings(APP_NAME, APP_NAME).setValue("verbose_logging", verbose);}

void resetSettings() {QSettings(APP_NAME, APP_NAME).clear();}
Expand Down

0 comments on commit 11aa13f

Please sign in to comment.