From 94ca4e7a03aa274cb32e1e90278f5620d57017ef Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Thu, 22 Apr 2021 11:57:41 +0200 Subject: [PATCH] [ObjCRuntime] Print to stderr if using xamarin_log fails. Fixes #11239. (#11262) * [ObjCRuntime] Print to stderr if using xamarin_log fails. Fixes #11239. Fixes https://github.com/xamarin/xamarin-macios/issues/11239. * Update xtro. --- src/ObjCRuntime/Runtime.CoreCLR.cs | 2 +- src/ObjCRuntime/Runtime.cs | 20 ++++++++++++++++--- .../xtro-sharpie/common-CoreFoundation.ignore | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/ObjCRuntime/Runtime.CoreCLR.cs b/src/ObjCRuntime/Runtime.CoreCLR.cs index 5be9151b0c..c27794f20b 100644 --- a/src/ObjCRuntime/Runtime.CoreCLR.cs +++ b/src/ObjCRuntime/Runtime.CoreCLR.cs @@ -27,7 +27,7 @@ namespace ObjCRuntime { [System.Diagnostics.Conditional ("UNDEFINED")] static void log_coreclr (string message) { - xamarin_log (message); + NSLog (message); } // Returns a retained MonoObject. Caller must release. diff --git a/src/ObjCRuntime/Runtime.cs b/src/ObjCRuntime/Runtime.cs index 5faa213e17..b801be966d 100644 --- a/src/ObjCRuntime/Runtime.cs +++ b/src/ObjCRuntime/Runtime.cs @@ -14,6 +14,7 @@ using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Text; using Foundation; using Registrar; @@ -1617,16 +1618,29 @@ namespace ObjCRuntime { } [DllImport ("__Internal", CharSet = CharSet.Unicode)] - internal extern static void xamarin_log (string s); + extern static void xamarin_log (string s); + + [DllImport (Constants.libcLibrary)] + extern static nint write (int filedes, byte[] buf, nint nbyte); internal static void NSLog (string value) { - xamarin_log (value); + try { + xamarin_log (value); + } catch { + // Append a newline like NSLog does + if (!value.EndsWith ('\n')) + value += "\n"; + // Don't use Console.WriteLine, since that brings in a lot of supporting code and may bloat apps. + var utf8 = Encoding.UTF8.GetBytes (value); + write (2 /* STDERR */, utf8, utf8.Length); + // Ignore any errors writing to stderr (might happen on devices if the developer tools haven't been mounted, but xamarin_log should always work on devices). + } } internal static void NSLog (string format, params object[] args) { - xamarin_log (string.Format (format, args)); + NSLog (string.Format (format, args)); } #endif // !COREBUILD diff --git a/tests/xtro-sharpie/common-CoreFoundation.ignore b/tests/xtro-sharpie/common-CoreFoundation.ignore index 05a20c8878..780044da1a 100644 --- a/tests/xtro-sharpie/common-CoreFoundation.ignore +++ b/tests/xtro-sharpie/common-CoreFoundation.ignore @@ -981,6 +981,7 @@ !unknown-pinvoke! dispatch_walltime bound !unknown-pinvoke! dispatch_write bound !unknown-pinvoke! open bound +!unknown-pinvoke! write bound !missing-field! kCFStreamErrorDomainSOCKS not bound !missing-field! kCFStreamErrorDomainSSL not bound !missing-field! kCFStreamPropertyShouldCloseNativeSocket not bound