UnityLogWriter.cs 4.2 KB

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