|
using UnityEngine; |
|
|
|
/// <summary> |
|
/// |
|
/// Debug Utils. |
|
/// |
|
/// <para> |
|
/// Usefull utilities for debuging. | The May 24th, 2019 version contains: |
|
/// 1.- Debug.Log Methods visible only in debug mode and unity editor. |
|
/// 2.- Filter System to show only desired messages. |
|
/// </para> |
|
/// |
|
/// <para> By Javier García | @jvrgms | 2019 </para> |
|
/// </summary> |
|
public static class DebugUtils { |
|
|
|
|
|
|
|
#region Nested Classes |
|
|
|
/// <summary> Level. </summary> |
|
public enum Level { |
|
Debug = 0, // Level with debugging purposes. |
|
Info = 1, // Level for public important info. |
|
DevInfo = 1, // Level for info just for development. |
|
Warning = 3, // Level for public warnings. |
|
DevWarning = 4, // Level for development warnings. |
|
Error = 5, // Level for public errors. |
|
DevError = 6 // Level for development errors. |
|
}; |
|
|
|
#endregion |
|
|
|
|
|
|
|
#region Filter System |
|
|
|
/// <summary> Gets the debug filter. </summary> |
|
/// <value>The debug filter.</value> |
|
public static int Filter { get; set; } = 0; |
|
|
|
/// <summary> Sets completely the debug filter. </summary> |
|
/// <param name="levels">Filters.</param> |
|
public static void SetFilter (params Level[] levels) { |
|
Filter = 0; |
|
foreach (Level level in levels) |
|
AddLevelToFilter (level); |
|
} |
|
|
|
/// <summary> Gets the array of levels in filter. </summary> |
|
/// <returns>The levels.</returns> |
|
public static Level[] GetFilteredLevels () { |
|
|
|
int levelsCount = 0; |
|
for(int i = 0; i <= 6; i++) |
|
if (IsInFilter ((Level)i)) |
|
levelsCount++; |
|
|
|
Level[] levels = new Level[levelsCount]; |
|
for (int i = 0; i <= 6; i++) |
|
if (IsInFilter ((Level)i)) |
|
levels[i] = (Level)i; |
|
|
|
return levels; |
|
} |
|
|
|
/// <summary> Adds a new filter. </summary> |
|
/// <param name="level">Filter.</param> |
|
public static void AddLevelToFilter (Level level) { |
|
Filter |= 1 << (int)level; |
|
} |
|
|
|
/// <summary> Removes a level from filter. </summary> |
|
/// <param name="level">Level.</param> |
|
public static void RemoveLevelFromFilter (Level level) { |
|
if (IsInFilter (level)) |
|
Filter &= ~(1 << (int)level); |
|
} |
|
|
|
/// <summary> Defines if a filter belongs to the debug filter. </summary> |
|
/// <returns><c>true</c> if is in debugfilter.</returns> |
|
/// <param name="filter">Filter.</param> |
|
public static bool IsInFilter (Level filter) { |
|
return (Filter & (1 << (int)filter)) > 0; |
|
} |
|
|
|
#endregion |
|
|
|
|
|
|
|
#region Private Methods |
|
|
|
/// <summary> Convert an object to an understandable string. </summary> |
|
/// <returns>The string.</returns> |
|
/// <param name="s">S.</param> |
|
private static string DebuggableString (object s) { |
|
//For debug purposes the array is checked to evidentiate null objects. |
|
return s != null ? s.ToString () : "null"; |
|
} |
|
|
|
/// <summary> Convert an array to an understandable string array. </summary> |
|
/// <returns>The string.</returns> |
|
/// <param name="array">Array.</param> |
|
private static string[] DebuggableString (params object[] array) { |
|
string[] stringArray = new string[array.Length]; |
|
for (int i = 0; i < array.Length; i++) |
|
stringArray[i] = DebuggableString (array[i]); |
|
return stringArray; |
|
} |
|
|
|
#endregion |
|
|
|
|
|
|
|
#region Log Methods |
|
|
|
#region Public |
|
|
|
/// <summary> Logs a debug message to the Unity Console. </summary> |
|
/// <param name="array">String array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void Log (params object[] array) { |
|
if (IsInFilter (Level.Debug)) |
|
Debug.Log (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs a formated debug message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.Debug)) |
|
Debug.LogFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs an info message to the Unity Console. </summary> |
|
/// <param name="array">String array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogInfo (params object[] array) { |
|
if (IsInFilter (Level.Info)) |
|
Debug.Log (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs a formated info message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogInfoFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.Info)) |
|
Debug.LogFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs an error message to the Unity Console. </summary> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogError (params object[] array) { |
|
if (IsInFilter (Level.Error)) |
|
Debug.LogError (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs an error message to the Unity Console. </summary> |
|
/// <param name="context">Context.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogError (Object context, params object[] array) { |
|
if (IsInFilter (Level.Error)) |
|
Debug.LogError (StringUtils.Concat (DebuggableString (array)), context); |
|
} |
|
|
|
/// <summary> Logs an formated error message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogErrorFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.Error)) |
|
Debug.LogErrorFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs a warning message to the Unity Console. </summary> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogWarning (params object[] array) { |
|
if (IsInFilter (Level.Warning)) |
|
Debug.LogWarning (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs a warning message to the Unity Console. </summary> |
|
/// <param name="context">Context.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogWarning (Object context, params object[] array) { |
|
if (IsInFilter (Level.Warning)) |
|
Debug.LogWarning (StringUtils.Concat (DebuggableString (array)), context); |
|
} |
|
|
|
/// <summary> Logs a formated warning message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogWarningFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.Warning)) |
|
Debug.LogWarningFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs an assertion message to the Unity Console. </summary> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogAssertion (params object[] array) { |
|
if (IsInFilter (Level.Error)) |
|
Debug.LogAssertion (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs an assertion message to the Unity Console. </summary> |
|
/// <param name="context">Context.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogAssertion (Object context, params object[] array) { |
|
if (IsInFilter (Level.Error)) |
|
Debug.LogAssertion (StringUtils.Concat (DebuggableString (array)), context); |
|
} |
|
|
|
/// <summary> Logs a formated assertion message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogAssertionFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.Error)) |
|
Debug.LogAssertionFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs an exception message to the Unity Console. </summary> |
|
/// <param name="exception">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogException (System.Exception exception) { |
|
if (IsInFilter (Level.Error)) |
|
Debug.LogException (exception); |
|
} |
|
|
|
/// <summary> Logs an exception message to the Unity Console. </summary> |
|
/// <param name="context">Context.</param> |
|
/// <param name="exception">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
public static void LogException (Object context, System.Exception exception) { |
|
if (IsInFilter (Level.Error)) |
|
Debug.LogException (exception, context); |
|
} |
|
|
|
#endregion |
|
|
|
#region Internal |
|
|
|
/// <summary> Logs an info message to the Unity Console. </summary> |
|
/// <param name="array">String array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevInfo (params object[] array) { |
|
if (IsInFilter (Level.DevInfo)) |
|
Debug.Log (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs a formated info message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevInfoFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.DevInfo)) |
|
Debug.LogFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs an error message to the Unity Console. </summary> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevError (params object[] array) { |
|
if (IsInFilter (Level.DevError)) |
|
Debug.LogError (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs an error message to the Unity Console. </summary> |
|
/// <param name="context">Context.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevError (Object context, params object[] array) { |
|
if (IsInFilter (Level.DevError)) |
|
Debug.LogError (StringUtils.Concat (DebuggableString (array)), context); |
|
} |
|
|
|
/// <summary> Logs a formated errpr message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevErrorFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.DevError)) |
|
Debug.LogErrorFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs a warning message to the Unity Console. </summary> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevWarning (params object[] array) { |
|
if (IsInFilter (Level.DevWarning)) |
|
Debug.LogWarning (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs a warning message to the Unity Console. </summary> |
|
/// <param name="context">Context.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevWarning (Object context, params object[] array) { |
|
if (IsInFilter (Level.DevWarning)) |
|
Debug.LogWarning (StringUtils.Concat (DebuggableString (array)), context); |
|
} |
|
|
|
/// <summary> Logs a formated warning message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevWarningFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.DevWarning)) |
|
Debug.LogWarningFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs an assertion message to the Unity Console. </summary> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevAssertion (params object[] array) { |
|
if (IsInFilter (Level.DevError)) |
|
Debug.LogAssertion (StringUtils.Concat (DebuggableString (array))); |
|
} |
|
|
|
/// <summary> Logs an assertion message to the Unity Console. </summary> |
|
/// <param name="context">Context.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevAssertion (Object context, params object[] array) { |
|
if (IsInFilter (Level.DevError)) |
|
Debug.LogAssertion (StringUtils.Concat (DebuggableString (array)), context); |
|
} |
|
|
|
/// <summary> Logs a formated assertion message to the Unity Console. </summary> |
|
/// <param name="format">Format.</param> |
|
/// <param name="array">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevAssertionFormat (string format, params object[] array) { |
|
if (IsInFilter (Level.DevError)) |
|
Debug.LogAssertionFormat (format, array); |
|
} |
|
|
|
/// <summary> Logs an error message to the Unity Console. </summary> |
|
/// <param name="exception">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevException (System.Exception exception) { |
|
if (IsInFilter (Level.DevError)) |
|
Debug.LogException (exception); |
|
} |
|
|
|
/// <summary> Logs an error message to the Unity Console. </summary> |
|
/// <param name="context">Context.</param> |
|
/// <param name="exception">Array.</param> |
|
[System.Diagnostics.Conditional ("DEBUG")] |
|
internal static void LogDevException (Object context, System.Exception exception) { |
|
if (IsInFilter (Level.DevError)) |
|
Debug.LogException (exception, context); |
|
} |
|
|
|
#endregion |
|
|
|
#endregion |
|
} |