-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ODS logger and use it during init (#4969)
## Change The user settings (and then group policy) initialization happen before we get the file logger set up. To be able to see into what is happening there more easily, this change creates a logger that uses `OutputDebugString` and uses it during the initial file logger creation.
- Loading branch information
Showing
6 changed files
with
131 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
#include "pch.h" | ||
#include "winget/OutputDebugStringLogger.h" | ||
|
||
namespace AppInstaller::Logging | ||
{ | ||
namespace | ||
{ | ||
static constexpr std::string_view s_OutputDebugStringLoggerName = "OutputDebugStringLogger"; | ||
} | ||
|
||
std::string OutputDebugStringLogger::GetName() const | ||
{ | ||
return std::string{ s_OutputDebugStringLoggerName }; | ||
} | ||
|
||
void OutputDebugStringLogger::Write(Channel channel, Level, std::string_view message) noexcept try | ||
{ | ||
std::stringstream strstr; | ||
strstr << "[" << std::setw(GetMaxChannelNameLength()) << std::left << std::setfill(' ') << GetChannelName(channel) << "] " << message << std::endl; | ||
std::string formattedMessage = std::move(strstr).str(); | ||
|
||
OutputDebugStringA(formattedMessage.c_str()); | ||
} | ||
catch (...) | ||
{ | ||
// Just eat any exceptions here; better than losing logs | ||
} | ||
|
||
void OutputDebugStringLogger::WriteDirect(Channel, Level, std::string_view message) noexcept try | ||
{ | ||
std::string nullTerminatedMessage{ message }; | ||
OutputDebugStringA(nullTerminatedMessage.c_str()); | ||
} | ||
catch (...) | ||
{ | ||
// Just eat any exceptions here; better than losing logs | ||
} | ||
|
||
void OutputDebugStringLogger::Add() | ||
{ | ||
Log().AddLogger(std::make_unique<OutputDebugStringLogger>()); | ||
} | ||
|
||
void OutputDebugStringLogger::Remove() | ||
{ | ||
Log().RemoveLogger(std::string{ s_OutputDebugStringLoggerName }); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/AppInstallerCommonCore/Public/winget/OutputDebugStringLogger.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
#pragma once | ||
#include <AppInstallerLogging.h> | ||
|
||
namespace AppInstaller::Logging | ||
{ | ||
// Sends logs to the OutputDebugString function. | ||
// Intended for use during initialization debugging. | ||
struct OutputDebugStringLogger : ILogger | ||
{ | ||
OutputDebugStringLogger() = default; | ||
|
||
~OutputDebugStringLogger() = default; | ||
|
||
// ILogger | ||
std::string GetName() const override; | ||
|
||
void Write(Channel channel, Level, std::string_view message) noexcept override; | ||
|
||
void WriteDirect(Channel channel, Level level, std::string_view message) noexcept override; | ||
|
||
// Adds OutputDebugStringLogger to the current Log | ||
static void Add(); | ||
|
||
// Removes OutputDebugStringLogger from the current Log | ||
static void Remove(); | ||
}; | ||
} |