Skip to content

Commit

Permalink
[Setup] Add logging for registry changes + add logger for powerpreview
Browse files Browse the repository at this point in the history
- cleanup logger project + remove SettingsAPI dependency
  • Loading branch information
yuyoyuppe committed Nov 11, 2021
1 parent d036740 commit bef119b
Show file tree
Hide file tree
Showing 18 changed files with 187 additions and 40 deletions.
3 changes: 2 additions & 1 deletion .github/actions/spell-check/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ Farbraum
FARPROC
fdw
feimage
FFAA
ffcd
FFDDDDDD
fff
Expand All @@ -655,7 +656,6 @@ finalizer
findfast
findstr
FIXEDFILEINFO
FFAA
FLASHZONES
FLASHZONESONQUICKSWITCH
flt
Expand Down Expand Up @@ -2439,6 +2439,7 @@ workaround
Workflow
workspaces
wostream
wostringstream
wox
wparam
wpf
Expand Down
12 changes: 12 additions & 0 deletions installer/PowerToysSetup.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "PowerToysSetup", "PowerToys
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PowerToysSetupCustomActions", "PowerToysSetupCustomActions\PowerToysSetupCustomActions.vcxproj", "{32F3882B-F2D6-4586-B5ED-11E39E522BD3}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "spdlog", "..\src\logging\logging.vcxproj", "{7E1E3F13-2BD6-3F75-A6A7-873A2B55C60F}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "logger", "..\src\common\logger\logger.vcxproj", "{D9B8FC84-322A-4F9F-BBB9-20915C47DDFD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Expand All @@ -21,6 +25,14 @@ Global
{32F3882B-F2D6-4586-B5ED-11E39E522BD3}.Debug|x64.Build.0 = Debug|x64
{32F3882B-F2D6-4586-B5ED-11E39E522BD3}.Release|x64.ActiveCfg = Release|x64
{32F3882B-F2D6-4586-B5ED-11E39E522BD3}.Release|x64.Build.0 = Release|x64
{7E1E3F13-2BD6-3F75-A6A7-873A2B55C60F}.Debug|x64.ActiveCfg = Debug|x64
{7E1E3F13-2BD6-3F75-A6A7-873A2B55C60F}.Debug|x64.Build.0 = Debug|x64
{7E1E3F13-2BD6-3F75-A6A7-873A2B55C60F}.Release|x64.ActiveCfg = Release|x64
{7E1E3F13-2BD6-3F75-A6A7-873A2B55C60F}.Release|x64.Build.0 = Release|x64
{D9B8FC84-322A-4F9F-BBB9-20915C47DDFD}.Debug|x64.ActiveCfg = Debug|x64
{D9B8FC84-322A-4F9F-BBB9-20915C47DDFD}.Debug|x64.Build.0 = Debug|x64
{D9B8FC84-322A-4F9F-BBB9-20915C47DDFD}.Release|x64.ActiveCfg = Release|x64
{D9B8FC84-322A-4F9F-BBB9-20915C47DDFD}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
34 changes: 31 additions & 3 deletions installer/PowerToysSetupCustomActions/CustomAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "resource.h"
#include <ProjectTelemetry.h>

#include <spdlog/sinks/base_sink.h>

#include "../../src/common/logger/logger.h"
#include "../../src/common/utils/MsiUtils.h"
#include "../../src/common/utils/modulesRegistry.h"
#include "../../src/common/updating/installer.h"
Expand All @@ -26,6 +29,24 @@ const DWORD USERNAME_LEN = UNLEN + 1; // User Name + '\0'
static const wchar_t* POWERTOYS_EXE_COMPONENT = L"{A2C66D91-3485-4D00-B04D-91844E6B345B}";
static const wchar_t* POWERTOYS_UPGRADE_CODE = L"{42B84BF7-5FBF-473B-9C8B-049DC16F7708}";

struct WcaSink : spdlog::sinks::base_sink<std::mutex>
{
virtual void sink_it_(const spdlog::details::log_msg& msg) override
{
WcaLog(LOGMSG_STANDARD, msg.payload.data());
}
virtual void flush_() override
{
// we don't need to flush wca log manually
}
};

void initSystemLogger()
{
static std::once_flag initLoggerFlag;
std::call_once(initLoggerFlag, []() { Logger::init(std::vector<spdlog::sink_ptr>{ std::make_shared<WcaSink>() }); });
}

HRESULT getInstallFolder(MSIHANDLE hInstall, std::wstring& installationDir)
{
DWORD len = 0;
Expand All @@ -34,7 +55,7 @@ HRESULT getInstallFolder(MSIHANDLE hInstall, std::wstring& installationDir)
len += 1;
installationDir.resize(len);
HRESULT hr = MsiGetPropertyW(hInstall, L"CustomActionData", installationDir.data(), &len);
if(installationDir.length())
if (installationDir.length())
{
installationDir.resize(installationDir.length() - 1);
}
Expand All @@ -44,31 +65,38 @@ HRESULT getInstallFolder(MSIHANDLE hInstall, std::wstring& installationDir)
}
UINT __stdcall ApplyModulesRegistryChangeSetsCA(MSIHANDLE hInstall)
{
initSystemLogger();
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
std::wstring installationFolder;
bool failedToApply = false;

hr = WcaInitialize(hInstall, "ApplyModulesRegistryChangeSets");
ExitOnFailure(hr, "Failed to initialize");
hr = getInstallFolder(hInstall, installationFolder);
ExitOnFailure(hr, "Failed to get installFolder.");

for (const auto& changeSet : getAllModulesChangeSets(installationFolder, false))
{
if (!changeSet.apply())
{
WcaLog(LOGMSG_STANDARD, "Couldn't apply registry changeSet");
failedToApply = true;
}
}

ExitOnFailure(hr, "Failed to extract msix");

if (!failedToApply)
{
WcaLog(LOGMSG_STANDARD, "All registry changeSets applied successfully");
}
LExit:
er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE;
return WcaFinalize(er);
}

UINT __stdcall UnApplyModulesRegistryChangeSetsCA(MSIHANDLE hInstall)
{
initSystemLogger();
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
std::wstring installationFolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<PlatformToolset>v142</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<Import Project="..\..\deps\spdlog.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
Expand Down Expand Up @@ -122,6 +123,11 @@
<ItemGroup>
<ResourceCompile Include="Resource.rc" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\common\logger\logger.vcxproj">
<Project>{d9b8fc84-322a-4f9f-bbb9-20915c47ddfd}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
1 change: 1 addition & 0 deletions installer/PowerToysSetupCustomActions/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <psapi.h>
#include <vector>
#include <array>
#include <mutex>

#include <winrt/Windows.Foundation.h>
#include <winrt/Windows.Foundation.Collections.h>
Expand Down
45 changes: 31 additions & 14 deletions src/common/logger/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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");
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions src/common/logger/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Logger
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>
Expand Down
3 changes: 0 additions & 3 deletions src/common/logger/logger.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
2 changes: 1 addition & 1 deletion src/common/logger/logger_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/common/logger/logger_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit bef119b

Please sign in to comment.