Logger.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. namespace BepInEx.Logging
  4. {
  5. /// <summary>
  6. /// Handles pub-sub event marshalling across all log listeners and sources.
  7. /// </summary>
  8. public static class Logger
  9. {
  10. /// <summary>
  11. /// Collection of all log listeners that receive log events.
  12. /// </summary>
  13. public static ICollection<ILogListener> Listeners { get; } = new List<ILogListener>();
  14. /// <summary>
  15. /// Collection of all log source that output log events.
  16. /// </summary>
  17. public static ICollection<ILogSource> Sources { get; } = new LogSourceCollection();
  18. private static readonly ManualLogSource InternalLogSource = CreateLogSource("BepInEx");
  19. internal static void InternalLogEvent(object sender, LogEventArgs eventArgs)
  20. {
  21. foreach (var listener in Listeners)
  22. {
  23. listener?.LogEvent(sender, eventArgs);
  24. }
  25. }
  26. /// <summary>
  27. /// Logs an entry to the internal logger instance.
  28. /// </summary>
  29. /// <param name="level">The level of the entry.</param>
  30. /// <param name="data">The data of the entry.</param>
  31. internal static void Log(LogLevel level, object data)
  32. {
  33. InternalLogSource.Log(level, data);
  34. }
  35. internal static void LogFatal(object data) => Log(LogLevel.Fatal, data);
  36. internal static void LogError(object data) => Log(LogLevel.Error, data);
  37. internal static void LogWarning(object data) => Log(LogLevel.Warning, data);
  38. internal static void LogMessage(object data) => Log(LogLevel.Message, data);
  39. internal static void LogInfo(object data) => Log(LogLevel.Info, data);
  40. internal static void LogDebug(object data) => Log(LogLevel.Debug, data);
  41. /// <summary>
  42. /// Creates a new log source with a name and attaches it to <see cref="Sources"/>.
  43. /// </summary>
  44. /// <param name="sourceName">Name of the log source to create.</param>
  45. /// <returns>An instance of <see cref="ManualLogSource"/> that allows to write logs.</returns>
  46. public static ManualLogSource CreateLogSource(string sourceName)
  47. {
  48. var source = new ManualLogSource(sourceName);
  49. Sources.Add(source);
  50. return source;
  51. }
  52. private class LogSourceCollection : List<ILogSource>, ICollection<ILogSource>
  53. {
  54. void ICollection<ILogSource>.Add(ILogSource item)
  55. {
  56. if (item == null)
  57. throw new ArgumentNullException(nameof(item), "Log sources cannot be null when added to the source list.");
  58. item.LogEvent += InternalLogEvent;
  59. base.Add(item);
  60. }
  61. void ICollection<ILogSource>.Clear()
  62. {
  63. foreach (var item in base.ToArray())
  64. {
  65. ((ICollection<ILogSource>)this).Remove(item);
  66. }
  67. }
  68. bool ICollection<ILogSource>.Remove(ILogSource item)
  69. {
  70. if (item == null)
  71. return false;
  72. if (!base.Contains(item))
  73. return false;
  74. item.LogEvent -= InternalLogEvent;
  75. base.Remove(item);
  76. return true;
  77. }
  78. }
  79. }
  80. }