UnityLogWriter.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Reflection;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using BepInEx.ConsoleUtil;
  6. using UnityEngine;
  7. namespace BepInEx.Logging
  8. {
  9. /// <summary>
  10. /// Logs entries using Unity specific outputs.
  11. /// </summary>
  12. public class UnityLogWriter : BaseLogger
  13. {
  14. private static readonly Action<string> WriteStringToUnityLog;
  15. [DllImport("mono.dll", EntryPoint = "mono_lookup_internal_call")]
  16. private static extern IntPtr MonoLookupInternalCall(IntPtr gconstpointer);
  17. static UnityLogWriter()
  18. {
  19. foreach (MethodInfo methodInfo in typeof(UnityEngine.UnityLogWriter).GetMethods(BindingFlags.Static | BindingFlags.Public))
  20. {
  21. if (MonoLookupInternalCall(methodInfo.MethodHandle.Value) == IntPtr.Zero)
  22. continue;
  23. WriteStringToUnityLog = (Action<string>) Delegate.CreateDelegate(typeof(Action<string>), methodInfo);
  24. break;
  25. }
  26. }
  27. /// <summary>
  28. /// Writes a string specifically to the game output log.
  29. /// </summary>
  30. /// <param name="value">The value to write.</param>
  31. public void WriteToLog(string value)
  32. {
  33. WriteStringToUnityLog?.Invoke(value);
  34. }
  35. protected void InternalWrite(string value)
  36. {
  37. Console.Write(value);
  38. WriteToLog(value);
  39. }
  40. /// <summary>
  41. /// Logs an entry to the Logger instance.
  42. /// </summary>
  43. /// <param name="level">The level of the entry.</param>
  44. /// <param name="entry">The textual value of the entry.</param>
  45. public override void Log(LogLevel level, object entry)
  46. {
  47. Kon.ForegroundColor = level.GetConsoleColor();
  48. base.Log(level, entry);
  49. Kon.ForegroundColor = ConsoleColor.Gray;
  50. // If the display level got ignored, still write it to the log
  51. if ((DisplayedLevels & level) == LogLevel.None)
  52. WriteToLog($"[{level.GetHighestLevel()}] {entry}\r\n");
  53. }
  54. public override void WriteLine(string value) => InternalWrite($"{value}\r\n");
  55. public override void Write(char value) => InternalWrite(value.ToString());
  56. public override void Write(string value) => InternalWrite(value);
  57. /// <summary>
  58. /// Start listening to Unity's log message events and sending the messages to BepInEx logger.
  59. /// </summary>
  60. public static void ListenUnityLogs()
  61. {
  62. Type application = typeof(Application);
  63. EventInfo logEvent = application.GetEvent("logMessageReceived", BindingFlags.Public | BindingFlags.Static);
  64. if (logEvent != null)
  65. {
  66. logEvent.AddEventHandler(null, new Application.LogCallback(OnUnityLogMessageReceived));
  67. }
  68. else
  69. {
  70. MethodInfo registerLogCallback = application.GetMethod("RegisterLogCallback", BindingFlags.Public | BindingFlags.Static);
  71. registerLogCallback.Invoke(null, new object[] { new Application.LogCallback(OnUnityLogMessageReceived) });
  72. }
  73. }
  74. private static void OnUnityLogMessageReceived(string message, string stackTrace, LogType type)
  75. {
  76. LogLevel logLevel = LogLevel.Message;
  77. switch (type)
  78. {
  79. case LogType.Error:
  80. case LogType.Assert:
  81. case LogType.Exception:
  82. logLevel = LogLevel.Error;
  83. break;
  84. case LogType.Warning:
  85. logLevel = LogLevel.Warning;
  86. break;
  87. case LogType.Log:
  88. default:
  89. logLevel = LogLevel.Info;
  90. break;
  91. }
  92. Logger.Log(logLevel, message);
  93. if (type == LogType.Exception)
  94. {
  95. Logger.Log(logLevel, $"Stack trace:\n{stackTrace}");
  96. }
  97. }
  98. }
  99. }
  100. namespace UnityEngine
  101. {
  102. internal sealed class UnityLogWriter
  103. {
  104. [MethodImpl(MethodImplOptions.InternalCall)]
  105. public static extern void WriteStringToUnityLogImpl(string s);
  106. [MethodImpl(MethodImplOptions.InternalCall)]
  107. public static extern void WriteStringToUnityLog(string s);
  108. }
  109. }