forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppInstallerLogging.cpp
More file actions
190 lines (162 loc) · 4.91 KB
/
Copy pathAppInstallerLogging.cpp
File metadata and controls
190 lines (162 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerFileLogger.h"
#include "Public/winget/TraceLogger.h"
#include "Public/AppInstallerTelemetry.h"
#include "Public/AppInstallerDateTime.h"
#include "Public/AppInstallerRuntime.h"
#include "Public/winget/ThreadGlobals.h"
namespace AppInstaller::Logging
{
namespace
{
template <typename E>
std::underlying_type_t<E> AsNum(E e)
{
return static_cast<std::underlying_type_t<E>>(e);
}
uint64_t ConvertChannelToBitmask(Channel channel)
{
if (channel == Channel::All)
{
return std::numeric_limits<uint64_t>::max();
}
else
{
return (1ull << AsNum(channel));
}
}
}
char const* GetChannelName(Channel channel)
{
switch(channel)
{
case Channel::Fail: return "FAIL";
case Channel::CLI: return "CLI";
case Channel::SQL: return "SQL";
case Channel::Repo: return "REPO";
case Channel::YAML: return "YAML";
case Channel::Core: return "CORE";
case Channel::Test: return "TEST";
default: return "NONE";
}
}
size_t GetMaxChannelNameLength() { return 4; }
void DiagnosticLogger::AddLogger(std::unique_ptr<ILogger>&& logger)
{
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;
for (auto i = m_loggers.begin(); i != m_loggers.end(); ++i)
{
if ((*i)->GetName() == name)
{
result = std::move(*i);
m_loggers.erase(i);
break;
}
}
return result;
}
void DiagnosticLogger::RemoveAllLoggers()
{
m_loggers.clear();
}
void DiagnosticLogger::EnableChannel(Channel channel)
{
m_enabledChannels |= ConvertChannelToBitmask(channel);
}
void DiagnosticLogger::DisableChannel(Channel channel)
{
m_enabledChannels &= ~ConvertChannelToBitmask(channel);
}
void DiagnosticLogger::SetLevel(Level level)
{
m_enabledLevel = level;
}
bool DiagnosticLogger::IsEnabled(Channel channel, Level level) const
{
return (!m_loggers.empty() &&
(m_enabledChannels & ConvertChannelToBitmask(channel)) != 0 &&
(AsNum(level) >= AsNum(m_enabledLevel)));
}
void DiagnosticLogger::Write(Channel channel, Level level, std::string_view message)
{
THROW_HR_IF_MSG(E_INVALIDARG, channel == Channel::All, "Cannot write to all channels");
if (IsEnabled(channel, level))
{
for (auto& logger : m_loggers)
{
logger->Write(channel, level, message);
}
}
}
void DiagnosticLogger::WriteDirect(Channel channel, Level level, std::string_view message)
{
THROW_HR_IF_MSG(E_INVALIDARG, channel == Channel::All, "Cannot write to all channels");
if (IsEnabled(channel, level))
{
for (auto& logger : m_loggers)
{
logger->WriteDirect(message);
}
}
}
DiagnosticLogger& Log()
{
ThreadLocalStorage::ThreadGlobals* pThreadGlobals = ThreadLocalStorage::ThreadGlobals::GetForCurrentThread();
if (pThreadGlobals)
{
return pThreadGlobals->GetDiagnosticLogger();
}
else
{
static DiagnosticLogger processGlobalLogger;
return processGlobalLogger;
}
}
void AddFileLogger()
{
Log().AddLogger(std::make_unique<FileLogger>());
}
void AddFileLogger(const std::filesystem::path& filePath)
{
Log().AddLogger(std::make_unique<FileLogger>(filePath));
}
void AddFileLogger(std::string_view fileNamePrefix)
{
Log().AddLogger(std::make_unique<FileLogger>(fileNamePrefix));
}
void AddTraceLogger()
{
Log().AddLogger(std::make_unique<TraceLogger>());
}
void BeginLogFileCleanup()
{
FileLogger::BeginCleanup(Runtime::GetPathTo(Runtime::PathName::DefaultLogLocation));
}
std::ostream& SetHRFormat(std::ostream& out)
{
return out << std::hex << std::setw(8) << std::setfill('0');
}
}
std::ostream& operator<<(std::ostream& out, const std::chrono::system_clock::time_point& time)
{
AppInstaller::Utility::OutputTimePoint(out, time);
return out;
}