UnityLogWriter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. /// <summary>
  14. /// Writes a string specifically to the game output log.
  15. /// </summary>
  16. /// <param name="value">The value to write.</param>
  17. public void WriteToLog(string value)
  18. {
  19. #if UNITY_2018
  20. UnityEngine.UnityLogWriter.WriteStringToUnityLogImpl(value);
  21. #else
  22. UnityEngine.UnityLogWriter.WriteStringToUnityLog(value);
  23. #endif
  24. }
  25. protected void InternalWrite(string value)
  26. {
  27. Console.Write(value);
  28. WriteToLog(value);
  29. }
  30. /// <summary>
  31. /// Logs an entry to the Logger instance.
  32. /// </summary>
  33. /// <param name="level">The level of the entry.</param>
  34. /// <param name="entry">The textual value of the entry.</param>
  35. public override void Log(LogLevel level, object entry)
  36. {
  37. Kon.ForegroundColor = level.GetConsoleColor();
  38. base.Log(level, entry);
  39. Kon.ForegroundColor = ConsoleColor.Gray;
  40. }
  41. public override void WriteLine(string value) => InternalWrite($"{value}\r\n");
  42. public override void Write(char value) => InternalWrite(value.ToString());
  43. public override void Write(string value) => InternalWrite(value);
  44. /// <summary>
  45. /// Start listening to Unity's log message events and sending the messages to BepInEx logger.
  46. /// </summary>
  47. public static void ListenUnityLogs()
  48. {
  49. Type application = typeof(Application);
  50. EventInfo logEvent = application.GetEvent("logMessageReceived", BindingFlags.Public | BindingFlags.Static);
  51. if (logEvent != null)
  52. {
  53. logEvent.AddEventHandler(null, new Application.LogCallback(OnUnityLogMessageReceived));
  54. }
  55. else
  56. {
  57. MethodInfo registerLogCallback = application.GetMethod("RegisterLogCallback", BindingFlags.Public | BindingFlags.Static);
  58. registerLogCallback.Invoke(null, new object[] { new Application.LogCallback(OnUnityLogMessageReceived) });
  59. }
  60. }
  61. private static void OnUnityLogMessageReceived(string message, string stackTrace, LogType type)
  62. {
  63. LogLevel logLevel = LogLevel.Message;
  64. switch (type)
  65. {
  66. case LogType.Error:
  67. case LogType.Assert:
  68. case LogType.Exception:
  69. logLevel = LogLevel.Error;
  70. break;
  71. case LogType.Warning:
  72. logLevel = LogLevel.Warning;
  73. break;
  74. case LogType.Log:
  75. default:
  76. logLevel = LogLevel.Message;
  77. break;
  78. }
  79. Logger.Log(logLevel, message);
  80. if (type == LogType.Exception)
  81. {
  82. Logger.Log(logLevel, $"Stack trace:\n{stackTrace}");
  83. }
  84. }
  85. }
  86. }
  87. namespace UnityEngine
  88. {
  89. internal sealed class UnityLogWriter
  90. {
  91. #if UNITY_2018
  92. [MethodImpl(MethodImplOptions.InternalCall)]
  93. public static extern void WriteStringToUnityLogImpl(string s);
  94. #endif
  95. [MethodImpl(MethodImplOptions.InternalCall)]
  96. public static extern void WriteStringToUnityLog(string s);
  97. }
  98. }