-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathAppInstallerLogging.cpp
218 lines (191 loc) · 5.52 KB
/
AppInstallerLogging.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerStrings.h"
#include "Public/AppInstallerDateTime.h"
#include "Public/winget/SharedThreadGlobals.h"
namespace AppInstaller::Logging
{
std::string_view 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";
case Channel::Config: return "CONF";
case Channel::Workflow: return "WORK";
default: return "NONE";
}
}
Channel GetChannelFromName(std::string_view channel)
{
std::string lowerChannel = Utility::ToLower(channel);
if (lowerChannel == "fail")
{
return Channel::Fail;
}
else if (lowerChannel == "cli")
{
return Channel::CLI;
}
else if (lowerChannel == "sql")
{
return Channel::SQL;
}
else if (lowerChannel == "repo")
{
return Channel::Repo;
}
else if (lowerChannel == "yaml")
{
return Channel::YAML;
}
else if (lowerChannel == "core")
{
return Channel::Core;
}
else if (lowerChannel == "test")
{
return Channel::Test;
}
else if (lowerChannel == "conf" || lowerChannel == "config")
{
return Channel::Config;
}
else if (lowerChannel == "workflow")
{
return Channel::Workflow;
}
else if (lowerChannel == "default" || lowerChannel == "defaults")
{
return Channel::Defaults;
}
else if (lowerChannel == "all")
{
return Channel::All;
}
return Channel::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)
{
WI_SetAllFlags(m_enabledChannels, channel);
}
void DiagnosticLogger::DisableChannel(Channel channel)
{
WI_ClearAllFlags(m_enabledChannels, channel);
}
void DiagnosticLogger::SetLevel(Level level)
{
m_enabledLevel = level;
}
Level DiagnosticLogger::GetLevel() const
{
return m_enabledLevel;
}
bool DiagnosticLogger::IsEnabled(Channel channel, Level level) const
{
return (!m_loggers.empty() &&
WI_IsAnyFlagSet(m_enabledChannels, channel) &&
(ToIntegral(level) >= ToIntegral(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(channel, level, message);
}
}
}
DiagnosticLogger& Log()
{
ThreadLocalStorage::ThreadGlobals* pThreadGlobals = ThreadLocalStorage::ThreadGlobals::GetForCurrentThread();
if (pThreadGlobals)
{
return pThreadGlobals->GetDiagnosticLogger();
}
else
{
static DiagnosticLogger processGlobalLogger;
return processGlobalLogger;
}
}
std::ostream& SetHRFormat(std::ostream& out)
{
return out << std::hex << std::setw(8) << std::setfill('0');
}
}
namespace std
{
std::ostream& operator<<(std::ostream& out, const std::chrono::system_clock::time_point& time)
{
AppInstaller::Utility::OutputTimePoint(out, time);
return out;
}
std::ostream& operator<<(std::ostream& out, const GUID& guid)
{
wchar_t buffer[256];
if (StringFromGUID2(guid, buffer, ARRAYSIZE(buffer)))
{
out << AppInstaller::Utility::ConvertToUTF8(buffer);
}
else
{
out << "error";
}
return out;
}
}