UnityLogListener.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using BepInEx.Logging;
  2. using System;
  3. using System.Reflection;
  4. using System.Runtime.CompilerServices;
  5. namespace BepInEx.Unity.Logging
  6. {
  7. /// <summary>
  8. /// Logs entries using Unity specific outputs.
  9. /// </summary>
  10. public class UnityLogListener : ILogListener
  11. {
  12. internal static readonly Action<string> WriteStringToUnityLog;
  13. static UnityLogListener()
  14. {
  15. foreach (MethodInfo methodInfo in typeof(UnityEngine.UnityLogWriter).GetMethods(BindingFlags.Static | BindingFlags.Public))
  16. {
  17. try
  18. {
  19. methodInfo.Invoke(null, new object[] { "" });
  20. }
  21. catch
  22. {
  23. continue;
  24. }
  25. WriteStringToUnityLog = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), methodInfo);
  26. break;
  27. }
  28. if (WriteStringToUnityLog == null)
  29. Logger.LogError("Unable to start Unity log writer");
  30. }
  31. public void LogEvent(object sender, LogEventArgs eventArgs)
  32. {
  33. if (eventArgs.Source is UnityLogSource)
  34. return;
  35. WriteStringToUnityLog?.Invoke(eventArgs.ToStringLine());
  36. }
  37. public void Dispose() { }
  38. }
  39. }
  40. namespace UnityEngine
  41. {
  42. internal sealed class UnityLogWriter
  43. {
  44. [MethodImpl(MethodImplOptions.InternalCall)]
  45. public static extern void WriteStringToUnityLogImpl(string s);
  46. [MethodImpl(MethodImplOptions.InternalCall)]
  47. public static extern void WriteStringToUnityLog(string s);
  48. }
  49. }