using FishNet.Documenting; using System; using System.Runtime.CompilerServices; using UnityEngine; namespace FishNet.Managing.Logging { /// /// Configuration ScriptableObject specifying which data to log. Used in conjuction with NetworkManager. /// [CreateAssetMenu(fileName = "New LevelLoggingConfiguration", menuName = "FishNet/Logging/Level Logging Configuration")] public class LevelLoggingConfiguration : LoggingConfiguration { #region Serialized. /// /// Type of logging to use for development builds and editor. /// [Tooltip("Type of logging to use for development builds and editor.")] [SerializeField] private LoggingType _developmentLogging = LoggingType.Common; /// /// Type of logging to use for GUI builds. /// [Tooltip("Type of logging to use for GUI builds.")] [SerializeField] private LoggingType _guiLogging = LoggingType.Warning; /// /// Type of logging to use for headless builds. /// [Tooltip("Type of logging to use for headless builds.")] [SerializeField] private LoggingType _headlessLogging = LoggingType.Error; #endregion #region Private. /// /// True when initialized. /// private bool _initialized; /// /// Highest type which can be logged. /// private LoggingType _highestLoggingType = LoggingType.Off; #endregion [APIExclude] public void LoggingConstructor(bool loggingEnabled, LoggingType development, LoggingType gui, LoggingType headless) { base.LoggingEnabled = loggingEnabled; _developmentLogging = development; _guiLogging = gui; _headlessLogging = headless; } /// /// Initializes script for use. /// /// public override void InitializeOnce() { byte currentHighest = (byte)LoggingType.Off; #if UNITY_SERVER //if headless. currentHighest = Math.Max(currentHighest, (byte)_headlessLogging); #endif #if UNITY_EDITOR || DEVELOPMENT_BUILD //if editor or development. currentHighest = Math.Max(currentHighest, (byte)_developmentLogging); #endif #if !UNITY_EDITOR && !UNITY_SERVER //if a build. currentHighest = Math.Max(currentHighest, (byte)_guiLogging); #endif _highestLoggingType = (LoggingType)currentHighest; _initialized = true; } /// /// True if can log for loggingType. /// /// Type of logging being filtered. /// public override bool CanLog(LoggingType loggingType) { if (!base.LoggingEnabled) return false; if (!_initialized) { #if UNITY_EDITOR || DEVELOPMENT_BUILD if (Application.isPlaying) Debug.LogError("CanLog called before being initialized."); else return true; #endif return false; } return ((byte)loggingType <= (byte)_highestLoggingType); } /// /// Logs a common value if can log. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override void Log(string value) { if (CanLog(LoggingType.Common)) Debug.Log(value); } /// /// Logs a warning value if can log. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override void LogWarning(string value) { if (CanLog(LoggingType.Warning)) Debug.LogWarning(value); } /// /// Logs an error value if can log. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override void LogError(string value) { if (CanLog(LoggingType.Error)) Debug.LogError(value); } /// /// Clones this logging configuration. /// /// public override LoggingConfiguration Clone() { LevelLoggingConfiguration copy = ScriptableObject.CreateInstance(); copy.LoggingConstructor(base.LoggingEnabled, _developmentLogging, _guiLogging, _headlessLogging); return copy; } } }