From f7923ebd75ef7a8a3fed74f4d33697848f8d3736 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Date: Mon, 3 Feb 2020 14:54:52 +0500 Subject: [PATCH] logic: Allow changing the log file path in preferences (issue #156) (#900) --- .gitignore | 1 + src/app/main.cpp | 2 -- src/lib/app/Application.cpp | 8 ++++++ src/lib/settings/ApplicationSettings.cpp | 11 ++++++++ src/lib/settings/ApplicationSettings.h | 3 +++ .../QtProjectWizardContentPreferences.cpp | 26 +++++++++++++++++++ .../QtProjectWizardContentPreferences.h | 1 + src/lib_gui/qt/window/QtMainWindow.cpp | 3 ++- 8 files changed, 52 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 78006936..36a43144 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /build/ +build-* /distr/ /deps/ diff --git a/src/app/main.cpp b/src/app/main.cpp index 3e145428..c1e72a10 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -61,8 +61,6 @@ void setupLogging() logManager->addLogger(consoleLogger); std::shared_ptr fileLogger = std::make_shared(); - fileLogger->setLogDirectory(UserPaths::getLogPath().getAbsolute()); - fileLogger->setFileName(FileLogger::generateDatedFileName(L"log")); fileLogger->setLogLevel(Logger::LOG_ALL); fileLogger->deleteLogFiles(FileLogger::generateDatedFileName(L"log", L"", -30)); logManager->addLogger(fileLogger); diff --git a/src/lib/app/Application.cpp b/src/lib/app/Application.cpp index 71f518c5..24ef6878 100644 --- a/src/lib/app/Application.cpp +++ b/src/lib/app/Application.cpp @@ -30,6 +30,7 @@ #include "tracing.h" #include "utilityString.h" #include "utilityUuid.h" +#include "FileLogger.h" std::shared_ptr Application::s_instance; std::string Application::s_uuid; @@ -112,6 +113,13 @@ void Application::loadSettings() settings->load(UserPaths::getAppSettingsPath()); LogManager::getInstance()->setLoggingEnabled(settings->getLoggingEnabled()); + Logger* logger = LogManager::getInstance()->getLoggerByType("FileLogger"); + if (logger) + { + const auto fileLogger = dynamic_cast(logger); + fileLogger->setLogDirectory(settings->getLogDirectoryPath()); + fileLogger->setFileName(FileLogger::generateDatedFileName(L"log")); + } loadStyle(settings->getColorSchemePath()); } diff --git a/src/lib/settings/ApplicationSettings.cpp b/src/lib/settings/ApplicationSettings.cpp index 1f1dc7d9..2d2badb7 100644 --- a/src/lib/settings/ApplicationSettings.cpp +++ b/src/lib/settings/ApplicationSettings.cpp @@ -337,6 +337,17 @@ void ApplicationSettings::setVerboseIndexerLoggingEnabled(bool value) setValue("application/verbose_indexer_logging_enabled", value); } +FilePath ApplicationSettings::getLogDirectoryPath() const +{ + return FilePath(getValue( + "application/log_directory_path", UserPaths::getLogPath().getAbsolute().wstr())); +} + +void ApplicationSettings::setLogDirectoryPath(const FilePath &path) +{ + setValue("application/log_directory_path", path.wstr()); +} + void ApplicationSettings::setLogFilter(int mask) { setValue("application/log_filter", mask); diff --git a/src/lib/settings/ApplicationSettings.h b/src/lib/settings/ApplicationSettings.h index bbd79311..49da28b7 100644 --- a/src/lib/settings/ApplicationSettings.h +++ b/src/lib/settings/ApplicationSettings.h @@ -82,6 +82,9 @@ public: bool getVerboseIndexerLoggingEnabled() const; void setVerboseIndexerLoggingEnabled(bool loggingEnabled); + FilePath getLogDirectoryPath() const; + void setLogDirectoryPath(const FilePath& path); + int getLogFilter() const; void setLogFilter(int mask); diff --git a/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentPreferences.cpp b/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentPreferences.cpp index 546ed26d..66325fcd 100644 --- a/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentPreferences.cpp +++ b/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentPreferences.cpp @@ -13,6 +13,7 @@ #include "MessageSwitchColorScheme.h" #include "ResourcePaths.h" #include "logging.h" +#include "FileLogger.h" #include "utility.h" #include "utilityApp.h" #include "utilityPathDetection.h" @@ -238,6 +239,16 @@ void QtProjectWizardContentPreferences::populate(QGridLayout* layout, int& row) layout, row); + m_logPath = new QtLocationPicker(this); + m_logPath->setPickDirectory(true); + addLabelAndWidget(QStringLiteral("Log Directory Path"), m_logPath, layout, row); + addHelpButton( + QStringLiteral("Log Directory Path"), + QStringLiteral("

Log file will be saved to this path.

"), + layout, + row); + row++; + addGap(layout, row); // Network @@ -488,6 +499,10 @@ void QtProjectWizardContentPreferences::load() m_loggingEnabled->setChecked(appSettings->getLoggingEnabled()); m_verboseIndexerLoggingEnabled->setChecked(appSettings->getVerboseIndexerLoggingEnabled()); m_verboseIndexerLoggingEnabled->setEnabled(m_loggingEnabled->isChecked()); + if (m_logPath) + { + m_logPath->setText(QString::fromStdWString(appSettings->getLogDirectoryPath().wstr())); + } m_automaticUpdateCheck->setChecked(appSettings->getAutomaticUpdateCheck()); @@ -551,6 +566,17 @@ void QtProjectWizardContentPreferences::save() appSettings->setLoggingEnabled(m_loggingEnabled->isChecked()); appSettings->setVerboseIndexerLoggingEnabled(m_verboseIndexerLoggingEnabled->isChecked()); + if (m_logPath && m_logPath->getText().toStdWString() != appSettings->getLogDirectoryPath().wstr()) + { + appSettings->setLogDirectoryPath(FilePath((m_logPath->getText() + '/').toStdWString())); + Logger* logger = LogManager::getInstance()->getLoggerByType("FileLogger"); + if (logger) + { + const auto fileLogger = dynamic_cast(logger); + fileLogger->setLogDirectory(appSettings->getLogDirectoryPath()); + fileLogger->setFileName(FileLogger::generateDatedFileName(L"log")); + } + } appSettings->setAutomaticUpdateCheck(m_automaticUpdateCheck->isChecked()); diff --git a/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentPreferences.h b/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentPreferences.h index 5a7b21fb..ff821960 100644 --- a/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentPreferences.h +++ b/src/lib_gui/qt/project_wizard/content/QtProjectWizardContentPreferences.h @@ -114,6 +114,7 @@ private: QCheckBox* m_loggingEnabled; QCheckBox* m_verboseIndexerLoggingEnabled; + QtLocationPicker* m_logPath; QCheckBox* m_automaticUpdateCheck; diff --git a/src/lib_gui/qt/window/QtMainWindow.cpp b/src/lib_gui/qt/window/QtMainWindow.cpp index e1530cc0..80827497 100644 --- a/src/lib_gui/qt/window/QtMainWindow.cpp +++ b/src/lib_gui/qt/window/QtMainWindow.cpp @@ -545,7 +545,8 @@ void QtMainWindow::showDataFolder() void QtMainWindow::showLogFolder() { QDesktopServices::openUrl(QUrl( - QString::fromStdWString(L"file:///" + UserPaths::getLogPath().makeCanonical().wstr()), + QString::fromStdWString( + L"file:///" + ApplicationSettings::getInstance()->getLogDirectoryPath().wstr()), QUrl::TolerantMode)); }