using System; using System.Collections.Generic; using BepInEx.Logging; namespace BepInEx { /// /// A static instance. /// public static class Logger { private static readonly ManualLogSource InternalLogSource = new ManualLogSource("BepInEx"); public static ICollection Listeners { get; } = new List(); public static ICollection Sources { get; } = new LogSourceCollection() { InternalLogSource }; private static void InternalLogEvent(object sender, LogEventArgs eventArgs) { foreach (var listener in Listeners) { listener?.LogEvent(sender, eventArgs); } } /// /// Logs an entry to the current logger instance. /// /// The level of the entry. /// The textual value of the entry. public static void Log(LogLevel level, object data) { InternalLogEvent(InternalLogSource, new LogEventArgs(data, level, InternalLogSource)); } public static void LogFatal(object data) => Log(LogLevel.Fatal, data); public static void LogError(object data) => Log(LogLevel.Error, data); public static void LogWarning(object data) => Log(LogLevel.Warning, data); public static void LogMessage(object data) => Log(LogLevel.Message, data); public static void LogInfo(object data) => Log(LogLevel.Info, data); public static void LogDebug(object data) => Log(LogLevel.Debug, data); private class LogSourceCollection : List, ICollection { void ICollection.Add(ILogSource item) { if (item == null) throw new ArgumentNullException(nameof(item), "Log sources cannot be null when added to the source list."); item.LogEvent += InternalLogEvent; base.Add(item); } void ICollection.Clear() { foreach (var item in base.ToArray()) { ((ICollection)this).Remove(item); } } bool ICollection.Remove(ILogSource item) { if (item == null) return false; if (!base.Contains(item)) return false; item.LogEvent -= InternalLogEvent; base.Remove(item); return true; } } } }