Better logging support, replaces LOG_HR_MSG with DebugLog (#4271)

This PR replaces and refactors ways to do logging in WinAppSDK codebase.
Currently, LOG_HR_MSG from wil library is used for logging purposes too even though its usage is for error-reporting.

In current scenario, we want to print logging information to Debug Console (using OutputDebugString). This PR introduces a new logging function to do the same : DebugLog in Logging.h.
With this change, any place which is using LOG_HR_MSG for just diagnostic logging, I will replace it with this function call and also remove any macros associated with it.

Another change is refactoring of Mddbootstrap.cpp's FindDDLMViaAppExtension() to be cleaner.

At 2-3 places, I have left custom winappruntime provided error code intact because it is throwing them. A user code could be setup to catch them and hence, to save on backward compatibility, I am keeping them intact. 

---------

Co-authored-by: Pratik Anand <praanan@microsoft.com>
This commit is contained in:
Pratik Anand 2024-04-16 09:42:58 -07:00 коммит произвёл GitHub
Родитель 85958f0b73
Коммит 789ad81e67
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
12 изменённых файлов: 66 добавлений и 115 удалений

Просмотреть файл

@ -30,5 +30,6 @@
<ClInclude Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.SelfContained.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)WindowsAppRuntime.VersionInfo.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)winrt_WindowsAppRuntime.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)Logging.h" />
</ItemGroup>
</Project>

26
dev/Common/Logging.h Normal file
Просмотреть файл

@ -0,0 +1,26 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
#pragma once
#include <Windows.h>
#include <source_location>
#include <sstream>
namespace Common::Logging
{
// use std::format in msg parameter to have a printf like varargs syntax for the debug message
inline void DebugLog(const wchar_t* msg, const std::source_location location = std::source_location::current())
{
std::wstringstream filename ;
filename << location.file_name(); // converting char to wstring (and wchar_t implicitly)
// format is "foo.cpp (66) : debug message"
const std::wstring printmsg = std::format(L"{} ({}) : {}\n", filename.str(), location.line(), msg);
OutputDebugStringW(printmsg.c_str());
}
inline void DebugLog(const std::wstring& msg, const std::source_location location = std::source_location::current())
{
DebugLog(msg.c_str(), location);
}
}

Просмотреть файл

@ -2,12 +2,11 @@
// Licensed under the MIT License.
#include "pch.h"
#include "DataStore.h"
#include "Logging.h"
#include "DynamicDependencyDataStore_h.h"
#include "winrt_WindowsAppRuntime.h"
#include <wil/winrt.h>
#include <shlobj.h>

Просмотреть файл

@ -8,25 +8,10 @@
#include <stdint.h>
/// MSIX Dynamic Dependency HRESULT: Windows App Runtime is not in the package graph.
#define MDD_E_WINDOWSAPPRUNTIME_NOT_IN_PACKAGE_GRAPH _HRESULT_TYPEDEF_(0x80040001L)
/// MSIX Dynamic Dependency HRESULT: Data Store not found (Windows App Runtime's Main package not registered?)
// MSIX Dynamic Dependency HRESULT: Data Store not found (Windows App Runtime's Main package not registered?)
#define MDD_E_WINDOWSAPPRUNTIME_DATASTORE_NOT_FOUND _HRESULT_TYPEDEF_(0x80040002L)
/// MSIX Dynamic Dependency: Bootstrap initialization is scanning for an applicable DynamicDependencyLifetimeManager (DDLM) package
#define MDD_E_BOOTSTRAP_INITIALIZE_SCAN_FOR_DDLM _HRESULT_TYPEDEF_(0x80040010L)
/// MSIX Dynamic Dependency: Bootstrap initialization found a DynamicDependencyLifetimeManager (DDLM) but doesn't match the criteria
#define MDD_E_BOOTSTRAP_INITIALIZE_DDLM_SCAN_NO_MATCH _HRESULT_TYPEDEF_(0x80040011L)
/// MSIX Dynamic Dependency: Bootstrap initialization found a DynamicDependencyLifetimeManager (DDLM) that does match the criteria
#define MDD_E_BOOTSTRAP_INITIALIZE_DDLM_SCAN_MATCH _HRESULT_TYPEDEF_(0x80040012L)
/// MSIX Dynamic Dependency: Bootstrap initialization found an applicable DynamicDependencyLifetimeManager (DDLM) best matching the criteria
#define MDD_E_BOOTSTRAP_INITIALIZE_DDLM_FOUND _HRESULT_TYPEDEF_(0x80040013L)
/// MSIX Dynamic Dependency: Bootstrap initialization request is incompatible with current Bootstrap initialization state.
// MSIX Dynamic Dependency: Bootstrap initialization request is incompatible with current Bootstrap initialization state.
#define MDD_E_BOOTSTRAP_INITIALIZE_INCOMPATIBLE _HRESULT_TYPEDEF_(0x80040014L)
#if defined(__cplusplus)

Просмотреть файл

@ -6,10 +6,10 @@
#include "Microsoft.Windows.Management.Deployment.PackageDeploymentManager.g.cpp"
#include "M.W.M.D.PackageDeploymentResult.h"
#include "MsixPackageManager.h"
#include "PackageDeploymentResolver.h"
#include "PackageManagerTelemetry.h"
#include "logging.h"
static_assert(static_cast<int>(winrt::Microsoft::Windows::Management::Deployment::StubPackageOption::Default) == static_cast<int>(winrt::Windows::Management::Deployment::StubPackageOption::Default),
"winrt::Microsoft::Windows::Management::Deployment::StubPackageOption::Default != winrt::Windows::Management::Deployment::StubPackageOption::Default");
@ -54,15 +54,14 @@ namespace winrt::Microsoft::Windows::Management::Deployment::implementation
{
if (!IsReady(packageSetItem))
{
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_FAILED,
"Id=%ls PackageFamilyName=%ls MinVersion=%hu.%hu.%hu.%hu ArchitectureFilter:0x%X",
packageSetItem.Id().c_str(),
packageSetItem.PackageFamilyName().c_str(),
packageSetItem.MinVersion().Major,
packageSetItem.MinVersion().Minor,
packageSetItem.MinVersion().Build,
packageSetItem.MinVersion().Revision,
packageSetItem.ProcessorArchitectureFilter());
Common::Logging::DebugLog(std::format(L"Id={} PackageFamilyName={} MinVersion={}.{}.{}.{} ArchitectureFilter:0x{}",
packageSetItem.Id().c_str(),
packageSetItem.PackageFamilyName().c_str(),
packageSetItem.MinVersion().Major,
packageSetItem.MinVersion().Minor,
packageSetItem.MinVersion().Build,
packageSetItem.MinVersion().Revision,
static_cast<std::uint32_t>(packageSetItem.ProcessorArchitectureFilter())));
return false;
}
}

Просмотреть файл

@ -1,19 +0,0 @@
// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.
#if !defined(MSIXPACKAGEMANAGER_H)
#define MSIXPACKAGEMANAGER_H
/// MSIX Package Manager: Scanning for an applicable package
#define MSIXPACKAGEMANAGER_E_PACKAGE_SCAN _HRESULT_TYPEDEF_(0x80040301L)
/// MSIX Package Manager: Found a package but doesn't match the criteria
#define MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_NOT_MATCH _HRESULT_TYPEDEF_(0x80040302L)
/// MSIX Package Manager: Found a package that does match the criteria
#define MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_MATCH _HRESULT_TYPEDEF_(0x80040303L)
/// MSIX Package Manager: Found no applicable package matching the criteria
#define MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_FAILED _HRESULT_TYPEDEF_(0x80040304L)
#endif // MSIXPACKAGEMANAGER_H

Просмотреть файл

@ -6,8 +6,7 @@
#include <winrt/Microsoft.Windows.ApplicationModel.DynamicDependency.h>
#include "PackageDeploymentResolver.h"
#include "MsixPackageManager.h"
#include "logging.h"
namespace Microsoft::Windows::ApplicationModel::PackageDeploymentResolver
{
@ -162,7 +161,7 @@ bool Microsoft::Windows::ApplicationModel::PackageDeploymentResolver::FindAny(
winrt::hstring packageFullName{ Find(packageManager, packageFamilyName, minVersion, processorArchitectureFilter, true) };
return !packageFullName.empty();
}
//TODO : this looks to be very similar to MddBootstrap.cpp FindDDLMViaEnumeration(). Good candidate for refactoring.
winrt::hstring Microsoft::Windows::ApplicationModel::PackageDeploymentResolver::Find(
const winrt::Windows::Management::Deployment::PackageManager& packageManager,
const winrt::hstring& packageFamilyName,
@ -185,9 +184,7 @@ winrt::hstring Microsoft::Windows::ApplicationModel::PackageDeploymentResolver::
minVersion.Major, minVersion.Minor,
minVersion.Build, minVersion.Revision,
static_cast<std::uint32_t>(processorArchitectureFilter)) };
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN,
"PackageDeploymentResolver: Scanning packages (%ls)",
criteria.get());
Common::Logging::DebugLog(std::format(L"PackageDeploymentResolver: Scanning packages ({})", criteria.get()));
if (packages)
{
for (const winrt::Windows::ApplicationModel::Package& candidate : packages)
@ -204,9 +201,6 @@ winrt::hstring Microsoft::Windows::ApplicationModel::PackageDeploymentResolver::
auto candidateFullName{ packageId.FullName() };
if (candidateVersion < minVersion)
{
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_NOT_MATCH,
"PackageDeploymentResolver: %ls not applicable. Version doesn't match MinVersion criteria (%ls)",
candidateFullName.c_str(), criteria.get());
continue;
}
@ -226,10 +220,7 @@ winrt::hstring Microsoft::Windows::ApplicationModel::PackageDeploymentResolver::
const auto supportedArchitectures{ GetSystemSupportedArchitectures(nativeMachine) };
if (!IsArchitectureInArchitectures(candidateArchitecture, supportedArchitectures))
{
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_NOT_MATCH,
"PackageDeploymentResolver: %ls not applicable. Architecture (%ls) doesn't match system supported architectures (0x%X %ls)",
candidateFullName.c_str(), ::AppModel::Identity::GetArchitectureAsString(candidateArchitecture),
static_cast<std::uint32_t>(supportedArchitectures), GetSystemSupportedArchitecturesAsString(nativeMachine));
// package arch didn't match anything from system supported architectures
continue;
}
}
@ -237,10 +228,7 @@ winrt::hstring Microsoft::Windows::ApplicationModel::PackageDeploymentResolver::
{
if (!IsArchitectureInArchitectures(candidateArchitecture, processorArchitectureFilter))
{
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_NOT_MATCH,
"PackageDeploymentResolver: %ls not applicable. Architecture (%ls) doesn't match specified architectures (0x%X)",
candidateFullName.c_str(), ::AppModel::Identity::GetArchitectureAsString(candidateArchitecture),
static_cast<std::uint32_t>(processorArchitectureFilter));
// package arch doesn't match from specified arch list
continue;
}
}
@ -250,18 +238,16 @@ winrt::hstring Microsoft::Windows::ApplicationModel::PackageDeploymentResolver::
auto status{ candidate.Status() };
if (!status.VerifyIsOK())
{
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_NOT_MATCH,
"PackageDeploymentResolver: %ls not applicable. Status not OK (%ls)",
candidateFullName.c_str(), criteria.get());
Common::Logging::DebugLog(std::format(L"PackageDeploymentResolver: {} not applicable. Status not OK ({})",
candidateFullName.c_str(), criteria.get()));
continue;
}
// Are we looking for any match?
if (stopOnFirstMatch)
{
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_FAILED,
"PackageDeploymentResolver: Stopping on 1st match %ls (%ls)",
candidateFullName.c_str(), criteria.get());
Common::Logging::DebugLog(std::format(L"PackageDeploymentResolver: Stopping on 1st match {} ({})",
candidateFullName.c_str(), criteria.get()));
return candidateFullName;
}
@ -274,15 +260,13 @@ winrt::hstring Microsoft::Windows::ApplicationModel::PackageDeploymentResolver::
// Did we find what we're looking for?
if (bestFitPackageFullName.empty())
{
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_FAILED,
"PackageDeploymentResolver: No match (%ls)",
criteria.get());
Common::Logging::DebugLog(std::format(L"PackageDeploymentResolver: No match ({})",
criteria.get()));
}
else
{
(void)LOG_HR_MSG(MSIXPACKAGEMANAGER_E_PACKAGE_SCAN_MATCH,
"PackageDeploymentResolver: %ls is applicable (%ls)",
bestFitPackageFullName.c_str(), criteria.get());
Common::Logging::DebugLog(std::format(L"PackageDeploymentResolver: {} is applicable ({})",
bestFitPackageFullName.c_str(), criteria.get()));
}
return bestFitPackageFullName;
}

Просмотреть файл

@ -45,7 +45,6 @@
<ClInclude Include="$(MSBuildThisFileDirectory)M.W.M.D.RegisterPackageOptions.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)M.W.M.D.RemovePackageOptions.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)M.W.M.D.StagePackageOptions.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)MsixPackageManager.h" />
<ClInclude Include="$(MSBuildThisFileDirectory)PackageDeploymentResolver.h" />
</ItemGroup>
<ItemGroup>
@ -53,7 +52,5 @@
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(RepoRoot)\dev\DynamicDependency\API;%(OutDir)</AdditionalIncludeDirectories>
</Midl>
</ItemGroup>
<ItemGroup>
<PublicHeaders Include="$(MSBuildThisFileDirectory)MsixPackageManager.h" />
</ItemGroup>
</Project>

Просмотреть файл

@ -97,9 +97,6 @@
<ClInclude Include="$(MSBuildThisFileDirectory)M.W.M.D.StagePackageOptions.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)MsixPackageManager.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="$(MSBuildThisFileDirectory)PackageDeploymentResolver.h">
<Filter>Header Files</Filter>
</ClInclude>

Просмотреть файл

@ -28,5 +28,4 @@
#include <appmodel.identity.h>
#include <appmodel.package.h>
#include "MsixPackageManager.h"
#include "Logging.h"

Просмотреть файл

@ -840,8 +840,7 @@ void FindDDLMViaEnumeration(
std::wstring& ddlmPackageFullName)
{
// Find the best fit
bool foundAny{};
PACKAGE_VERSION bestFitVersion{};
PACKAGE_VERSION bestFitVersion{0}; //setting everything to 0
winrt::hstring bestFitPackageFamilyName{};
winrt::hstring bestFitPackageFullName{};
@ -895,7 +894,6 @@ void FindDDLMViaEnumeration(
winrt::hstring currentUser;
const auto c_packageTypes{ winrt::Windows::Management::Deployment::PackageTypes::Main };
auto packages{ packageManager.FindPackagesForUserWithPackageTypes(currentUser, c_packageTypes) };
(void)LOG_HR_MSG(MDD_E_BOOTSTRAP_INITIALIZE_SCAN_FOR_DDLM, "Bootstrap.Intitialize: Scanning packages for %ls", criteria.get());
int packagesScanned{};
for (auto package : packages)
{
@ -982,9 +980,6 @@ void FindDDLMViaEnumeration(
version.Revision = packageVersion.Revision;
if (version.Version < minVersion.Version)
{
(void)LOG_HR_MSG(MDD_E_BOOTSTRAP_INITIALIZE_DDLM_SCAN_NO_MATCH,
"Bootstrap.Intitialize: %ls not applicable. Version doesn't match MinVersion criteria (%ls)",
packageFullName.c_str(), criteria.get());
continue;
}
@ -993,41 +988,28 @@ void FindDDLMViaEnumeration(
const auto currentArchitecture{ AppModel::Identity::GetCurrentArchitecture() };
if (architecture != currentArchitecture)
{
(void)LOG_HR_MSG(MDD_E_BOOTSTRAP_INITIALIZE_DDLM_SCAN_NO_MATCH,
"Bootstrap.Intitialize: %ls not applicable. Architecture doesn't match current architecture %ls (%ls)",
packageFullName.c_str(), ::AppModel::Identity::GetCurrentArchitectureAsString(), criteria.get());
continue;
}
// Do we have a package under consideration?
if (!foundAny)
{
(void)LOG_HR_MSG(MDD_E_BOOTSTRAP_INITIALIZE_DDLM_SCAN_MATCH,
"Bootstrap.Intitialize: %ls is applicable (%ls)",
packageFullName.c_str(), criteria.get());
bestFitVersion = version;
bestFitPackageFamilyName = packageId.FamilyName();
bestFitPackageFullName = packageId.FullName();
foundAny = true;
continue;
}
// Do we already have a higher version under consideration?
// this works for both cases :
// 1. find the first package which matches the criteria (because we initiailizue bestfit with 0 version)
// 2. find a better match than previously matched best fit
if (bestFitVersion.Version < version.Version)
{
(void)LOG_HR_MSG(MDD_E_BOOTSTRAP_INITIALIZE_DDLM_SCAN_MATCH,
"Bootstrap.Intitialize: %ls is more applicable (%ls)",
packageFullName.c_str(), criteria.get());
bestFitVersion = version;
bestFitPackageFamilyName = packageId.FamilyName();
bestFitPackageFullName = packageId.FullName();
continue;
}
}
THROW_HR_IF_MSG(STATEREPOSITORY_E_DEPENDENCY_NOT_RESOLVED, !foundAny, "Enumeration: %ls", criteria.get());
(void)LOG_HR_MSG(MDD_E_BOOTSTRAP_INITIALIZE_DDLM_FOUND,
"Bootstrap.Intitialize: %ls best matches the criteria (%ls) of %d packages scanned",
bestFitPackageFullName.c_str(), criteria.get(), packagesScanned);
// if bestFitVersion is 0, the value we initialized it with, then we didn't find a suitable candidate
THROW_HR_IF_MSG(STATEREPOSITORY_E_DEPENDENCY_NOT_RESOLVED, bestFitVersion.Version == 0, "No suitable version matching criteria is found. Criteria: %ls", criteria.get());
{
wchar_t printmsg[128];
Common::Logging::DebugLog(std::format(L"Bootstrap.Intitialize: {} best matches the criteria ({}) of {} packages scanned",
bestFitPackageFullName.c_str(), criteria.get(), packagesScanned));
}
ddlmPackageFamilyName = bestFitPackageFamilyName.c_str();
ddlmPackageFullName = bestFitPackageFullName.c_str();
}

Просмотреть файл

@ -16,5 +16,6 @@
#include "framework.h"
#include "MddBootstrapActivity.h"
#include "MddBootstrapTracelogging.h"
#include "logging.h"
#endif //PCH_H