Skip to content

Commit

Permalink
[mono] Implement System.Console for Android (dotnet#35415)
Browse files Browse the repository at this point in the history
* Implement ConsolePal for Android

Co-authored-by: Stephen Toub <[email protected]>
  • Loading branch information
EgorBo and stephentoub authored Apr 27, 2020
1 parent 8c39b7e commit f283945
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/libraries/Common/src/Interop/Android/Interop.Libraries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

internal static partial class Interop
{
internal static partial class Libraries
{
internal const string Liblog = "liblog";
}
}
31 changes: 31 additions & 0 deletions src/libraries/Common/src/Interop/Android/Interop.Logcat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class Logcat
{
[DllImport(Libraries.Liblog)]
private static extern void __android_log_print(LogLevel level, string? tag, string format, string args, IntPtr ptr);

internal static void AndroidLogPrint(LogLevel level, string? tag, string message) =>
__android_log_print(level, tag, "%s", message, IntPtr.Zero);

internal enum LogLevel
{
Unknown = 0x00,
Default = 0x01,
Verbose = 0x02,
Debug = 0x03,
Info = 0x04,
Warn = 0x05,
Error = 0x06,
Fatal = 0x07,
Silent = 0x08
}
}
}
14 changes: 12 additions & 2 deletions src/libraries/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<RootNamespace>System.Console</RootNamespace>
<AssemblyName>System.Console</AssemblyName>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent)-Windows_NT;$(NetCoreAppCurrent)-Unix;$(NetCoreAppCurrent)-Android;$(NetCoreAppCurrent)-iOS;$(NetCoreAppCurrent)-tvOS</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
Expand All @@ -28,6 +28,16 @@
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
</ItemGroup>
<!-- Android -->
<ItemGroup Condition="'$(TargetsAndroid)' == 'true'">
<Compile Include="System\ConsolePal.Android.cs" />
<Compile Include="$(CommonPath)Interop\Android\Interop.Logcat.cs">
<Link>Common\Interop\Android\Interop.Logcat.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Android\Interop.Libraries.cs">
<Link>Common\Interop\Android\Interop.Libraries.cs</Link>
</Compile>
</ItemGroup>
<!-- Windows -->
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="System\ConsolePal.Windows.cs" />
Expand Down Expand Up @@ -125,7 +135,7 @@
Link="Common\System\Text\ValueStringBuilder.cs" />
</ItemGroup>
<!-- Unix -->
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' and '$(TargetsiOS)' != 'true' and '$(TargetstvOS)' != 'true'">
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' and '$(TargetsAndroid)' != 'true' and '$(TargetsiOS)' != 'true' and '$(TargetstvOS)' != 'true'">
<Compile Include="System\ConsolePal.Unix.cs" />
<Compile Include="System\TermInfo.cs" />
<Compile Include="System\IO\StdInReader.cs" />
Expand Down
168 changes: 168 additions & 0 deletions src/libraries/System.Console/src/System/ConsolePal.Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.IO;
using System.Text;
using System.Runtime.InteropServices;

namespace System
{
internal sealed unsafe class LogcatStream : ConsoleStream
{
public LogcatStream() : base(FileAccess.Write) {}

public override int Read(byte[] buffer, int offset, int count) => throw Error.GetReadNotSupported();

public override unsafe void Write(byte[] buffer, int offset, int count)
{
string log = ConsolePal.OutputEncoding.GetString(buffer, offset, count);
Interop.Logcat.AndroidLogPrint(Interop.Logcat.LogLevel.Info, null, log);
}
}

internal static class ConsolePal
{
internal static void EnsureConsoleInitialized() { }

public static Stream OpenStandardInput() => throw new PlatformNotSupportedException();

public static Stream OpenStandardOutput() => new LogcatStream();

public static Stream OpenStandardError() => new LogcatStream();

public static Encoding InputEncoding => throw new PlatformNotSupportedException();

public static void SetConsoleInputEncoding(Encoding enc) => throw new PlatformNotSupportedException();

public static Encoding OutputEncoding => Encoding.Unicode;

public static void SetConsoleOutputEncoding(Encoding enc) => throw new PlatformNotSupportedException();

public static bool IsInputRedirectedCore() => false;

public static bool IsOutputRedirectedCore() => false;

public static bool IsErrorRedirectedCore() => false;

internal static TextReader GetOrCreateReader() => throw new PlatformNotSupportedException();

public static bool NumberLock => false;

public static bool CapsLock => false;

public static bool KeyAvailable => false;

public static ConsoleKeyInfo ReadKey(bool intercept) => throw new PlatformNotSupportedException();

public static bool TreatControlCAsInput
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static ConsoleColor BackgroundColor
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static ConsoleColor ForegroundColor
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static void ResetColor() => throw new PlatformNotSupportedException();

public static int CursorSize
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static bool CursorVisible
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static int CursorLeft => throw new PlatformNotSupportedException();

public static int CursorTop => throw new PlatformNotSupportedException();

public static string Title
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static void Beep() => throw new PlatformNotSupportedException();

public static void Beep(int frequency, int duration) => throw new PlatformNotSupportedException();

public static void MoveBufferArea(int sourceLeft, int sourceTop,
int sourceWidth, int sourceHeight, int targetLeft, int targetTop,
char sourceChar, ConsoleColor sourceForeColor,
ConsoleColor sourceBackColor) => throw new PlatformNotSupportedException();

public static void Clear() => throw new PlatformNotSupportedException();

public static void SetCursorPosition(int left, int top) => throw new PlatformNotSupportedException();

public static int BufferWidth
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static int BufferHeight
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static void SetBufferSize(int width, int height) => throw new PlatformNotSupportedException();

public static int LargestWindowWidth => throw new PlatformNotSupportedException();

public static int LargestWindowHeight => throw new PlatformNotSupportedException();

public static int WindowLeft
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static int WindowTop
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static int WindowWidth
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static int WindowHeight
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static void SetWindowPosition(int left, int top) => throw new PlatformNotSupportedException();

public static void SetWindowSize(int width, int height) => throw new PlatformNotSupportedException();

internal sealed class ControlCHandlerRegistrar
{
internal ControlCHandlerRegistrar() => throw new PlatformNotSupportedException();

internal void Register() => throw new PlatformNotSupportedException();

internal void Unregister() => throw new PlatformNotSupportedException();
}
}
}

0 comments on commit f283945

Please sign in to comment.