mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 17:56:44 +02:00
[Setup] Add logging for registry changes + add logger for powerpreview
- cleanup logger project + remove SettingsAPI dependency
This commit is contained in:
committed by
Andrey Nekrasov
parent
d036740c8b
commit
bef119b03b
@@ -3,7 +3,7 @@
|
||||
#include "pch.h"
|
||||
#include "framework.h"
|
||||
#include "logger.h"
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <spdlog/sinks/daily_file_sink.h>
|
||||
#include <spdlog/sinks/msvc_sink.h>
|
||||
#include <spdlog/sinks/null_sink.h>
|
||||
@@ -16,26 +16,32 @@ using spdlog::sinks::daily_file_sink_mt;
|
||||
using spdlog::sinks::msvc_sink_mt;
|
||||
using std::make_shared;
|
||||
|
||||
std::map<std::wstring, level_enum> logLevelMapping = {
|
||||
{ L"trace", level_enum::trace },
|
||||
{ L"debug", level_enum::debug },
|
||||
{ L"info", level_enum::info },
|
||||
{ L"warn", level_enum::warn },
|
||||
{ L"err", level_enum::err },
|
||||
{ L"critical", level_enum::critical },
|
||||
{ L"off", level_enum::off },
|
||||
};
|
||||
namespace
|
||||
{
|
||||
const std::unordered_map<std::wstring, level_enum> logLevelMapping = {
|
||||
{ L"trace", level_enum::trace },
|
||||
{ L"debug", level_enum::debug },
|
||||
{ L"info", level_enum::info },
|
||||
{ L"warn", level_enum::warn },
|
||||
{ L"err", level_enum::err },
|
||||
{ L"critical", level_enum::critical },
|
||||
{ L"off", level_enum::off },
|
||||
};
|
||||
}
|
||||
|
||||
level_enum getLogLevel(std::wstring_view logSettingsPath)
|
||||
{
|
||||
auto logLevel = get_log_settings(logSettingsPath).logLevel;
|
||||
level_enum result = logLevelMapping[LogSettings::defaultLogLevel];
|
||||
if (logLevelMapping.find(logLevel) != logLevelMapping.end())
|
||||
if (auto it = logLevelMapping.find(logLevel); it != logLevelMapping.end())
|
||||
{
|
||||
result = logLevelMapping[logLevel];
|
||||
return it->second;
|
||||
}
|
||||
|
||||
return result;
|
||||
if (auto it = logLevelMapping.find(LogSettings::defaultLogLevel); it != logLevelMapping.end())
|
||||
{
|
||||
return it->second;
|
||||
}
|
||||
return level_enum::trace;
|
||||
}
|
||||
|
||||
std::shared_ptr<spdlog::logger> Logger::logger = spdlog::null_logger_mt("null");
|
||||
@@ -89,3 +95,14 @@ void Logger::init(std::string loggerName, std::wstring logFilePath, std::wstring
|
||||
spdlog::flush_every(std::chrono::seconds(3));
|
||||
logger->info("{} logger is initialized", loggerName);
|
||||
}
|
||||
|
||||
void Logger::init(std::vector<spdlog::sink_ptr> sinks)
|
||||
{
|
||||
auto logger = std::make_shared<spdlog::logger>("", begin(sinks), end(sinks));
|
||||
if (!logger)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Logger::logger = logger;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ public:
|
||||
Logger() = delete;
|
||||
|
||||
static void init(std::string loggerName, std::wstring logFilePath, std::wstring_view logSettingsPath);
|
||||
static void init(std::vector<spdlog::sink_ptr> sinks);
|
||||
|
||||
// log message should not be localized
|
||||
template<typename FormatString, typename... Args>
|
||||
|
||||
@@ -52,9 +52,6 @@
|
||||
<ProjectReference Include="..\..\logging\logging.vcxproj">
|
||||
<Project>{7e1e3f13-2bd6-3f75-a6a7-873a2b55c60f}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\SettingsAPI\SetttingsAPI.vcxproj">
|
||||
<Project>{6955446d-23f7-4023-9bb3-8657f904af99}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
|
||||
@@ -9,7 +9,7 @@ using namespace winrt::Windows::Data::Json;
|
||||
|
||||
LogSettings::LogSettings()
|
||||
{
|
||||
this->logLevel = LogSettings::defaultLogLevel;
|
||||
logLevel = defaultLogLevel;
|
||||
}
|
||||
|
||||
std::optional<JsonObject> from_file(std::wstring_view file_name)
|
||||
|
||||
@@ -13,6 +13,8 @@ struct LogSettings
|
||||
inline const static std::wstring actionRunnerLogPath = L"RunnerLogs\\action-runner-log.txt";
|
||||
inline const static std::string updateLoggerName = "update";
|
||||
inline const static std::wstring updateLogPath = L"UpdateLogs\\update-log.txt";
|
||||
inline const static std::string fileExplorerLoggerName = "FileExplorer";
|
||||
inline const static std::wstring fileExplorerLogPath = L"Logs\\file-explorer-log.txt";
|
||||
inline const static std::string launcherLoggerName = "launcher";
|
||||
inline const static std::wstring launcherLogPath = L"LogsModuleInterface\\launcher-log.txt";
|
||||
inline const static std::wstring awakeLogPath = L"Logs\\awake-log.txt";
|
||||
|
||||
@@ -9,6 +9,8 @@
|
||||
#include <optional>
|
||||
#include <cassert>
|
||||
|
||||
#include "../logger/logger.h"
|
||||
#include "../utils/winapi_error.h"
|
||||
#include "../version/version.h"
|
||||
|
||||
namespace registry
|
||||
@@ -35,7 +37,28 @@ namespace registry
|
||||
|
||||
template<class... Ts>
|
||||
overloaded(Ts...) -> overloaded<Ts...>;
|
||||
|
||||
inline const wchar_t* getScopeName(HKEY scope)
|
||||
{
|
||||
if (scope == HKEY_LOCAL_MACHINE)
|
||||
{
|
||||
return L"HKLM";
|
||||
}
|
||||
else if (scope == HKEY_CURRENT_USER)
|
||||
{
|
||||
return L"HKCU";
|
||||
}
|
||||
else if (scope == HKEY_CLASSES_ROOT)
|
||||
{
|
||||
return L"HKCR";
|
||||
}
|
||||
else
|
||||
{
|
||||
return L"HK??";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ValueChange
|
||||
{
|
||||
using value_t = std::variant<DWORD, std::wstring>;
|
||||
@@ -51,11 +74,28 @@ namespace registry
|
||||
{
|
||||
}
|
||||
|
||||
std::wstring toString() const
|
||||
{
|
||||
using namespace detail;
|
||||
|
||||
std::wstring value_str;
|
||||
std::visit(overloaded{ [&](DWORD value) {
|
||||
std::wostringstream oss;
|
||||
oss << value;
|
||||
value_str = oss.str();
|
||||
},
|
||||
[&](const std::wstring& value) { value_str = value; } },
|
||||
value);
|
||||
|
||||
return fmt::format(L"{}\\{}\\{}:{}", detail::getScopeName(scope), path, name ? *name : L"Default", value_str);
|
||||
}
|
||||
|
||||
bool isApplied() const
|
||||
{
|
||||
HKEY key{};
|
||||
if (RegOpenKeyExW(scope, path.c_str(), 0, KEY_READ, &key) != ERROR_SUCCESS)
|
||||
if (auto res = RegOpenKeyExW(scope, path.c_str(), 0, KEY_READ, &key); res != ERROR_SUCCESS)
|
||||
{
|
||||
Logger::info(L"isApplied of {}: RegOpenKeyExW failed: {}", toString(), get_last_error_or_default(res));
|
||||
return false;
|
||||
}
|
||||
detail::on_exit closeKey{ [key] { RegCloseKey(key); } };
|
||||
@@ -65,13 +105,15 @@ namespace registry
|
||||
DWORD retrievedType{};
|
||||
wchar_t buffer[VALUE_BUFFER_SIZE];
|
||||
DWORD valueSize = sizeof(buffer);
|
||||
if (RegQueryValueExW(key,
|
||||
name.has_value() ? name->c_str() : nullptr,
|
||||
0,
|
||||
&retrievedType,
|
||||
reinterpret_cast<LPBYTE>(&buffer),
|
||||
&valueSize) != ERROR_SUCCESS)
|
||||
if (auto res = RegQueryValueExW(key,
|
||||
name.has_value() ? name->c_str() : nullptr,
|
||||
0,
|
||||
&retrievedType,
|
||||
reinterpret_cast<LPBYTE>(&buffer),
|
||||
&valueSize);
|
||||
res != ERROR_SUCCESS)
|
||||
{
|
||||
Logger::info(L"isApplied of {}: RegQueryValueExW failed: {}", toString(), get_last_error_or_default(res));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -94,9 +136,10 @@ namespace registry
|
||||
{
|
||||
HKEY key{};
|
||||
|
||||
if (RegCreateKeyExW(scope, path.c_str(), 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &key, nullptr) !=
|
||||
ERROR_SUCCESS)
|
||||
if (auto res = RegCreateKeyExW(scope, path.c_str(), 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &key, nullptr); res !=
|
||||
ERROR_SUCCESS)
|
||||
{
|
||||
Logger::error(L"apply of {}: RegCreateKeyExW failed: {}", toString(), get_last_error_or_default(res));
|
||||
return false;
|
||||
}
|
||||
detail::on_exit closeKey{ [key] { RegCloseKey(key); } };
|
||||
@@ -106,26 +149,35 @@ namespace registry
|
||||
DWORD valueType;
|
||||
|
||||
valueToBuffer(value, buffer, valueSize, valueType);
|
||||
return RegSetValueExW(key,
|
||||
name.has_value() ? name->c_str() : nullptr,
|
||||
0,
|
||||
valueType,
|
||||
reinterpret_cast<BYTE*>(buffer),
|
||||
valueSize) == ERROR_SUCCESS;
|
||||
if (auto res = RegSetValueExW(key,
|
||||
name.has_value() ? name->c_str() : nullptr,
|
||||
0,
|
||||
valueType,
|
||||
reinterpret_cast<BYTE*>(buffer),
|
||||
valueSize);
|
||||
res != ERROR_SUCCESS)
|
||||
{
|
||||
Logger::error(L"apply of {}: RegSetValueExW failed: {}", toString(), get_last_error_or_default(res));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool unApply() const
|
||||
{
|
||||
HKEY key{};
|
||||
if (RegOpenKeyExW(scope, path.c_str(), 0, KEY_ALL_ACCESS, &key) != ERROR_SUCCESS)
|
||||
if (auto res = RegOpenKeyExW(scope, path.c_str(), 0, KEY_ALL_ACCESS, &key); res != ERROR_SUCCESS)
|
||||
{
|
||||
Logger::error(L"unApply of {}: RegOpenKeyExW failed: {}", toString(), get_last_error_or_default(res));
|
||||
return false;
|
||||
}
|
||||
detail::on_exit closeKey{ [key] { RegCloseKey(key); } };
|
||||
|
||||
// delete the value itself
|
||||
if (RegDeleteKeyValueW(scope, path.c_str(), name.has_value() ? name->c_str() : nullptr) != ERROR_SUCCESS)
|
||||
if (auto res = RegDeleteKeyValueW(scope, path.c_str(), name.has_value() ? name->c_str() : nullptr); res != ERROR_SUCCESS)
|
||||
{
|
||||
Logger::error(L"unApply of {}: RegDeleteKeyValueW failed: {}", toString(), get_last_error_or_default(res));
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -157,6 +157,9 @@
|
||||
<ProjectReference Include="..\..\..\common\logger\logger.vcxproj">
|
||||
<Project>{d9b8fc84-322a-4f9f-bbb9-20915c47ddfd}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\SettingsAPI\SetttingsAPI.vcxproj">
|
||||
<Project>{6955446d-23f7-4023-9bb3-8657f904af99}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\Themes\Themes.vcxproj">
|
||||
<Project>{98537082-0fdb-40de-abd8-0dc5a4269bab}</Project>
|
||||
</ProjectReference>
|
||||
|
||||
@@ -80,6 +80,9 @@
|
||||
<ProjectReference Include="..\..\..\common\logger\logger.vcxproj">
|
||||
<Project>{d9b8fc84-322a-4f9f-bbb9-20915c47ddfd}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\SettingsAPI\SetttingsAPI.vcxproj">
|
||||
<Project>{6955446d-23f7-4023-9bb3-8657f904af99}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Resources.resx" />
|
||||
|
||||
@@ -166,6 +166,9 @@
|
||||
<ProjectReference Include="..\..\..\common\logger\logger.vcxproj">
|
||||
<Project>{d9b8fc84-322a-4f9f-bbb9-20915c47ddfd}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\SettingsAPI\SetttingsAPI.vcxproj">
|
||||
<Project>{6955446d-23f7-4023-9bb3-8657f904af99}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\FancyZonesLib\FancyZonesLib.vcxproj">
|
||||
<Project>{f9c68edf-ac74-4b77-9af1-005d9c9f6a99}</Project>
|
||||
</ProjectReference>
|
||||
|
||||
@@ -56,6 +56,9 @@
|
||||
<ProjectReference Include="..\..\..\common\logger\logger.vcxproj">
|
||||
<Project>{d9b8fc84-322a-4f9f-bbb9-20915c47ddfd}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\SettingsAPI\SetttingsAPI.vcxproj">
|
||||
<Project>{6955446d-23f7-4023-9bb3-8657f904af99}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\FancyZonesLib\FancyZonesLib.vcxproj">
|
||||
<Project>{f9c68edf-ac74-4b77-9af1-005d9c9f6a99}</Project>
|
||||
</ProjectReference>
|
||||
|
||||
@@ -62,6 +62,9 @@
|
||||
<ProjectReference Include="..\..\..\..\common\Display\Display.vcxproj">
|
||||
<Project>{caba8dfb-823b-4bf2-93ac-3f31984150d9}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\..\common\SettingsAPI\SetttingsAPI.vcxproj">
|
||||
<Project>{6955446d-23f7-4023-9bb3-8657f904af99}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\FancyZonesLib\FancyZonesLib.vcxproj">
|
||||
<Project>{f9c68edf-ac74-4b77-9af1-005d9c9f6a99}</Project>
|
||||
</ProjectReference>
|
||||
|
||||
@@ -71,6 +71,9 @@
|
||||
<ProjectReference Include="..\..\..\common\logger\logger.vcxproj">
|
||||
<Project>{d9b8fc84-322a-4f9f-bbb9-20915c47ddfd}</Project>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\..\..\common\SettingsAPI\SetttingsAPI.vcxproj">
|
||||
<Project>{6955446d-23f7-4023-9bb3-8657f904af99}</Project>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
|
||||
@@ -11,12 +11,20 @@
|
||||
#include <common/utils/os-detect.h>
|
||||
#include <common/utils/process_path.h>
|
||||
|
||||
#include <SettingsAPI/settings_helpers.h>
|
||||
|
||||
// Constructor
|
||||
PowerPreviewModule::PowerPreviewModule() :
|
||||
m_moduleName(GET_RESOURCE_STRING(IDS_MODULE_NAME)),
|
||||
app_key(powerpreviewConstants::ModuleKey)
|
||||
{
|
||||
const std::wstring installationDir = get_module_folderpath();
|
||||
|
||||
std::filesystem::path logFilePath(PTSettingsHelper::get_module_save_folder_location(this->app_key));
|
||||
logFilePath.append(LogSettings::fileExplorerLogPath);
|
||||
Logger::init(LogSettings::fileExplorerLoggerName, logFilePath.wstring(), PTSettingsHelper::get_log_settings_file_location());
|
||||
|
||||
Logger::info("Initializing PowerPreviewModule");
|
||||
const bool installPerUser = false;
|
||||
m_fileExplorerModules.push_back({ .settingName = L"svg-previewer-toggle-setting",
|
||||
.settingDescription = GET_RESOURCE_STRING(IDS_PREVPANE_SVG_SETTINGS_DESCRIPTION),
|
||||
@@ -133,7 +141,10 @@ void PowerPreviewModule::disable()
|
||||
{
|
||||
for (auto& fileExplorerModule : m_fileExplorerModules)
|
||||
{
|
||||
fileExplorerModule.registryChanges.unApply();
|
||||
if (!fileExplorerModule.registryChanges.unApply())
|
||||
{
|
||||
Logger::error(L"Couldn't disable file explorer module {} during module disable() call", fileExplorerModule.settingName);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@@ -207,6 +218,7 @@ void PowerPreviewModule::apply_settings(const PowerToysSettings::PowerToyValues&
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger::error(L"Couldn't {} file explorer module {} during apply_settings", *toggle ? L"enable " : L"disable", fileExplorerModule.settingName);
|
||||
Trace::PowerPreviewSettingsUpdateFailed(fileExplorerModule.settingName.c_str(), !*toggle, *toggle, true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user