UnityLogWriter.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BepInEx.ConsoleUtil;
  4. namespace BepInEx.Logging
  5. {
  6. /// <summary>
  7. /// Logs entries using Unity specific outputs.
  8. /// </summary>
  9. public class UnityLogWriter : BaseLogger
  10. {
  11. /// <summary>
  12. /// Writes a string specifically to the game output log.
  13. /// </summary>
  14. /// <param name="value">The value to write.</param>
  15. public void WriteToLog(string value)
  16. {
  17. UnityEngine.UnityLogWriter.WriteStringToUnityLog(value);
  18. }
  19. protected void InternalWrite(string value)
  20. {
  21. Console.Write(value);
  22. WriteToLog(value);
  23. }
  24. /// <summary>
  25. /// Logs an entry to the Logger instance.
  26. /// </summary>
  27. /// <param name="level">The level of the entry.</param>
  28. /// <param name="entry">The textual value of the entry.</param>
  29. public override void Log(LogLevel level, object entry)
  30. {
  31. Kon.ForegroundColor = level.GetConsoleColor();
  32. base.Log(level, entry);
  33. Kon.ForegroundColor = ConsoleColor.Gray;
  34. }
  35. public override void WriteLine(string value) => InternalWrite($"{value}\r\n");
  36. public override void Write(char value) => InternalWrite(value.ToString());
  37. public override void Write(string value) => InternalWrite(value);
  38. }
  39. }
  40. namespace UnityEngine
  41. {
  42. internal sealed class UnityLogWriter
  43. {
  44. [MethodImpl(MethodImplOptions.InternalCall)]
  45. public static extern void WriteStringToUnityLog(string s);
  46. }
  47. }