ZibraLiquidDebug.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using AOT;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. using com.zibra.liquid.Bridge;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif // UNITY_EDITOR
  9. namespace com.zibra.liquid.Solver
  10. {
  11. #if ZIBRA_EFFECTS_DEBUG
  12. #if UNITY_EDITOR
  13. [InitializeOnLoad]
  14. #endif
  15. public static class ZibraLiquidDebug
  16. {
  17. public enum LogLevel
  18. {
  19. Verbose = 0,
  20. Info = 1,
  21. Performance = 2,
  22. Warning = 3,
  23. Error = 4,
  24. }
  25. [StructLayout(LayoutKind.Sequential)]
  26. public struct DebugMessage
  27. {
  28. public IntPtr Text;
  29. public ZibraLiquidDebug.LogLevel Level;
  30. };
  31. [StructLayout(LayoutKind.Sequential)]
  32. public struct LoggerSettings
  33. {
  34. public IntPtr PFNCallback;
  35. public ZibraLiquidDebug.LogLevel LogLevel;
  36. };
  37. public static string EditorPrefsKey = "ZibraLiquidLogLevel";
  38. public static LogLevel CurrentLogLevel;
  39. public static void SetLogLevel(LogLevel level)
  40. {
  41. CurrentLogLevel = level;
  42. #if UNITY_EDITOR
  43. EditorPrefs.SetInt(EditorPrefsKey, (int)level);
  44. #endif // UNITY_EDITOR
  45. InitializeDebug();
  46. }
  47. static ZibraLiquidDebug()
  48. {
  49. #if UNITY_EDITOR
  50. CurrentLogLevel = (LogLevel)EditorPrefs.GetInt(EditorPrefsKey, (int)LogLevel.Error);
  51. #else
  52. CurrentLogLevel = LogLevel.Error;
  53. #endif // UNITY_EDITOR
  54. }
  55. #if UNITY_EDITOR
  56. [InitializeOnLoadMethod]
  57. #endif
  58. static void InitializeDebug()
  59. {
  60. DebugLogCallbackT callbackDelegate = new DebugLogCallbackT(DebugLogCallback);
  61. var settings = new LoggerSettings();
  62. settings.PFNCallback = Marshal.GetFunctionPointerForDelegate(callbackDelegate);
  63. settings.LogLevel = CurrentLogLevel;
  64. IntPtr settingsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(settings));
  65. Marshal.StructureToPtr(settings, settingsPtr, true);
  66. // ZibraLiquid_SetDebugLogWrapperPointer(settingsPtr);
  67. Marshal.FreeHGlobal(settingsPtr);
  68. }
  69. [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
  70. delegate void DebugLogCallbackT(IntPtr message);
  71. [MonoPInvokeCallback(typeof(DebugLogCallbackT))]
  72. static void DebugLogCallback(IntPtr request)
  73. {
  74. DebugMessage message = Marshal.PtrToStructure<DebugMessage>(request);
  75. string text = Marshal.PtrToStringAnsi(message.Text);
  76. switch (message.Level)
  77. {
  78. case LogLevel.Verbose:
  79. Debug.Log("ZibraLiquid[verbose]: " + text);
  80. break;
  81. case LogLevel.Info:
  82. Debug.Log("ZibraLiquid: " + text);
  83. break;
  84. case LogLevel.Warning:
  85. Debug.LogWarning(text);
  86. break;
  87. case LogLevel.Performance:
  88. Debug.LogWarning("ZibraLiquid | Performance Warning:" + text);
  89. break;
  90. case LogLevel.Error:
  91. Debug.LogError("ZibraLiquid" + text);
  92. break;
  93. default:
  94. Debug.LogError("ZibraLiquid | Incorrect native log data format.");
  95. break;
  96. }
  97. }
  98. // [DllImport(LiquidBridge.PluginLibraryName)]
  99. // static extern void ZibraLiquid_SetDebugLogWrapperPointer(IntPtr callback);
  100. }
  101. #endif
  102. }