[FancyZones] Place log files in subfolders with the version number. (#10549)

This commit is contained in:
Seraphima Zykova 2021-04-07 14:43:35 +01:00 committed by GitHub
parent 675ae91c29
commit 55a851e5f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 2 deletions

View file

@ -287,6 +287,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "utils", "utils", "{B39DC643
src\common\utils\elevation.h = src\common\utils\elevation.h
src\common\utils\exec.h = src\common\utils\exec.h
src\common\utils\json.h = src\common\utils\json.h
src\common\utils\logger_helper.h = src\common\utils\logger_helper.h
src\common\utils\os-detect.h = src\common\utils\os-detect.h
src\common\utils\process_path.h = src\common\utils\process_path.h
src\common\utils\resources.h = src\common\utils\resources.h

View file

@ -7,13 +7,15 @@ struct LogSettings
inline const static std::wstring defaultLogLevel = L"trace";
inline const static std::wstring logLevelOption = L"logLevel";
inline const static std::string runnerLoggerName = "runner";
inline const static std::wstring logPath = L"Logs\\";
inline const static std::wstring runnerLogPath = L"RunnerLogs\\runner-log.txt";
inline const static std::string actionRunnerLoggerName = "action-runner";
inline const static std::wstring actionRunnerLogPath = L"RunnerLogs\\action-runner-log.txt";
inline const static std::string launcherLoggerName = "launcher";
inline const static std::wstring launcherLogPath = L"LogsModuleInterface\\launcher-log.txt";
inline const static std::string fancyZonesLoggerName = "fancyzones";
inline const static std::wstring fancyZonesLogPath = L"FancyZonesLogs\\fancyzones-log.txt";
inline const static std::wstring fancyZonesLogPath = L"fancyzones-log.txt";
inline const static std::wstring fancyZonesOldLogPath = L"FancyZonesLogs\\"; // needed to clean up old logs
inline const static std::string shortcutGuideLoggerName = "shortcut-guide";
inline const static std::wstring shortcutGuideLogPath = L"ShortcutGuideLogs\\shortcut-guide-log.txt";
inline const static std::string keyboardManagerLoggerName = "keyboard-manager";

View file

@ -0,0 +1,55 @@
#pragma once
#include <filesystem>
#include <common/version/version.h>
namespace LoggerHelpers
{
inline std::filesystem::path get_log_folder_path(std::wstring_view appPath)
{
std::filesystem::path logFolderPath(appPath);
logFolderPath.append(LogSettings::logPath);
logFolderPath.append(get_product_version());
return logFolderPath;
}
inline bool delete_old_log_folder(const std::filesystem::path& logFolderPath)
{
try
{
std::filesystem::remove_all(logFolderPath);
return true;
}
catch (std::filesystem::filesystem_error& e)
{
Logger::error("Failed to delete old log folder: {}", e.what());
}
return false;
}
inline bool delete_other_versions_log_folders(std::wstring_view appPath, const std::filesystem::path& currentVersionLogFolder)
{
bool result = true;
std::filesystem::path logFolderPath(appPath);
logFolderPath.append(LogSettings::logPath);
for (const auto& dir : std::filesystem::directory_iterator(logFolderPath))
{
if (dir != currentVersionLogFolder)
{
try
{
std::filesystem::remove_all(dir);
}
catch (std::filesystem::filesystem_error& e)
{
Logger::error("Failed to delete previous version log folder: {}", e.what());
result = false;
}
}
}
return result;
}
}

View file

@ -14,6 +14,7 @@
#include <lib/FancyZonesWinHookEventIDs.h>
#include <lib/FancyZonesData.cpp>
#include <common/logger/logger.h>
#include <common/utils/logger_helper.h>
#include <common/utils/resources.h>
#include <common/utils/winapi_error.h>
#include <common/utils/window.h>
@ -156,9 +157,19 @@ public:
{
app_name = GET_RESOURCE_STRING(IDS_FANCYZONES);
app_key = NonLocalizable::FancyZonesStr;
std::filesystem::path logFilePath(PTSettingsHelper::get_module_save_folder_location(app_key));
const auto appFolder = PTSettingsHelper::get_module_save_folder_location(app_key);
const std::filesystem::path logFolder = LoggerHelpers::get_log_folder_path(appFolder);
std::filesystem::path logFilePath(logFolder);
logFilePath.append(LogSettings::fancyZonesLogPath);
Logger::init(LogSettings::fancyZonesLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location());
std::filesystem::path oldLogFolder(appFolder);
oldLogFolder.append(LogSettings::fancyZonesOldLogPath);
LoggerHelpers::delete_old_log_folder(oldLogFolder);
LoggerHelpers::delete_other_versions_log_folders(appFolder, logFolder);
m_settings = MakeFancyZonesSettings(reinterpret_cast<HINSTANCE>(&__ImageBase), FancyZonesModule::get_name(), FancyZonesModule::get_key());
FancyZonesDataInstance().LoadFancyZonesData();
s_instance = this;