Logger.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. namespace BepInEx.Logging
  4. {
  5. /// <summary>
  6. /// A static Logger instance.
  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. private static bool internalLogsInitialized;
  20. internal static void InitializeInternalLoggers()
  21. {
  22. if (internalLogsInitialized)
  23. return;
  24. Sources.Add(new HarmonyLogSource());
  25. internalLogsInitialized = true;
  26. }
  27. internal static void InternalLogEvent(object sender, LogEventArgs eventArgs)
  28. {
  29. foreach (var listener in Listeners)
  30. {
  31. listener?.LogEvent(sender, eventArgs);
  32. }
  33. }
  34. /// <summary>
  35. /// Logs an entry to the current logger instance.
  36. /// </summary>
  37. /// <param name="level">The level of the entry.</param>
  38. /// <param name="data">The textual value of the entry.</param>
  39. internal static void Log(LogLevel level, object data)
  40. {
  41. InternalLogSource.Log(level, data);
  42. }
  43. internal static void LogFatal(object data) => Log(LogLevel.Fatal, data);
  44. internal static void LogError(object data) => Log(LogLevel.Error, data);
  45. internal static void LogWarning(object data) => Log(LogLevel.Warning, data);
  46. internal static void LogMessage(object data) => Log(LogLevel.Message, data);
  47. internal static void LogInfo(object data) => Log(LogLevel.Info, data);
  48. internal static void LogDebug(object data) => Log(LogLevel.Debug, data);
  49. /// <summary>
  50. /// Creates a new log source with a name and attaches it to log sources.
  51. /// </summary>
  52. /// <param name="sourceName">Name of the log source to create.</param>
  53. /// <returns>An instance of <see cref="ManualLogSource"/> that allows to write logs.</returns>
  54. public static ManualLogSource CreateLogSource(string sourceName)
  55. {
  56. var source = new ManualLogSource(sourceName);
  57. Sources.Add(source);
  58. return source;
  59. }
  60. private class LogSourceCollection : List<ILogSource>, ICollection<ILogSource>
  61. {
  62. void ICollection<ILogSource>.Add(ILogSource item)
  63. {
  64. if (item == null)
  65. throw new ArgumentNullException(nameof(item), "Log sources cannot be null when added to the source list.");
  66. item.LogEvent += InternalLogEvent;
  67. base.Add(item);
  68. }
  69. void ICollection<ILogSource>.Clear()
  70. {
  71. foreach (var item in base.ToArray())
  72. {
  73. ((ICollection<ILogSource>)this).Remove(item);
  74. }
  75. }
  76. bool ICollection<ILogSource>.Remove(ILogSource item)
  77. {
  78. if (item == null)
  79. return false;
  80. if (!base.Contains(item))
  81. return false;
  82. item.LogEvent -= InternalLogEvent;
  83. base.Remove(item);
  84. return true;
  85. }
  86. }
  87. }
  88. }