Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert global loggers to Thread local loggers #3

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/AppInstallerCLICore/COMContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,23 @@ namespace AppInstaller
m_executionStage = executionStage;
m_comProgressCallback(ReportType::ExecutionPhaseUpdate, 0, 0, ProgressType::None, m_executionStage);
}

void COMContext::SetLoggers(std::string telemetryCorelationJson, std::string comCaller)
{
SetThreadGlobalsActive();

Logging::Log().SetLevel(Logging::Level::Verbose);
Logging::AddTraceLogger();

#ifdef _DEBUG
// Log to file for COM API calls only when debugging in visual studio
Logging::AddFileLogger();
Logging::BeginLogFileCleanup();
#endif

Logging::EnableWilFailureTelemetry();
Logging::Telemetry().SetTelemetryCorelationJson(telemetryCorelationJson);
Logging::Telemetry().SetCOMCaller(comCaller);
Logging::Telemetry().LogStartup(true);
}
}
5 changes: 5 additions & 0 deletions src/AppInstallerCLICore/COMContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ namespace AppInstaller
//Execution::Context
void SetExecutionStage(CLI::Workflow::ExecutionStage executionPhase, bool);

// Set Progress Callback function
void SetProgressCallbackFunction(ProgressCallBackFunction&& f)
{
m_comProgressCallback = std::move(f);
}

private:

// Set diagnostic and telemetry loggers
void SetLoggers(std::string telemetryCorelationJson, std::string comCaller);

CLI::Workflow::ExecutionStage m_executionStage = CLI::Workflow::ExecutionStage::Initial;
ProgressCallBackFunction m_comProgressCallback;
};
Expand Down
10 changes: 7 additions & 3 deletions src/AppInstallerCLICore/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@ namespace AppInstaller::CLI
{
init_apartment();

Execution::Context context{ std::cout, std::cin };
context.EnableCtrlHandler();

context.SetThreadGlobalsActive();

// Enable all logging for this phase; we will update once we have the arguments
Logging::Log().EnableChannel(Logging::Channel::All);
Logging::Log().SetLevel(Logging::Level::Verbose);

Logging::AddFileLogger();
Logging::AddTraceLogger();
Logging::EnableWilFailureTelemetry();

// Set output to UTF8
Expand All @@ -60,9 +67,6 @@ namespace AppInstaller::CLI
// Initiate the background cleanup of the log file location.
Logging::BeginLogFileCleanup();

Execution::Context context{ std::cout, std::cin };
context.EnableCtrlHandler();

context << Workflow::ReportExecutionStage(Workflow::ExecutionStage::ParseArgs);

// Convert incoming wide char args to UTF8
Expand Down
5 changes: 5 additions & 0 deletions src/AppInstallerCLICore/ExecutionContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,9 @@ namespace AppInstaller::CLI::Execution

m_executionStage = stage;
}

void Context::SetThreadGlobalsActive()
{
m_threadGlobals.SetForCurrentThread();
}
}
6 changes: 5 additions & 1 deletion src/AppInstallerCLICore/ExecutionContext.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#pragma once
#include <AppInstallerLogging.h>
#include <ThreadGlobals.h>
#include "ExecutionReporter.h"
#include "ExecutionArgs.h"
#include "ExecutionContextData.h"
Expand Down Expand Up @@ -102,6 +102,9 @@ namespace AppInstaller::CLI::Execution
}

virtual void SetExecutionStage(Workflow::ExecutionStage stage, bool);

// Set Globals for Current Thread
void SetThreadGlobalsActive();

#ifndef AICLI_DISABLE_TEST_HOOKS
// Enable tests to override behavior
Expand All @@ -115,5 +118,6 @@ namespace AppInstaller::CLI::Execution
size_t m_CtrlSignalCount = 0;
ContextFlag m_flags = ContextFlag::None;
Workflow::ExecutionStage m_executionStage = Workflow::ExecutionStage::Initial;
AppInstaller::ThreadLocalStorage::ThreadGlobals m_threadGlobals;
};
}
1 change: 1 addition & 0 deletions src/AppInstallerCLITests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ int main(int argc, char** argv)
}
Logging::Log().SetLevel(Logging::Level::Verbose);
Logging::EnableWilFailureTelemetry();
AppInstaller::Logging::Telemetry().SetUserSettingsStatus();

// Force all tests to run against settings inside this container.
// This prevents test runs from trashing the users actual settings.
Expand Down
4 changes: 4 additions & 0 deletions src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,9 @@
<ClInclude Include="Public\AppInstallerTelemetry.h" />
<ClInclude Include="Public\AppInstallerLogging.h" />
<ClInclude Include="Public\AppInstallerArchitecture.h" />
<ClInclude Include="Public\AppInstallerTraceLogger.h" />
<ClInclude Include="Public\AppInstallerVersions.h" />
<ClInclude Include="Public\ThreadGlobals.h" />
<ClInclude Include="Public\winget\ExperimentalFeature.h" />
<ClInclude Include="Public\winget\ExtensionCatalog.h" />
<ClInclude Include="Public\winget\JsonSchemaValidation.h" />
Expand Down Expand Up @@ -323,6 +325,7 @@
<ExcludedFromBuild Condition="'$(Configuration)'=='Fuzzing'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="NameNormalization.cpp" />
<ClCompile Include="Public\TraceLogger.cpp" />
<ClCompile Include="Regex.cpp" />
<ClCompile Include="Registry.cpp" />
<ClCompile Include="Runtime.cpp" />
Expand All @@ -335,6 +338,7 @@
<ClCompile Include="Synchronization.cpp" />
<ClCompile Include="Telemetry\TraceLogging.cpp" />
<ClCompile Include="Architecture.cpp" />
<ClCompile Include="ThreadGlobals.cpp" />
<ClCompile Include="UserSettings.cpp" />
<ClCompile Include="Versions.cpp" />
<ClCompile Include="Yaml.cpp" />
Expand Down
12 changes: 12 additions & 0 deletions src/AppInstallerCommonCore/AppInstallerCommonCore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@
<ClInclude Include="Public\winget\JsonSchemaValidation.h">
<Filter>Public\winget</Filter>
</ClInclude>
<ClInclude Include="Public\AppInstallerTraceLogger.h">
<Filter>Public</Filter>
</ClInclude>
<ClInclude Include="Public\ThreadGlobals.h">
<Filter>Public</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
Expand Down Expand Up @@ -269,6 +275,12 @@
<ClCompile Include="JsonSchemaValidation.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Public\TraceLogger.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ThreadGlobals.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="PropertySheet.props" />
Expand Down
21 changes: 15 additions & 6 deletions src/AppInstallerCommonCore/AppInstallerLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
#include "Public/AppInstallerLogging.h"

#include "Public/AppInstallerFileLogger.h"
#include "Public/AppInstallerTraceLogger.h"
#include "Public/AppInstallerTelemetry.h"
#include "Public/AppInstallerDateTime.h"
#include "Public/AppInstallerRuntime.h"

#include "Public/ThreadGlobals.h"

namespace AppInstaller::Logging
{
namespace
Expand Down Expand Up @@ -42,18 +45,13 @@ namespace AppInstaller::Logging
case Channel::YAML: return "YAML";
case Channel::Core: return "CORE";
case Channel::Test: return "TEST";
case Channel::Log: return "LOG";
default: return "NONE";
}
}

size_t GetMaxChannelNameLength() { return 4; }

DiagnosticLogger& DiagnosticLogger::GetInstance()
{
static DiagnosticLogger instance;
return instance;
}

void DiagnosticLogger::AddLogger(std::unique_ptr<ILogger>&& logger)
{
m_loggers.emplace_back(std::move(logger));
Expand Down Expand Up @@ -129,11 +127,21 @@ namespace AppInstaller::Logging
}
}

DiagnosticLogger& Log()
{
return AppInstaller::ThreadLocalStorage::ThreadGlobals::GetForCurrentThread()->GetDiagnosticLogger();
}

void AddFileLogger(const std::filesystem::path& filePath)
{
Log().AddLogger(std::make_unique<FileLogger>(filePath));
}

void AddTraceLogger()
{
Log().AddLogger(std::make_unique<TraceLogger>());
}

void BeginLogFileCleanup()
{
FileLogger::BeginCleanup(Runtime::GetPathTo(Runtime::PathName::DefaultLogLocation));
Expand All @@ -143,6 +151,7 @@ namespace AppInstaller::Logging
{
return out << std::hex << std::setw(8) << std::setfill('0');
}

}

std::ostream& operator<<(std::ostream& out, const std::chrono::system_clock::time_point& time)
Expand Down
Loading