using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Channel = Ubisoft.HtLogger.Channel; namespace Ubisoft.Consumer.Sample.Logger { public class LoggerTestSceneController : MonoBehaviour { private readonly HashSet CHANNELS_SET = new HashSet() { GameLogger.CHANNEL_ORANGE }; private enum EChannelsInability { EnableAllChannels, DisableAllChannels, EnableAllChannelsExceptOrange, DisableAllChannelsExceptOrange } public Dropdown m_channelsDropdown; public Dropdown m_channelsInabilityDropdown; private Channel m_currentChannel; private HtLogger Logger { get { return HtLogger.Instance; } } void Start() { if (m_channelsDropdown != null) { m_channelsDropdown.ClearOptions(); List options = new List(); Dropdown.OptionData option; List gameChannels = GameLogger.Channels; int count = gameChannels.Count; for (int i = 0; i < count; ++i) { option = new Dropdown.OptionData { text = gameChannels[i].Name }; options.Add(option); } m_channelsDropdown.AddOptions(options); SetCurrentChannelByIndex(0); } if (m_channelsInabilityDropdown != null) { m_channelsInabilityDropdown.ClearOptions(); List options = new List(); Dropdown.OptionData option; string[] values = Enum.GetNames(typeof(EChannelsInability)); int count = values.Length; for (int i = 0; i < count; ++i) { option = new Dropdown.OptionData { text = values[i] }; options.Add(option); } m_channelsInabilityDropdown.AddOptions(options); } } public void OnChangeChannel(int index) { if (m_channelsDropdown != null) { SetCurrentChannelByIndex(index); } } public void OnChangeChannelsInability(int index) { EChannelsInability channelsInability = (EChannelsInability)index; switch (channelsInability) { case EChannelsInability.EnableAllChannels: Logger.EnableAllChannels(); break; case EChannelsInability.DisableAllChannels: Logger.DisableAllChannels(); break; case EChannelsInability.EnableAllChannelsExceptOrange: Logger.EnableAllChannelsExcept(CHANNELS_SET); break; case EChannelsInability.DisableAllChannelsExceptOrange: Logger.DisableAllChannelsExcept(CHANNELS_SET); break; } } public void LogTrace() { string message = "LogTrace"; string messageInDirectChannel = $"{message} in a channel passed in directly (no color supported)"; string channel = "ChannelAsString"; int param1 = 4; string param2 = "boo"; // Use Logger directly Logger.LogTrace(message); Logger.LogTrace($"{message} with context", m_channelsDropdown); Logger.LogTraceFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.LogTraceFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogTrace(channel, messageInDirectChannel); Logger.ChannelLogTrace(channel, messageInDirectChannel + " with context", m_channelsDropdown); Logger.ChannelLogTraceFormat(channel, messageInDirectChannel + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogTraceFormat(channel, m_channelsDropdown, messageInDirectChannel + " with context and format (Param1: {0}, Param2: {1})", param1, param2); // Use a channel string messageInChannel = $"{message} in selected channel"; ChannelLogTrace(m_currentChannel, messageInChannel); // Use default channel messageInChannel = $"{message} in General channel"; ChannelLogTrace(HtLogger.Instance.GeneralChannel, messageInChannel); } public void LogDebug() { string message = "LogDebug"; string messageInDirectChannel = $"{message} in a channel passed in directly (no color supported)"; string channel = "ChannelAsString"; int param1 = 4; string param2 = "boo"; // Use Logger directly Logger.LogDebug(message); Logger.LogDebug($"{message} with context", m_channelsDropdown); Logger.LogDebugFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.LogDebugFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogDebug(channel, messageInDirectChannel); Logger.ChannelLogDebug(channel, messageInDirectChannel + " with context", m_channelsDropdown); Logger.ChannelLogDebugFormat(channel, messageInDirectChannel + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogDebugFormat(channel, m_channelsDropdown, messageInDirectChannel + " with context and format (Param1: {0}, Param2: {1})", param1, param2); // Use a channel string messageInChannel = $"{message} in selected channel"; ChannelLogDebug(m_currentChannel, messageInChannel); // Use default channel messageInChannel = $"{message} in General channel"; ChannelLogDebug(HtLogger.Instance.GeneralChannel, messageInChannel); } public void LogInformation() { string message = "LogInformation"; string messageInDirectChannel = $"{message} in a channel passed in directly (no color supported)"; string channel = "ChannelAsString"; int param1 = 4; string param2 = "boo"; // Use Logger directly Logger.LogInformation(message); Logger.LogInformation($"{message} with context", m_channelsDropdown); Logger.LogInformationFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.LogInformationFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogInformation(channel, messageInDirectChannel); Logger.ChannelLogInformation(channel, messageInDirectChannel + " with context", m_channelsDropdown); Logger.ChannelLogInformationFormat(channel, messageInDirectChannel + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogInformationFormat(channel, m_channelsDropdown, messageInDirectChannel + " with context and format (Param1: {0}, Param2: {1})", param1, param2); // Use a channel string messageInChannel = $"{message} in selected channel"; ChannelLogInformation(m_currentChannel, messageInChannel); // Use default channel messageInChannel = $"{message} in General channel"; ChannelLogInformation(HtLogger.Instance.GeneralChannel, messageInChannel); } public void LogWarning() { string message = "LogWarning"; string messageInDirectChannel = $"{message} in a channel passed in directly (no color supported)"; string channel = "ChannelAsString"; int param1 = 4; string param2 = "boo"; // Use Logger directly Logger.LogWarning(message); Logger.LogWarning($"{message} with context", m_channelsDropdown); Logger.LogWarningFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.LogWarningFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogWarning(channel, messageInDirectChannel); Logger.ChannelLogWarning(channel, messageInDirectChannel + " with context", m_channelsDropdown); Logger.ChannelLogWarningFormat(channel, messageInDirectChannel + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogWarningFormat(channel, m_channelsDropdown, messageInDirectChannel + " with context and format (Param1: {0}, Param2: {1})", param1, param2); // Use a channel string messageInChannel = $"{message} in selected channel"; ChannelLogWarning(m_currentChannel, messageInChannel); // Use default channel messageInChannel = $"{message} in General channel"; ChannelLogWarning(HtLogger.Instance.GeneralChannel, messageInChannel); } public void LogError() { string message = "LogError"; string messageInDirectChannel = $"{message} in a channel passed in directly (no color supported)"; string channel = "ChannelAsString"; int param1 = 4; string param2 = "boo"; // Use Logger directly Logger.LogError(message); Logger.LogError($"{message} with context", m_channelsDropdown); Logger.LogErrorFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.LogErrorFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogError(channel, messageInDirectChannel); Logger.ChannelLogError(channel, messageInDirectChannel + " with context", m_channelsDropdown); Logger.ChannelLogErrorFormat(channel, messageInDirectChannel + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogErrorFormat(channel, m_channelsDropdown, messageInDirectChannel + " with context and format (Param1: {0}, Param2: {1})", param1, param2); // Use a channel string messageInChannel = $"{message} in selected channel"; ChannelLogError(m_currentChannel, messageInChannel); // Use default channel messageInChannel = $"{message} in General channel"; ChannelLogError(HtLogger.Instance.GeneralChannel, messageInChannel); } public void LogCritical() { string message = "LogCritical"; string messageInDirectChannel = $"{message} in a channel passed in directly (no color supported)"; string channel = "ChannelAsString"; int param1 = 4; string param2 = "boo"; // Use Logger directly Logger.LogCritical(message); Logger.LogCritical($"{message} with context", m_channelsDropdown); Logger.LogCriticalFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.LogCriticalFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogCritical(channel, messageInDirectChannel); Logger.ChannelLogCritical(channel, messageInDirectChannel + " with context", m_channelsDropdown); Logger.ChannelLogCriticalFormat(channel, messageInDirectChannel + " with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogCriticalFormat(channel, m_channelsDropdown, messageInDirectChannel + " with context and format (Param1: {0}, Param2: {1})", param1, param2); // Use a channel string messageInChannel = $"{message} in selected channel"; ChannelLogCritical(m_currentChannel, messageInChannel); // Use default channel messageInChannel = $"{message} in General channel"; ChannelLogCritical(HtLogger.Instance.GeneralChannel, messageInChannel); } public void LogToAllChannels() { GameLogger.CHANNEL_ORANGE.LogTrace("LogTrace"); GameLogger.CHANNEL_GREEN.LogTrace("LogTrace"); GameLogger.CHANNEL_ORANGE.LogDebug("LogDebug"); GameLogger.CHANNEL_GREEN.LogDebug("LogDebug"); GameLogger.CHANNEL_ORANGE.LogInformation("LogInformation"); GameLogger.CHANNEL_GREEN.LogInformation("LogInformation"); GameLogger.CHANNEL_ORANGE.LogWarning("LogWarning"); GameLogger.CHANNEL_GREEN.LogWarning("LogWarning"); GameLogger.CHANNEL_ORANGE.LogError("LogError"); GameLogger.CHANNEL_GREEN.LogError("LogError"); GameLogger.CHANNEL_ORANGE.LogCritical("LogCritical"); GameLogger.CHANNEL_GREEN.LogCritical("LogCritical"); GameLogger.CHANNEL_GREEN.Assert(false); GameLogger.CHANNEL_ORANGE.Assert(false); } public void Assert() { string message = "Assert"; string messageInDirectChannel = $"{message} in a channel passed in directly "; int param1 = 4; string param2 = "boo"; // Use Logger directly // It won't be logged because condition is true Logger.Assert(true, $"{message} true"); Logger.Assert(false, $"{message} false"); // It won't be logged because condition is true Logger.Assert(true, $"{message} true with context", m_channelsDropdown); Logger.Assert(false, $"{message} false with context", m_channelsDropdown); // It won't be logged because condition is true Logger.AssertFormat(true, $"{message} true with format (Param1: {0}, Param2: {1})", param1, param2); Logger.AssertFormat(false, $"{message} false with format (Param1: {0}, Param2: {1})", param1, param2); Logger.LogAssertion("LogAssertion"); Logger.LogAssertion("LogAssertion with context", m_channelsDropdown); string channel = "ChannelAsString"; // It won't be logged because condition is true Logger.ChannelAssert(channel, true, $"{messageInDirectChannel} true"); Logger.ChannelAssert(channel, false, $"{messageInDirectChannel} false"); // It won't be logged because condition is true Logger.ChannelAssertFormat(channel, true, $"{messageInDirectChannel} true with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelAssertFormat(channel, false, $"{messageInDirectChannel} false with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogAssertion(channel, messageInDirectChannel); Logger.ChannelLogAssertion(channel, m_channelsDropdown, $"{messageInDirectChannel} with context"); Logger.ChannelLogAssertion(channel, $"{messageInDirectChannel} with format (Param1: {0}, Param2: {1})", param1, param2); Logger.ChannelLogAssertion(channel, m_channelsDropdown, $"{messageInDirectChannel} with context and format (Param1: {0}, Param2: {1})", param1, param2); // Use a channel string messageInChannel = $"{message} in selected channel"; ChannelAssert(m_currentChannel, messageInChannel); // Use default channel messageInChannel = $"{message} in General channel"; ChannelAssert(HtLogger.Instance.GeneralChannel, messageInChannel); } private int[] temp; public void TriggerException() { Logger.LogTrace(temp[10]); } private void ChannelLogTrace(Channel channel, string message) { int param1 = 4; string param2 = "boo"; channel.LogTrace(message); channel.LogTrace($"{message} with context", m_channelsDropdown); channel.LogTraceFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); channel.LogTraceFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); } private void ChannelLogDebug(Channel channel, string message) { int param1 = 4; string param2 = "boo"; channel.LogDebug(message); channel.LogDebug($"{message} with context", m_channelsDropdown); channel.LogDebugFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); channel.LogDebugFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); } private void ChannelLogInformation(Channel channel, string message) { int param1 = 4; string param2 = "boo"; channel.LogInformation(message); channel.LogInformation($"{message} with context", m_channelsDropdown); channel.LogInformationFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); channel.LogInformationFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); } private void ChannelLogWarning(Channel channel, string message) { int param1 = 4; string param2 = "boo"; channel.LogWarning(message); channel.LogWarning($"{message} with context", m_channelsDropdown); channel.LogWarningFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); channel.LogWarningFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); } private void ChannelLogError(Channel channel, string message) { int param1 = 4; string param2 = "boo"; channel.LogError(message); channel.LogError($"{message} with context", m_channelsDropdown); channel.LogErrorFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); channel.LogErrorFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); } private void ChannelLogCritical(Channel channel, string message) { int param1 = 4; string param2 = "boo"; channel.LogCritical(message); channel.LogCritical($"{message} with context", m_channelsDropdown); channel.LogCriticalFormat(message + " with format (Param1: {0}, Param2: {1})", param1, param2); channel.LogCriticalFormat(m_channelsDropdown, message + " with context and format (Param1: {0}, Param2: {1})", param1, param2); } private void ChannelAssert(Channel channel, string message) { int param1 = 4; string param2 = "boo"; // It won't be logged because condition is true channel.Assert(true, $"{message} true"); channel.Assert(false, $"{message} false"); // It won't be logged because condition is true channel.Assert(true, $"{message} true with context", m_channelsDropdown); channel.Assert(false, $"{message} false with context", m_channelsDropdown); // It won't be logged because condition is true channel.AssertFormat(true, $"{message} true with format (Param1: {0}, Param2: {1})", param1, param2); channel.AssertFormat(false, $"{message} false with format (Param1: {0}, Param2: {1})", param1, param2); channel.LogAssertion("LogAssertion"); channel.LogAssertion("LogAssertion with context", m_channelsDropdown); } private void SetCurrentChannelByIndex(int index) { List channels = GameLogger.Channels; if (index > -1 && index < channels.Count) { m_currentChannel = channels[index]; } else { m_currentChannel = null; } } } }