-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathHostLogger.cs
346 lines (287 loc) · 12.3 KB
/
HostLogger.cs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Concurrent;
using System.IO;
using System.Management.Automation.Host;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
namespace Microsoft.PowerShell.EditorServices.Hosting
{
/// <summary>
/// User-facing log level for editor services configuration.
/// </summary>
/// <remarks>
/// The underlying values of this enum attempt to align to both
/// <see cref="Microsoft.Extensions.Logging.LogLevel" /> and
/// <see cref="Serilog.Events.LogEventLevel" />.
/// </remarks>
public enum PsesLogLevel
{
Diagnostic = 0,
Verbose = 1,
Normal = 2,
Warning = 3,
Error = 4,
}
/// <summary>
/// A logging front-end for host startup allowing handover to the backend and decoupling from
/// the host's particular logging sink.
/// </summary>
/// <remarks>
/// This custom logger exists to allow us to log during startup, which is vital information for
/// debugging, but happens before we can load any logger library. This is because startup
/// happens in our isolated assembly environment. See #2292 for more information.
/// </remarks>
public sealed class HostLogger :
IObservable<(PsesLogLevel logLevel, string message)>,
IObservable<(int logLevel, string message)>
{
/// <summary>
/// A simple translation struct to convert PsesLogLevel to an int for backend passthrough.
/// </summary>
private class LogObserver : IObserver<(PsesLogLevel logLevel, string message)>
{
private readonly IObserver<(int logLevel, string message)> _observer;
public LogObserver(IObserver<(int logLevel, string message)> observer) => _observer = observer;
public void OnCompleted() => _observer.OnCompleted();
public void OnError(Exception error) => _observer.OnError(error);
public void OnNext((PsesLogLevel logLevel, string message) value) => _observer.OnNext(((int)value.logLevel, value.message));
}
/// <summary>
/// Simple unsubscriber that allows subscribers to remove themselves from the observer list later.
/// </summary>
private class Unsubscriber : IDisposable
{
private readonly ConcurrentDictionary<IObserver<(PsesLogLevel, string)>, bool> _subscribedObservers;
private readonly IObserver<(PsesLogLevel, string)> _thisSubscriber;
public Unsubscriber(ConcurrentDictionary<IObserver<(PsesLogLevel, string)>, bool> subscribedObservers, IObserver<(PsesLogLevel, string)> thisSubscriber)
{
_subscribedObservers = subscribedObservers;
_thisSubscriber = thisSubscriber;
}
public void Dispose() => _subscribedObservers.TryRemove(_thisSubscriber, out bool _);
}
private readonly PsesLogLevel _minimumLogLevel;
private readonly ConcurrentQueue<(PsesLogLevel logLevel, string message)> _logMessages;
// The bool value here is meaningless and ignored,
// the ConcurrentDictionary just provides a way to efficiently keep track of subscribers across threads
private readonly ConcurrentDictionary<IObserver<(PsesLogLevel logLevel, string message)>, bool> _observers;
/// <summary>
/// Construct a new logger in the host.
/// </summary>
/// <param name="minimumLogLevel">The minimum log level to log.</param>
public HostLogger(PsesLogLevel minimumLogLevel)
{
_minimumLogLevel = minimumLogLevel;
_logMessages = new ConcurrentQueue<(PsesLogLevel logLevel, string message)>();
_observers = new ConcurrentDictionary<IObserver<(PsesLogLevel logLevel, string message)>, bool>();
}
/// <summary>
/// Subscribe a new log sink.
/// </summary>
/// <param name="observer">The log sink to subscribe.</param>
/// <returns>A disposable unsubscribe object.</returns>
public IDisposable Subscribe(IObserver<(PsesLogLevel logLevel, string message)> observer)
{
if (observer == null)
{
throw new ArgumentNullException(nameof(observer));
}
_observers[observer] = true;
// Catch up a late subscriber to messages already logged
foreach ((PsesLogLevel logLevel, string message) entry in _logMessages)
{
observer.OnNext(entry);
}
return new Unsubscriber(_observers, observer);
}
/// <summary>
/// Subscribe a new log sink.
/// </summary>
/// <param name="observer">The log sink to subscribe.</param>
/// <returns>A disposable unsubscribe object.</returns>
public IDisposable Subscribe(IObserver<(int logLevel, string message)> observer)
{
if (observer == null)
{
throw new ArgumentNullException(nameof(observer));
}
return Subscribe(new LogObserver(observer));
}
/// <summary>
/// Log a message to log sinks.
/// </summary>
/// <param name="logLevel">The log severity level of message to log.</param>
/// <param name="message">The message to log.</param>
public void Log(PsesLogLevel logLevel, string message)
{
// Do nothing if the severity is lower than the minimum
if (logLevel < _minimumLogLevel)
{
return;
}
// Remember this for later subscriptions
_logMessages.Enqueue((logLevel, message));
// Send this log to all observers
foreach (IObserver<(PsesLogLevel logLevel, string message)> observer in _observers.Keys)
{
observer.OnNext((logLevel, message));
}
}
/// <summary>
/// Convenience method for logging exceptions.
/// </summary>
/// <param name="message">The human-directed message to accompany the exception.</param>
/// <param name="exception">The actual exception to log.</param>
/// <param name="callerName">The name of the calling method.</param>
/// <param name="callerSourceFile">The name of the file where this is logged.</param>
/// <param name="callerLineNumber">The line in the file where this is logged.</param>
public void LogException(
string message,
Exception exception,
[CallerMemberName] string callerName = null,
[CallerFilePath] string callerSourceFile = null,
[CallerLineNumber] int callerLineNumber = -1) => Log(PsesLogLevel.Error, $"{message}. Exception logged in {callerSourceFile} on line {callerLineNumber} in {callerName}:\n{exception}");
}
/// <summary>
/// A log sink to direct log messages back to the PowerShell host.
/// </summary>
/// <remarks>
/// Note that calling this through the cmdlet causes an error,
/// so instead we log directly to the host.
/// Since it's likely that the process will end when PSES shuts down,
/// there's no good reason to need objects rather than writing directly to the host.
/// </remarks>
internal class PSHostLogger : IObserver<(PsesLogLevel logLevel, string message)>
{
private readonly PSHostUserInterface _ui;
/// <summary>
/// Create a new PowerShell host logger.
/// </summary>
/// <param name="ui">The PowerShell host user interface object to log output to.</param>
public PSHostLogger(PSHostUserInterface ui) => _ui = ui;
public void OnCompleted()
{
// No-op since there's nothing to close or dispose,
// we just stop writing to the host
}
public void OnError(Exception error) => OnNext((PsesLogLevel.Error, $"Error occurred while logging: {error}"));
public void OnNext((PsesLogLevel logLevel, string message) value)
{
switch (value.logLevel)
{
case PsesLogLevel.Diagnostic:
_ui.WriteDebugLine(value.message);
return;
case PsesLogLevel.Verbose:
_ui.WriteVerboseLine(value.message);
return;
case PsesLogLevel.Normal:
_ui.WriteLine(value.message);
return;
case PsesLogLevel.Warning:
_ui.WriteWarningLine(value.message);
return;
case PsesLogLevel.Error:
_ui.WriteErrorLine(value.message);
return;
default:
_ui.WriteLine(value.message);
return;
}
}
}
internal class StreamLogger : IObserver<(PsesLogLevel logLevel, string message)>, IDisposable
{
public static StreamLogger CreateWithNewFile(string path)
{
FileStream fileStream = new(
path,
FileMode.Create,
FileAccess.Write,
FileShare.Read,
bufferSize: 4096,
FileOptions.SequentialScan);
return new StreamLogger(new StreamWriter(fileStream, new UTF8Encoding(encoderShouldEmitUTF8Identifier: true)));
}
private readonly StreamWriter _fileWriter;
private readonly BlockingCollection<string> _messageQueue;
private readonly CancellationTokenSource _cancellationSource;
private readonly Thread _writerThread;
// This cannot be a bool
// See https://stackoverflow.com/q/6164751
private int _hasCompleted;
private IDisposable _unsubscriber;
public StreamLogger(StreamWriter streamWriter)
{
streamWriter.AutoFlush = true;
_fileWriter = streamWriter;
_hasCompleted = 0;
_cancellationSource = new CancellationTokenSource();
_messageQueue = new BlockingCollection<string>();
// Start writer listening to queue
_writerThread = new Thread(RunWriter)
{
Name = "PSES Stream Logger Thread",
};
_writerThread.Start();
}
public void OnCompleted()
{
// Ensure we only complete once
if (Interlocked.Exchange(ref _hasCompleted, 1) != 0)
{
return;
}
_cancellationSource.Cancel();
_writerThread.Join();
_unsubscriber.Dispose();
_fileWriter.Flush();
_fileWriter.Close();
_fileWriter.Dispose();
_cancellationSource.Dispose();
_messageQueue.Dispose();
}
public void OnError(Exception error) => OnNext((PsesLogLevel.Error, $"Error occurred while logging: {error}"));
public void OnNext((PsesLogLevel logLevel, string message) value)
{
string message = null;
switch (value.logLevel)
{
case PsesLogLevel.Diagnostic:
message = $"[DBG]: {value.message}";
break;
case PsesLogLevel.Verbose:
message = $"[VRB]: {value.message}";
break;
case PsesLogLevel.Normal:
message = $"[INF]: {value.message}";
break;
case PsesLogLevel.Warning:
message = $"[WRN]: {value.message}";
break;
case PsesLogLevel.Error:
message = $"[ERR]: {value.message}";
break;
}
_messageQueue.Add(message);
}
public void AddUnsubscriber(IDisposable unsubscriber) => _unsubscriber = unsubscriber;
public void Dispose() => OnCompleted();
private void RunWriter()
{
try
{
foreach (string logMessage in _messageQueue.GetConsumingEnumerable(_cancellationSource.Token))
{
_fileWriter.WriteLine(logMessage);
}
}
catch (OperationCanceledException)
{
}
}
}
}