powershell-es/test/PowerShellEditorServices.Test.E2E/Hosts/DebugOutputStream.cs
fwastring baa0056244
Some checks are pending
CI Tests / dotnet (push) Waiting to run
CI Tests / dotnet-1 (push) Waiting to run
CI Tests / dotnet-2 (push) Waiting to run
Emacs End-to-End Tests / ert (push) Waiting to run
Vim End-to-End Tests / themis (push) Waiting to run
initial
2026-02-17 13:06:31 +01:00

43 lines
1.3 KiB
C#

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System.Diagnostics;
using System.IO;
using System.Text;
using Nerdbank.Streams;
namespace PowerShellEditorServices.Test.E2E;
/// <summary>
/// A stream that logs all data read and written to the debug stream which is visible in the debug console when a
/// debugger is attached.
/// </summary>
internal class DebugOutputStream : MonitoringStream
{
public DebugOutputStream(Stream? underlyingStream)
: base(underlyingStream ?? new MemoryStream())
{
DidRead += (_, segment) =>
{
if (segment.Array is null) { return; }
LogData("⬅️", segment.Array, segment.Offset, segment.Count);
};
DidWrite += (_, segment) =>
{
if (segment.Array is null) { return; }
LogData("➡️", segment.Array, segment.Offset, segment.Count);
};
}
private static void LogData(string header, byte[] buffer, int offset, int count)
{
// If debugging, the raw traffic will be visible in the debug console
if (Debugger.IsAttached)
{
string data = Encoding.UTF8.GetString(buffer, offset, count);
Debug.WriteLine($"{header} {data}");
}
}
}