using System.Collections.Generic; using UnityEngine; using Ubisoft.Hotel.Package; using Ubisoft.Hotel.Package.Editor; namespace Ubisoft.Hotel.Logger.Editor { public class LoggerProperty : PackageProperty { /// /// Create a LoggerProperty object. /// /// Property name. /// Root Unity path ("Assets/...") to the folder that contains all ChannelsPreset objects. /// Name of the file in channelslPresetRootUnityPath that was copied into this object /// A LoggerProperty object to configure Logger. public static LoggerProperty CreateInstanceWithParams(string name, string channelslPresetRootUnityPath = null, string channelsPresetName = null) { LoggerProperty returnValue = CreateInstance(); returnValue.name = name; returnValue.ChannelsPresetRootUnityPath = channelslPresetRootUnityPath; returnValue.ChannelsPresetName = channelsPresetName; return returnValue; } /// /// Minimum log level to use. Logs lower than this level will be stripped out which means that there will be /// no impact in performance or build size /// [SerializeField] private HtLogger.ELogLevel m_minLogLevel; /// /// Whether or not assertions are enabled. It should always be false for RELEASE builds /// [SerializeField] private bool m_isAssertEnabled; /// /// Whether or not exceptions are enabled. /// [SerializeField] private bool m_areExceptionsEnabled; /// /// Whether or not Unity Console output stream is enabled /// [SerializeField] private bool m_isUnityLoggerEnabled; /// /// Max size in kbytes for the file log. Pass in 0 if you don't want to use a file as an output stream. /// This attribute is only useful when using the ZLogger implementation. /// [SerializeField] private int m_maxSizeFile; /// /// Root Unity path ("Assets/...") to the folder that contains all ChannelsPreset objects. /// [SerializeField] private string m_channelsPresetRootUnityPath; /// /// Name of the file in m_channelsPresetRootUnityPath that contains the ChannelsPreset objects to copy to /// PlayerLoggerProperty so HtLogger starts with that configuration. /// [SerializeField] private string m_channelsPresetName; public HtLogger.ELogLevel MinLogLevel { get { return m_minLogLevel; } set { m_minLogLevel = value; } } public bool IsAssertEnabled { get { return m_isAssertEnabled; } set { m_isAssertEnabled = value; } } public bool AreExceptionsEnabled { get { return m_areExceptionsEnabled; } set { m_areExceptionsEnabled = value; } } public bool IsUnityLoggerEnabled { get { return m_isUnityLoggerEnabled; } set { m_isUnityLoggerEnabled = value; } } public int MaxSizeFile { get { return m_maxSizeFile; } set { m_maxSizeFile = value; } } public string ChannelsPresetRootUnityPath { get { return m_channelsPresetRootUnityPath; } set { m_channelsPresetRootUnityPath = value; } } public string ChannelsPresetName { get { return m_channelsPresetName; } set { m_channelsPresetName = value; } } public override void Apply(BuildSuite buildSuite) { UnityBuildProperty unityProperty = UnityBuildProperty.CreateInstanceWithParams($"{name}_UnityBuildProperty"); // If min level is None then no levels are allowed if (MinLogLevel != HtLogger.ELogLevel.None) { // We need to create the define symbol that enables every log level from the one selected (be aware that a log level enables the ones less verbose too) string symbol; string[] names = System.Enum.GetNames(typeof(HtLogger.ELogLevel)); int count = names.Length; int logLevelAsInt = (int)MinLogLevel; for (int i = logLevelAsInt; i < count; i++) { symbol = HtLogger.GetLoggerDefineSymbol((HtLogger.ELogLevel)i); unityProperty.AddDefineSymbol(symbol); } } if (IsAssertEnabled) { unityProperty.AddDefineSymbol(HtLogger.HT_LOGGER_ASSERT_ON); } if (AreExceptionsEnabled) { unityProperty.AddDefineSymbol(HtLogger.HT_LOGGER_EXCEPTION_ON); } buildSuite.OrderProperty(unityProperty); } public override void AddPackagePropertyPlayer(List list) { PlayerLoggerProperty property = CreateInstance(); property.name = HtTypes.GetTypeShortName(GetType()); property.MinLogLevel = MinLogLevel; property.IsUnityLoggerEnabled = IsUnityLoggerEnabled; property.MaxSizeFile = MaxSizeFile; list.Add(property); // // ChannelsPreset //∫ ChannelsPreset preset = null; // Attempt to load ChannelPresetName string presetName = ChannelsPresetName; if (!string.IsNullOrEmpty(ChannelsPresetRootUnityPath) && !string.IsNullOrEmpty(presetName)) { string unityPathRoot = ChannelsPresetRootUnityPath; if (!ChannelsPresetRootUnityPath.EndsWith("/")) { unityPathRoot += "/"; } string unityPath = $"{unityPathRoot}{presetName}.asset"; preset = UnityEditor.AssetDatabase.LoadAssetAtPath(unityPath, typeof(ChannelsPreset)) as ChannelsPreset; if (preset == null) { string errorText = $"No ChannelsPreset scriptable object found at {Debug.FormatTextInUserContext(unityPath)}. Create it at this location by using the contextual menu or correct this path in your suite builder script. "; presetName = "default"; // Check if a default preset is defined in 'ChannelsPresetRootUnityPath' unityPath = $"{unityPathRoot}{presetName}.asset"; preset = UnityEditor.AssetDatabase.LoadAssetAtPath(unityPath, typeof(ChannelsPreset)) as ChannelsPreset; if (preset == null) { errorText += "Hotel Logger default preset is used instead because no custom default preset was found."; } else { errorText += $"Custom default preset at {Debug.FormatTextInUserContext(unityPath)} is used instead."; } Debug.LogError(errorText); } } property.ChannelsPreset = ChannelsPreset.CreateInstanceWithParams("Logger:ChannelsPreset"); if (preset != null) { property.ChannelsPreset.CopyFrom(preset); } property.ChannelsPreset.Name = presetName; property.ChannelsPreset.RootUnityPath = ChannelsPresetRootUnityPath; // ChannelsPreset property needs to be added to list so it won't get lost when copied to Resources list.Add(property.ChannelsPreset); } } }