using FishNet.Documenting; using FishNet.Managing.Logging; using System.Runtime.CompilerServices; using UnityEngine; namespace FishNet.Managing { public partial class NetworkManager : MonoBehaviour { #region Serialized. /// /// Logging configuration to use. When empty default logging settings will be used. /// [Tooltip("Logging configuration to use. When empty default logging settings will be used.")] [SerializeField] private LoggingConfiguration _logging; #endregion #region Const. private const string ERROR_LOGGING_PREFIX = "Error - "; private const string WARNING_LOGGING_PREFIX = "Warning - "; private const string COMMON_LOGGING_PREFIX = "Log - "; #endregion /// /// Initializes logging settings. /// private void InitializeLogging() { if (_logging == null) _logging = ScriptableObject.CreateInstance(); else _logging = _logging.Clone(); _logging.InitializeOnce(); } /// /// True if can log for loggingType. /// /// Type of logging being filtered. /// internal bool InternalCanLog(LoggingType loggingType) { return _logging.CanLog(loggingType); } /// /// Performs a common log, should logging settings permit it. /// internal void InternalLog(string value) { _logging.Log(value); } /// /// Performs a log using the loggingType, should logging settings permit it. /// internal void InternalLog(LoggingType loggingType, string value) { if (loggingType == LoggingType.Common) _logging.Log(value); else if (loggingType == LoggingType.Warning) _logging.LogWarning(value); else if (loggingType == LoggingType.Error) _logging.LogError(value); } /// /// Performs a warning log, should logging settings permit it. /// internal void InternalLogWarning(string value) { _logging.LogWarning(value); } /// /// Performs an error log, should logging settings permit it. /// internal void InternalLogError(string value) { _logging.LogError(value); } } public static class NetworkManagerExtensions { /// /// True if can log for loggingType. /// internal static bool CanLog(this NetworkManager networkManager, LoggingType loggingType) { if (GetNetworkManager(ref networkManager)) return networkManager.InternalCanLog(loggingType); else return false; } /// /// Performs a log using the loggingType, should logging settings permit it. /// public static void Log(this NetworkManager networkManager, LoggingType loggingType, string value) { if (loggingType == LoggingType.Common) networkManager.Log(value); else if (loggingType == LoggingType.Warning) networkManager.LogWarning(value); else if (loggingType == LoggingType.Error) networkManager.LogError(value); } /// /// Performs a common log, should logging settings permit it. /// public static void Log(this NetworkManager networkManager, string message) { if (GetNetworkManager(ref networkManager)) networkManager.InternalLog(message); else Debug.Log(message); } /// /// Performs a warning log, should logging settings permit it. /// public static void LogWarning(this NetworkManager networkManager, string message) { if (GetNetworkManager(ref networkManager)) networkManager.InternalLogWarning(message); else Debug.LogWarning(message); } /// /// Performs an error log, should logging settings permit it. /// public static void LogError(this NetworkManager networkManager, string message) { if (GetNetworkManager(ref networkManager)) networkManager.InternalLogError(message); else Debug.LogError(message); } /// /// Gets a NetworkManager, first using a preferred option. /// /// True if a NetworkManager was found. private static bool GetNetworkManager(ref NetworkManager preferredNm) { if (preferredNm != null) return true; preferredNm = InstanceFinder.NetworkManager; return (preferredNm != null); } #region Backwards compatibility. /// /// Performs a common log, should logging settings permit it. /// public static void Log(string msg) => NetworkManagerExtensions.Log(null, msg); /// /// Performs a warning log, should logging settings permit it. /// public static void LogWarning(string msg) => NetworkManagerExtensions.LogWarning(null, msg); /// /// Performs an error log, should logging settings permit it. /// public static void LogError(string msg) => NetworkManagerExtensions.LogError(null, msg); /// /// True if can log for loggingType. /// public static bool CanLog(LoggingType lt) => NetworkManagerExtensions.CanLog(null, lt); #endregion } }