Skip to content

Commit

Permalink
Move impl to System.Native
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Mar 20, 2020
1 parent b109227 commit 9c8abf7
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 27 deletions.
15 changes: 15 additions & 0 deletions src/libraries/Common/src/Interop/iOS/System.Native/Interop.Log.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Licensed to the .NET Foundation under one or more agreements.

This comment has been minimized.

Copy link
@marek-safar

marek-safar Mar 20, 2020

Contributor

This should not be under ios (tvos, macos and others have it too)

// 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 Sys
{
[DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_Log")]
internal static extern unsafe bool Log(IntPtr buffer, int count);

This comment has been minimized.

Copy link
@marek-safar

marek-safar Mar 20, 2020

Contributor

Why is it unsafe?

}
}
10 changes: 10 additions & 0 deletions src/libraries/Native/Unix/System.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ set(NATIVE_SOURCES
pal_sysctl.c
)

if (CLR_CMAKE_TARGET_IOS)
set(NATIVE_SOURCES ${NATIVE_SOURCES} ios/pal_sys.m)
else ()
set(NATIVE_SOURCES ${NATIVE_SOURCES} pal_console.c)
endif ()

if (CLR_CMAKE_TARGET_LINUX)
set(NATIVE_SOURCES ${NATIVE_SOURCES} pal_networkchange.c)

Expand Down Expand Up @@ -62,6 +68,10 @@ add_library(System.Native-Static
${NATIVE_SOURCES}
)

if (CLR_CMAKE_TARGET_IOS)
target_link_libraries(System.Native "-framework Foundation")
endif ()

set_target_properties(System.Native-Static PROPERTIES OUTPUT_NAME System.Native CLEAN_DIRECT_OUTPUT 1)

install (TARGETS System.Native-Static DESTINATION .)
11 changes: 11 additions & 0 deletions src/libraries/Native/Unix/System.Native/ios/pal_sys.h
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.

#pragma once

#include "pal_compiler.h"
#include "pal_types.h"

PALEXPORT void SystemNative_Log(uint8_t* buffer, int32_t length);
57 changes: 57 additions & 0 deletions src/libraries/Native/Unix/System.Native/ios/pal_sys.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

// 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.

#include "pal_sys.h"
#import <Foundation/Foundation.h>

void SystemNative_Log (uint8_t* buffer, int32_t length)
{
// COOP: no managed memory access: any mode.
NSString *msg = [[NSString alloc] initWithBytes: buffer length: length encoding: NSUTF16LittleEndianStringEncoding];

// TODO: TARGET_OS_WATCH is not supported yet
#if TARGET_OS_WATCH && defined (__arm__)

This comment has been minimized.

Copy link
@marek-safar

marek-safar Mar 20, 2020

Contributor

I'd not add code which won't be used

const char* utf8 = [msg UTF8String];
size_t len = strlen (utf8);
fwrite (utf8, 1, len, stdout);
if (len == 0 || utf8 [len - 1] != '\n')
{
fwrite ("\n", 1, 1, stdout);
}
fflush (stdout);
#else
if (length > 4096)
{
// Write in chunks of max 4096 characters; older versions of iOS seems to have a bug where NSLog may hang with long strings (!).
// https://github.com/xamarin/maccore/issues/1014
const char* utf8 = [msg UTF8String];
size_t len = strlen (utf8);
const size_t max_size = 4096;
while (len > 0)
{
size_t chunk_size = len > max_size ? max_size : len;

// Try to not break in the middle of a line, by looking backwards for a newline
while (chunk_size > 0 && utf8 [chunk_size] != 0 && utf8 [chunk_size] != '\n')
{
chunk_size--;
}
if (chunk_size == 0)
{
// No newline found, break in the middle.
chunk_size = len > max_size ? max_size : len;
}
NSLog (@"%.*s", (int) chunk_size, utf8);
len -= chunk_size;
utf8 += chunk_size;
}
}
else
{
NSLog (@"%@", msg);
}
#endif
[msg release];
}
6 changes: 6 additions & 0 deletions src/libraries/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<!-- iOS -->
<ItemGroup Condition="'$(TargetsiOS)' == 'true'">
<Compile Include="System\ConsolePal.iOS.cs" />
<Compile Include="$(CommonPath)Interop\iOS\System.Native\Interop.Log.cs">
<Link>Common\Interop\iOS\Interop.iOS.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs">
<Link>Common\Interop\Unix\Interop.Libraries.cs</Link>
</Compile>
</ItemGroup>
<!-- Windows -->
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
Expand Down
41 changes: 14 additions & 27 deletions src/libraries/System.Console/src/System/ConsolePal.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,27 @@

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

namespace System
{
internal class NSLogStream : Stream

This comment has been minimized.

Copy link
@marek-safar

marek-safar Mar 20, 2020

Contributor

Should be sealed

{
private bool _error;

public NSLogStream(bool error)
{
_error = error;
}

public override void Flush() { }

public override int Read(byte[] buffer, int offset, int count) => throw new PlatformNotSupportedException();
public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException();

public override long Seek(long offset, SeekOrigin origin) => throw new PlatformNotSupportedException();
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();

public override void SetLength(long value) => throw new PlatformNotSupportedException();

// call NSLog
[DllImport("__Internal")]
private static unsafe extern void mono_log(byte* str, int count, bool isError);
public override void SetLength(long value) => throw new NotSupportedException();

public override unsafe void Write(byte[] buffer, int offset, int count)
{
fixed (byte* ptr = buffer)
if (count > 0 && buffer.Length >= offset + count)
{
mono_log(ptr + offset, count, _error);
fixed (byte* ptr = buffer)
{
Interop.Sys.Log((IntPtr)(ptr + offset), count);
}
}
}

Expand All @@ -43,39 +34,35 @@ public override unsafe void Write(byte[] buffer, int offset, int count)

public override bool CanWrite => true;

public override long Length => throw new PlatformNotSupportedException();
public override long Length => throw new NotSupportedException();

public override long Position
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
}

// Provides Windows-based support for System.Console.
internal static class ConsolePal
{
public static Stream OpenStandardInput() => throw new PlatformNotSupportedException();

public static Stream OpenStandardOutput() => new NSLogStream(false);
public static Stream OpenStandardOutput() => new NSLogStream();

public static Stream OpenStandardError() => new NSLogStream(true);
public static Stream OpenStandardError() => new NSLogStream();

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

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

public static Encoding OutputEncoding => Encoding.UTF8;
public static Encoding OutputEncoding => Encoding.Unicode;

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

/// <summary>Gets whether Console.In is targeting a terminal display.</summary>
public static bool IsInputRedirectedCore() => throw new PlatformNotSupportedException();

/// <summary>Gets whether Console.Out is targeting a terminal display.</summary>
public static bool IsOutputRedirectedCore() => throw new PlatformNotSupportedException();

/// <summary>Gets whether Console.In is targeting a terminal display.</summary>
public static bool IsErrorRedirectedCore() => throw new PlatformNotSupportedException();

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

0 comments on commit 9c8abf7

Please sign in to comment.