commit 264eaf340bb4dc922e2a6dbd0ae55cf31a3c08f5
parent a3c53edb404b90a6eb2eec385d8cf11da395b1a0
Author: JohnMcPMS <johnmcp@microsoft.com>
Date: Fri, 7 Feb 2020 11:23:39 -0800
Improve util dll logging interface (#31)
Diffstat:
11 files changed, 79 insertions(+), 14 deletions(-)
diff --git a/src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj b/src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj
@@ -165,13 +165,13 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="DateTime.h" />
- <ClInclude Include="FileLogger.h" />
<ClInclude Include="HttpStream\HttpClientWrapper.h" />
<ClInclude Include="HttpStream\HttpLocalCache.h" />
<ClInclude Include="HttpStream\HttpRandomAccessStream.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="Public\AppInstallerDownloader.h" />
<ClInclude Include="Public\AppInstallerErrors.h" />
+ <ClInclude Include="Public\AppInstallerFileLogger.h" />
<ClInclude Include="Public\AppInstallerLanguageUtilities.h" />
<ClInclude Include="Public\AppInstallerMsixInfo.h" />
<ClInclude Include="Public\AppInstallerRuntime.h" />
diff --git a/src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj.filters b/src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj.filters
@@ -45,9 +45,6 @@
<ClInclude Include="Public\AppInstallerStrings.h">
<Filter>Public</Filter>
</ClInclude>
- <ClInclude Include="FileLogger.h">
- <Filter>Header Files</Filter>
- </ClInclude>
<ClInclude Include="DateTime.h">
<Filter>Header Files</Filter>
</ClInclude>
@@ -81,6 +78,9 @@
<ClInclude Include="Public\AppInstallerMsixInfo.h">
<Filter>Public</Filter>
</ClInclude>
+ <ClInclude Include="Public\AppInstallerFileLogger.h">
+ <Filter>Public</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
diff --git a/src/AppInstallerCommonCore/AppInstallerLogging.cpp b/src/AppInstallerCommonCore/AppInstallerLogging.cpp
@@ -4,8 +4,8 @@
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerTelemetry.h"
+#include "Public/AppInstallerFileLogger.h"
#include "DateTime.h"
-#include "FileLogger.h"
namespace AppInstaller::Logging
{
@@ -57,6 +57,19 @@ namespace AppInstaller::Logging
m_loggers.emplace_back(std::move(logger));
}
+ bool DiagnosticLogger::ContainsLogger(const std::string& name)
+ {
+ for (auto i = m_loggers.begin(); i != m_loggers.end(); ++i)
+ {
+ if ((*i)->GetName() == name)
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
std::unique_ptr<ILogger> DiagnosticLogger::RemoveLogger(const std::string& name)
{
std::unique_ptr<ILogger> result;
@@ -74,6 +87,11 @@ namespace AppInstaller::Logging
return result;
}
+ void DiagnosticLogger::RemoveAllLoggers()
+ {
+ m_loggers.clear();
+ }
+
void DiagnosticLogger::EnableChannel(Channel channel)
{
m_enabledChannels |= ConvertChannelToBitmask(channel);
@@ -91,7 +109,8 @@ namespace AppInstaller::Logging
bool DiagnosticLogger::IsEnabled(Channel channel, Level level) const
{
- return ((m_enabledChannels & ConvertChannelToBitmask(channel)) != 0 &&
+ return (!m_loggers.empty() &&
+ (m_enabledChannels & ConvertChannelToBitmask(channel)) != 0 &&
(AsNum(level) >= AsNum(m_enabledLevel)));
}
diff --git a/src/AppInstallerCommonCore/FileLogger.cpp b/src/AppInstallerCommonCore/FileLogger.cpp
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
-#include "FileLogger.h"
+#include "Public/AppInstallerFileLogger.h"
#include "Public/AppInstallerRuntime.h"
#include "DateTime.h"
diff --git a/src/AppInstallerCommonCore/FileLogger.h b/src/AppInstallerCommonCore/Public/AppInstallerFileLogger.h
diff --git a/src/AppInstallerCommonCore/Public/AppInstallerLogging.h b/src/AppInstallerCommonCore/Public/AppInstallerLogging.h
@@ -81,12 +81,24 @@ namespace AppInstaller::Logging
// Gets the singleton instance of this type.
static DiagnosticLogger& GetInstance();
+ // NOTE: The logger management functionality is *SINGLE THREAD SAFE*.
+ // This includes with logging itself.
+ // As it is not expected that adding/removing loggers is an
+ // extremely frequent operation, no care has been made to protect
+ // it from modifying loggers while logging may be occurring.
+
// Adds a logger to the active set.
void AddLogger(std::unique_ptr<ILogger>&& logger);
+ // Determines if a logger with the given name is present.
+ bool ContainsLogger(const std::string& name);
+
// Removes a logger from the active set, returning it.
std::unique_ptr<ILogger> RemoveLogger(const std::string& name);
+ // Removes all loggers.
+ void RemoveAllLoggers();
+
// Enables the given channel.
void EnableChannel(Channel channel);
diff --git a/src/AppInstallerRepositoryCore/Microsoft/Schema/Version.cpp b/src/AppInstallerRepositoryCore/Microsoft/Schema/Version.cpp
@@ -37,7 +37,7 @@ namespace AppInstaller::Repository::Microsoft::Schema
}
// We do not have the capacity to operate on this schema version
- THROW_HR(E_NOT_SET);
+ THROW_HR(HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED));
}
std::ostream& operator<<(std::ostream& out, const Version& version)
diff --git a/src/AppInstallerSQLiteIndexUtil/AppInstallerSQLiteIndexUtil.h b/src/AppInstallerSQLiteIndexUtil/AppInstallerSQLiteIndexUtil.h
@@ -18,6 +18,11 @@ extern "C"
APPINSTALLER_SQLITE_INDEX_API AppInstallerLoggingInit(
APPINSTALLER_SQLITE_INDEX_STRING logPath);
+ // Removes the given log file from the logging infrastructure.
+ // If logPath is nullptr, then remove all loggers.
+ APPINSTALLER_SQLITE_INDEX_API AppInstallerLoggingTerm(
+ APPINSTALLER_SQLITE_INDEX_STRING logPath);
+
// Creates a new index file at filePath with the given version.
APPINSTALLER_SQLITE_INDEX_API AppInstallerSQLiteIndexCreate(
APPINSTALLER_SQLITE_INDEX_STRING filePath,
diff --git a/src/AppInstallerSQLiteIndexUtil/Exports.cpp b/src/AppInstallerSQLiteIndexUtil/Exports.cpp
@@ -3,7 +3,6 @@
#include "pch.h"
#include "AppInstallerSQLiteIndexUtil.h"
-
using namespace AppInstaller::Utility;
using namespace AppInstaller::Repository::Microsoft;
@@ -13,11 +12,37 @@ extern "C"
{
THROW_HR_IF(E_INVALIDARG, !logPath);
- // Enable all logs for now.
- AppInstaller::Logging::Log().EnableChannel(AppInstaller::Logging::Channel::All);
- AppInstaller::Logging::Log().SetLevel(AppInstaller::Logging::Level::Verbose);
- AppInstaller::Logging::AddFileLogger(logPath);
- AppInstaller::Logging::EnableWilFailureTelemetry();
+ static std::once_flag s_initLogging;
+ std::call_once(s_initLogging, []() {
+ // Enable all logs for now.
+ AppInstaller::Logging::Log().EnableChannel(AppInstaller::Logging::Channel::All);
+ AppInstaller::Logging::Log().SetLevel(AppInstaller::Logging::Level::Verbose);
+ AppInstaller::Logging::EnableWilFailureTelemetry();
+ });
+
+ std::filesystem::path pathAsPath = logPath;
+ std::string loggerName = AppInstaller::Logging::FileLogger::GetNameForPath(pathAsPath);
+
+ if (!AppInstaller::Logging::Log().ContainsLogger(loggerName))
+ {
+ AppInstaller::Logging::AddFileLogger(pathAsPath);
+ }
+
+ return S_OK;
+ }
+ CATCH_RETURN()
+
+ APPINSTALLER_SQLITE_INDEX_API AppInstallerLoggingTerm(APPINSTALLER_SQLITE_INDEX_STRING logPath) try
+ {
+ if (logPath)
+ {
+ std::string loggerName = AppInstaller::Logging::FileLogger::GetNameForPath(logPath);
+ (void)AppInstaller::Logging::Log().RemoveLogger(loggerName);
+ }
+ else
+ {
+ AppInstaller::Logging::Log().RemoveAllLoggers();
+ }
return S_OK;
}
diff --git a/src/AppInstallerSQLiteIndexUtil/Source.def b/src/AppInstallerSQLiteIndexUtil/Source.def
@@ -1,6 +1,7 @@
LIBRARY AppInstallerSQLiteIndexUtil
EXPORTS
AppInstallerLoggingInit
+ AppInstallerLoggingTerm
AppInstallerSQLiteIndexCreate
AppInstallerSQLiteIndexOpen
AppInstallerSQLiteIndexClose
diff --git a/src/AppInstallerSQLiteIndexUtil/pch.h b/src/AppInstallerSQLiteIndexUtil/pch.h
@@ -5,6 +5,7 @@
#define NOMINMAX
#include <Windows.h>
+#include <Public/AppInstallerFileLogger.h>
#include <Public/AppInstallerStrings.h>
#include <Public/AppInstallerLogging.h>
#include <Public/AppInstallerTelemetry.h>
@@ -12,5 +13,7 @@
#include <wil/result_macros.h>
+#include <filesystem>
#include <memory>
+#include <mutex>
#include <string>