Introduce CSSLayoutSetLogger to pass the print result to C# side

Summary:
- CSSLayoutSetLogger
  - Problem: Unity or other logging system can't use printf output
  - Solution: Add CSSLogger to pass CSSNodePrint result to UnityEngine.Debug.Log or other logging system via CSSLogger function for debugging purpose

Reviewed By: emilsjolander

Differential Revision: D4027044

fbshipit-source-id: 90e2e449260888770f71fa7ea790ca9764d91c44
This commit is contained in:
Kazuki Sakamoto
2016-10-19 11:01:24 -07:00
committed by Facebook Github Bot
parent e4ad7d3c12
commit daed6f5b8a
11 changed files with 347 additions and 229 deletions

View File

@@ -16,14 +16,16 @@ namespace Facebook.CSSLayout
public delegate void FailFunc(string message);
private static bool _assertInitialized;
private static FailFunc _failFunc;
public static void Initialize()
{
if (!_assertInitialized)
{
Native.CSSAssertSetFailFunc((message) => {
_failFunc = (message) => {
throw new InvalidOperationException(message);
});
};
Native.CSSAssertSetFailFunc(_failFunc);
_assertInitialized = true;
}
}

View File

@@ -0,0 +1,38 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
namespace Facebook.CSSLayout
{
internal static class CSSLogger
{
public delegate void Func(string message);
private static bool _initialized;
private static Func _managedLogger = null;
public static Func Logger = null;
public static void Initialize()
{
if (!_initialized)
{
_managedLogger = (message) => {
if (Logger != null)
{
Logger(message);
}
};
Native.CSSInteropSetLogger(_managedLogger);
_initialized = true;
}
}
}
}

View File

@@ -11,6 +11,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace Facebook.CSSLayout
{
@@ -110,9 +111,9 @@ namespace Facebook.CSSLayout
}
CSSAssert.Initialize();
CSSLogger.Initialize();
_cssNode = Native.CSSNodeNew();
_children = new List<CSSNode>(4);
Native.CSSNodeSetPrintFunc(_cssNode, PrintInternal);
}
public void Free()
@@ -704,9 +705,14 @@ namespace Facebook.CSSLayout
return new CSSSize { width = measureResult.Width, height = measureResult.Height };
}
private void PrintInternal(IntPtr context)
public string Print(CSSPrintOptions options = CSSPrintOptions.Layout|CSSPrintOptions.Style|CSSPrintOptions.Children)
{
System.Diagnostics.Debug.WriteLine(ToString());
AssertNativeInstance();
StringBuilder sb = new StringBuilder();
CSSLogger.Logger = (message) => {sb.Append(message);};
Native.CSSNodePrint(_cssNode, options);
CSSLogger.Logger = null;
return sb.ToString();
}
public IEnumerator<CSSNode> GetEnumerator()

View File

@@ -1,15 +0,0 @@
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
using System;
namespace Facebook.CSSLayout
{
public delegate void CSSPrintFunc(IntPtr context);
}

View File

@@ -20,6 +20,9 @@ namespace Facebook.CSSLayout
private const string DllName = "CSSLayout";
#endif
[DllImport(DllName)]
public static extern void CSSInteropSetLogger(CSSLogger.Func func);
[DllImport(DllName)]
public static extern void CSSAssertSetFailFunc(CSSAssert.FailFunc func);
@@ -81,12 +84,6 @@ namespace Facebook.CSSLayout
[DllImport(DllName)]
public static extern CSSMeasureFunc CSSNodeGetMeasureFunc(IntPtr node);
[DllImport(DllName)]
public static extern void CSSNodeSetPrintFunc(IntPtr node, CSSPrintFunc printFunc);
[DllImport(DllName)]
public static extern CSSPrintFunc CSSNodeGePrintFunc(IntPtr node);
[DllImport(DllName)]
public static extern void CSSNodeSetIsTextnode(IntPtr node, [MarshalAs(UnmanagedType.I1)] bool isTextNode);