/**************************************************************************** * Copyright 2019 Nreal Techonology Limited. All rights reserved. * * This file is part of NRSDK. * * https://www.nreal.ai/ * *****************************************************************************/ namespace NRKernal { using System.Diagnostics; /// Values that represent log levels. public enum LogLevel { /// An enum constant representing all option. All = 0, /// An enum constant representing the debug option. Debug, /// An enum constant representing the Information option. Info, /// An enum constant representing the warning option. Warning, /// An enum constant representing the error option. Error, /// An enum constant representing the fatal option. Fatal, /// An enum constant representing the off option. Off } /// Interface for log helper. public interface ILogHelper { /// Logs. /// The level. /// The message. void Log(LogLevel level, object message); } /// A default log helper. public class DefaultLogHelper : ILogHelper { /// Logs. /// Thrown when an exception error condition occurs. /// The level. /// The message. public void Log(LogLevel level, object message) { string log_info = string.Format("[{0}]{1}", level.ToString(), message.ToString()); switch (level) { case LogLevel.Debug: UnityEngine.Debug.Log(log_info); break; case LogLevel.Info: UnityEngine.Debug.Log(log_info); break; case LogLevel.Warning: UnityEngine.Debug.LogWarning(log_info); break; case LogLevel.Error: UnityEngine.Debug.LogError(log_info); break; default: throw new System.Exception(log_info); } } } /// A tool for log. public class NRDebugger { private static ILogHelper m_LogHelper = new DefaultLogHelper(); /// The log level. private static LogLevel m_LogLevel = LogLevel.Warning; /// Gets or sets the log level. /// The log level. public static LogLevel logLevel { get { return m_LogLevel; } set { m_LogLevel = value; } } /// Set the log helper. /// The log helper. public static void SetLogHelper(ILogHelper logHelper) { m_LogHelper = logHelper; } /// (Only available in DEBUG builds) debugs. /// The message. [Conditional("DEBUG")] public static void Debug(object message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Debug) { m_LogHelper.Log(LogLevel.Debug, message); } } /// (Only available in DEBUG builds) debugs. /// The message. [Conditional("DEBUG")] public static void Debug(string message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Debug) { m_LogHelper.Log(LogLevel.Debug, message); } } /// (Only available in DEBUG builds) debugs. /// Describes the format to use. /// The argument 0. [Conditional("DEBUG")] public static void Debug(string format, object arg0) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Debug) { m_LogHelper.Log(LogLevel.Debug, StringUtility.Format(format, arg0)); } } /// (Only available in DEBUG builds) debugs. /// Describes the format to use. /// The argument 0. /// The first argument. [Conditional("DEBUG")] public static void Debug(string format, object arg0, object arg1) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Debug) { m_LogHelper.Log(LogLevel.Debug, StringUtility.Format(format, arg0, arg1)); } } /// (Only available in DEBUG builds) debugs. /// Describes the format to use. /// The argument 0. /// The first argument. /// The second argument. [Conditional("DEBUG")] public static void Debug(string format, object arg0, object arg1, object arg2) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Debug) { m_LogHelper.Log(LogLevel.Debug, StringUtility.Format(format, arg0, arg1, arg2)); } } /// (Only available in DEBUG builds) debugs. /// Describes the format to use. /// A variable-length parameters list containing arguments. [Conditional("DEBUG")] public static void Debug(string format, params object[] args) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Debug) { m_LogHelper.Log(LogLevel.Debug, StringUtility.Format(format, args)); } } /// Infoes. /// The message. public static void Info(object message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Info) { m_LogHelper.Log(LogLevel.Info, message); } } /// Infoes. /// The message. public static void Info(string message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Info) { m_LogHelper.Log(LogLevel.Info, message); } } /// Infoes. /// Describes the format to use. /// The argument 0. public static void Info(string format, object arg0) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Info) { m_LogHelper.Log(LogLevel.Info, StringUtility.Format(format, arg0)); } } /// Infoes. /// Describes the format to use. /// The argument 0. /// The first argument. public static void Info(string format, object arg0, object arg1) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Info) { m_LogHelper.Log(LogLevel.Info, StringUtility.Format(format, arg0, arg1)); } } /// Infoes. /// Describes the format to use. /// The argument 0. /// The first argument. /// The second argument. public static void Info(string format, object arg0, object arg1, object arg2) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Info) { m_LogHelper.Log(LogLevel.Info, StringUtility.Format(format, arg0, arg1, arg2)); } } /// Infoes. /// Describes the format to use. /// A variable-length parameters list containing arguments. public static void Info(string format, params object[] args) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Info) { m_LogHelper.Log(LogLevel.Info, StringUtility.Format(format, args)); } } /// Warnings. /// The message. public static void Warning(object message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Warning) { m_LogHelper.Log(LogLevel.Warning, message); } } /// Warnings. /// The message. public static void Warning(string message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Warning) { m_LogHelper.Log(LogLevel.Warning, message); } } /// Warnings. /// Describes the format to use. /// The argument 0. public static void Warning(string format, object arg0) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Warning) { m_LogHelper.Log(LogLevel.Warning, StringUtility.Format(format, arg0)); } } /// Warnings. /// Describes the format to use. /// The argument 0. /// The first argument. public static void Warning(string format, object arg0, object arg1) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Warning) { m_LogHelper.Log(LogLevel.Warning, StringUtility.Format(format, arg0, arg1)); } } /// Warnings. /// Describes the format to use. /// The argument 0. /// The first argument. /// The second argument. public static void Warning(string format, object arg0, object arg1, object arg2) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Warning) { m_LogHelper.Log(LogLevel.Warning, StringUtility.Format(format, arg0, arg1, arg2)); } } /// Warnings. /// Describes the format to use. /// A variable-length parameters list containing arguments. public static void Warning(string format, params object[] args) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Warning) { m_LogHelper.Log(LogLevel.Warning, StringUtility.Format(format, args)); } } /// Errors. /// The message. public static void Error(object message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Error) { m_LogHelper.Log(LogLevel.Error, message); } } /// Errors. /// The message. public static void Error(string message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Error) { m_LogHelper.Log(LogLevel.Error, message); } } /// Errors. /// Describes the format to use. /// The argument 0. public static void Error(string format, object arg0) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Error) { m_LogHelper.Log(LogLevel.Error, StringUtility.Format(format, arg0)); } } /// Errors. /// Describes the format to use. /// The argument 0. /// The first argument. public static void Error(string format, object arg0, object arg1) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Error) { m_LogHelper.Log(LogLevel.Error, StringUtility.Format(format, arg0, arg1)); } } /// Errors. /// Describes the format to use. /// The argument 0. /// The first argument. /// The second argument. public static void Error(string format, object arg0, object arg1, object arg2) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Error) { m_LogHelper.Log(LogLevel.Error, StringUtility.Format(format, arg0, arg1, arg2)); } } /// Errors. /// Describes the format to use. /// A variable-length parameters list containing arguments. public static void Error(string format, params object[] args) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Error) { m_LogHelper.Log(LogLevel.Error, StringUtility.Format(format, args)); } } /// Fatals. /// The message. public static void Fatal(object message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Fatal) { m_LogHelper.Log(LogLevel.Fatal, message); } } /// Fatals. /// The message. public static void Fatal(string message) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Fatal) { m_LogHelper.Log(LogLevel.Fatal, message); } } /// Fatals. /// Describes the format to use. /// The argument 0. public static void Fatal(string format, object arg0) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Fatal) { m_LogHelper.Log(LogLevel.Fatal, StringUtility.Format(format, arg0)); } } /// Fatals. /// Describes the format to use. /// The argument 0. /// The first argument. public static void Fatal(string format, object arg0, object arg1) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Fatal) { m_LogHelper.Log(LogLevel.Fatal, StringUtility.Format(format, arg0, arg1)); } } /// Fatals. /// Describes the format to use. /// The argument 0. /// The first argument. /// The second argument. public static void Fatal(string format, object arg0, object arg1, object arg2) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Fatal) { m_LogHelper.Log(LogLevel.Fatal, StringUtility.Format(format, arg0, arg1, arg2)); } } /// Fatals. /// Describes the format to use. /// A variable-length parameters list containing arguments. public static void Fatal(string format, params object[] args) { if (m_LogHelper == null) { return; } if (m_LogLevel <= LogLevel.Fatal) { m_LogHelper.Log(LogLevel.Fatal, StringUtility.Format(format, args)); } } } }