Logger.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using BepInEx.ConsoleUtil;
  4. namespace BepInEx
  5. {
  6. /// <summary>
  7. /// A helper class to use for logging.
  8. /// </summary>
  9. public static class BepInLogger
  10. {
  11. /// <summary>
  12. /// The handler for a entry logged event.
  13. /// </summary>
  14. /// <param name="entry">The text element of the log itself.</param>
  15. /// <param name="show">Whether or not it should be dislpayed to the user.</param>
  16. public delegate void EntryLoggedEventHandler(string entry, bool show = false);
  17. /// <summary>
  18. /// The listener event for an entry being logged.
  19. /// </summary>
  20. public static event EntryLoggedEventHandler EntryLogged;
  21. /// <summary>
  22. /// Logs an entry to the logger, and any listeners are notified of the entry.
  23. /// </summary>
  24. /// <param name="entry">The text element of the log itself.</param>
  25. /// <param name="show">Whether or not it should be dislpayed to the user.</param>
  26. public static void Log(string entry, bool show = false)
  27. {
  28. Log(entry, show, ConsoleColor.Gray);
  29. }
  30. /// <summary>
  31. /// Logs an entry to the logger, and any listeners are notified of the entry.
  32. /// </summary>
  33. /// <param name="entry">The text element of the log itself. Uses .ToString().</param>
  34. /// <param name="show">Whether or not it should be dislpayed to the user.</param>
  35. /// <param name="color">The color of the text to show in the console.</param>
  36. public static void Log(object entry, bool show, ConsoleColor color)
  37. {
  38. Log(entry.ToString(), show, color);
  39. }
  40. /// <summary>
  41. /// Logs an entry to the logger, and any listeners are notified of the entry.
  42. /// </summary>
  43. /// <param name="entry">The text element of the log itself.</param>
  44. /// <param name="show">Whether or not it should be dislpayed to the user.</param>
  45. /// <param name="color">The color of the text to show in the console.</param>
  46. public static void Log(string entry, bool show, ConsoleColor color)
  47. {
  48. UnityEngine.UnityLogWriter.WriteStringToUnityLog($"BEPIN - {entry}\r\n");
  49. Kon.ForegroundColor = color;
  50. Console.WriteLine(entry);
  51. EntryLogged?.Invoke(entry, show);
  52. }
  53. }
  54. }
  55. namespace UnityEngine
  56. {
  57. internal sealed class UnityLogWriter
  58. {
  59. [MethodImpl(MethodImplOptions.InternalCall)]
  60. public static extern void WriteStringToUnityLog(string s);
  61. }
  62. }