Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Console.Write on iOS #56713

Merged
merged 33 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
71b3888
Fix Console.Write on iOS
MaximLipnin Aug 2, 2021
8f6f7bc
Move the helper method to iOS-specific location
MaximLipnin Aug 2, 2021
a97f0d5
Add a test
MaximLipnin Aug 2, 2021
90bf33c
Remove unused using
MaximLipnin Aug 2, 2021
e6806e4
Apply Ralf's suggestion
MaximLipnin Aug 2, 2021
2bdb6e7
Move Interop.Sys.Log to a local function
MaximLipnin Aug 2, 2021
612dc1c
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 3, 2021
6f8542b
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 9, 2021
47d8f01
Move the caching logic over to StreamWriter
MaximLipnin Aug 10, 2021
4f0653a
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 10, 2021
14545a8
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 11, 2021
725e64c
feedback
MaximLipnin Aug 11, 2021
5b8499c
Remove unused usings
MaximLipnin Aug 12, 2021
61bb68b
Derive from StreamWriter
MaximLipnin Aug 12, 2021
68e10e7
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 12, 2021
ac45868
Feedback
MaximLipnin Aug 12, 2021
2e57d54
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 16, 2021
c8aa30f
Try a solution not changing public API surface and using NSLogStream …
MaximLipnin Aug 16, 2021
b76ddfe
Some feedback
MaximLipnin Aug 17, 2021
b999fff
Use ArrayPool
MaximLipnin Aug 17, 2021
49f5c41
Not rely on the length of the rented array
MaximLipnin Aug 17, 2021
aaa1f35
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 18, 2021
90da2a0
Address the feedback
MaximLipnin Aug 27, 2021
88685c7
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 27, 2021
976954b
not use _ prefix for local variable
MaximLipnin Aug 27, 2021
245edb3
Address the feedback
MaximLipnin Aug 27, 2021
a234df5
Merge branch 'fix_console_write_on_ios' of https://github.com/maximli…
MaximLipnin Aug 27, 2021
34d215f
Fix an error
MaximLipnin Aug 30, 2021
1eb7528
Merge branch 'main' into fix_console_write_on_ios
MaximLipnin Aug 30, 2021
a2e118e
Print a span rather than a string
MaximLipnin Aug 30, 2021
732a62f
Slice the char span to the exact used length
MaximLipnin Sep 1, 2021
9375848
Address Stephen's feedback
MaximLipnin Sep 1, 2021
7504d30
Remove a redundant using
MaximLipnin Sep 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/libraries/System.Console/src/System/ConsolePal.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
MaximLipnin marked this conversation as resolved.
Show resolved Hide resolved
using System.Text;

namespace System
{
internal sealed class NSLogStream : ConsoleStream
{
private readonly StringBuilder _buffer = new StringBuilder();

public NSLogStream() : base(FileAccess.Write) {}

public override int Read(Span<byte> buffer) => throw Error.GetReadNotSupported();

public override unsafe void Write(ReadOnlySpan<byte> buffer)
{
fixed (byte* ptr = buffer)
ReadOnlySpan<char> charSpan = MemoryMarshal.Cast<byte, char>(buffer);
MaximLipnin marked this conversation as resolved.
Show resolved Hide resolved

lock (_buffer)
{
Interop.Sys.Log(ptr, buffer.Length);
AccumulateNewLines(_buffer, charSpan, line =>
{
fixed (char* ptr = line)
{
Interop.Sys.Log((byte*)ptr, line.Length * 2);
MaximLipnin marked this conversation as resolved.
Show resolved Hide resolved
}
});
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/libraries/System.Console/src/System/IO/ConsoleStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
MaximLipnin marked this conversation as resolved.
Show resolved Hide resolved

namespace System.IO
{
Expand Down Expand Up @@ -90,5 +91,37 @@ protected void ValidateWrite(byte[] buffer, int offset, int count)
throw Error.GetWriteNotSupported();
}
}

protected static void AccumulateNewLines(StringBuilder accumulator, ReadOnlySpan<char> buffer, Action<string> printer)
MaximLipnin marked this conversation as resolved.
Show resolved Hide resolved
{
int lineStartIndex = 0;
for (int i = 0; i < buffer.Length; i++)
{
if (buffer[i] == '\n')
{
ReadOnlySpan<char> sliceWithNl = buffer.Slice(lineStartIndex, i - lineStartIndex);
if (accumulator.Length > 0)
{
// we found a new line, append content from accumulator if it's not empty
accumulator.Append(sliceWithNl);
printer(accumulator.ToString());
accumulator.Clear();
}
else
{
// accumulator is empty - print the line as it is
printer(sliceWithNl.ToString());
}
lineStartIndex = i + 1;
}
}

if (buffer.Length > 0 && buffer[^1] != '\n')
{
// add a line without '\n' to accumulator
ReadOnlySpan<char> appendix = buffer.Slice(lineStartIndex, buffer.Length - lineStartIndex);
accumulator.Append(appendix);
}
}
}
}