Logger.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. Uses .ToString().</param>
  25. /// <param name="show">Whether or not it should be dislpayed to the user.</param>
  26. /// <param name="color">The color of the text to show in the console.</param>
  27. public static void Log(object entry, bool show = false, ConsoleColor color = ConsoleColor.Gray)
  28. {
  29. Log(entry.ToString(), show, color);
  30. }
  31. /// <summary>
  32. /// Logs an entry to the logger, and any listeners are notified of the entry.
  33. /// </summary>
  34. /// <param name="entry">The text element of the log itself.</param>
  35. /// <param name="show">Whether or not it should be dislpayed to the user.</param>
  36. /// <param name="color">The color of the text to show in the console.</param>
  37. public static void Log(string entry, bool show = false, ConsoleColor color = ConsoleColor.Gray)
  38. {
  39. UnityEngine.UnityLogWriter.WriteStringToUnityLog($"BEPIN - {entry}\r\n");
  40. Kon.ForegroundColor = color;
  41. Console.WriteLine(entry);
  42. EntryLogged?.Invoke(entry, show);
  43. }
  44. }
  45. }
  46. namespace UnityEngine
  47. {
  48. internal sealed class UnityLogWriter
  49. {
  50. [MethodImpl(MethodImplOptions.InternalCall)]
  51. public static extern void WriteStringToUnityLog(string s);
  52. }
  53. }