[csharp]Cleaner implementation on YogaLogger

This commit is contained in:
Rui Marinho
2017-02-10 22:12:24 +00:00
parent 199975c5bf
commit 7cbfecd6a9

View File

@@ -8,17 +8,14 @@
*/ */
using System; using System;
using System.Runtime.InteropServices;
namespace Facebook.Yoga namespace Facebook.Yoga
{ {
internal static class YogaLogger internal static class YogaLogger
{ {
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Func(YogaLogLevel level, string message); public delegate void Func(YogaLogLevel level, string message);
private static bool _initialized; private static bool _initialized;
private static Func _managedLogger = null;
public static Func Logger = null; public static Func Logger = null;
@@ -26,42 +23,27 @@ namespace Facebook.Yoga
{ {
if (!_initialized) if (!_initialized)
{ {
_managedLogger = (level, message) => {
if (Logger != null)
{
Logger(level, message);
}
if (level == YogaLogLevel.Error) Native.YGInteropSetLogger(LoggerInternal);
{
throw new InvalidOperationException(message);
}
};
#if __IOS__
Native.YGInteropSetLogger(ManagedLogger);
#else
Native.YGInteropSetLogger(_managedLogger);
#endif
_initialized = true; _initialized = true;
} }
} }
#if __IOS__ #if __IOS__
delegate void ManagedLoggerCallback(YogaLogLevel level, string message); [ObjCRuntime.MonoPInvokeCallback(typeof(Func))]
[ObjCRuntime.MonoPInvokeCallback(typeof(ManagedLoggerCallback))]
public static void ManagedLogger(YogaLogLevel level, string message)
{
if (Logger != null)
{
Logger(level, message);
}
if (level == YogaLogLevel.Error)
{
throw new InvalidOperationException(message);
}
}
#endif #endif
public static void LoggerInternal(YogaLogLevel level, string message)
{
if (Logger != null)
{
Logger(level, message);
}
if (level == YogaLogLevel.Error)
{
throw new InvalidOperationException(message);
}
}
} }
} }