UnityLogWriter.cs 3.2 KB

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