Logger.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEngine;
  2. namespace Kamgam.SkyClouds.URP
  3. {
  4. public class Logger
  5. {
  6. public delegate void LogCallback(string msg, LogLevel logLevel);
  7. public const string Prefix = "SkyClouds.URP: ";
  8. public static LogLevel CurrentLogLevel = LogLevel.Warning;
  9. /// <summary>
  10. /// Optional: leave as is or set to NULL to not use it.<br />
  11. /// Set this to a function which returns the log level (from settings for example).<br />
  12. /// This will be called before every log.
  13. /// <example>
  14. /// [RuntimeInitializeOnLoadMethod]
  15. /// private static void HookUpToLogger()
  16. /// {
  17. /// Logger.OnGetLogLevel = () => GetOrCreateSettings().LogLevel;
  18. /// }
  19. /// </example>
  20. /// </summary>
  21. public static System.Func<LogLevel> OnGetLogLevel = null;
  22. public enum LogLevel
  23. {
  24. Log = 0,
  25. Warning = 1,
  26. Error = 2,
  27. Message = 3,
  28. NoLogs = 99
  29. }
  30. public static bool IsLogLevelVisible(LogLevel logLevel)
  31. {
  32. return (int)logLevel >= (int)CurrentLogLevel;
  33. }
  34. public static void UpdateCurrentLogLevel()
  35. {
  36. if (OnGetLogLevel != null)
  37. {
  38. CurrentLogLevel = OnGetLogLevel();
  39. }
  40. }
  41. public static void Log(string message, GameObject go = null)
  42. {
  43. UpdateCurrentLogLevel();
  44. if (IsLogLevelVisible(LogLevel.Log))
  45. {
  46. if (go == null)
  47. Debug.Log(Prefix + message);
  48. else
  49. Debug.Log(Prefix + message, go);
  50. }
  51. }
  52. public static void LogWarning(string message, GameObject go = null)
  53. {
  54. UpdateCurrentLogLevel();
  55. if (IsLogLevelVisible(LogLevel.Warning))
  56. {
  57. if (go == null)
  58. Debug.LogWarning(Prefix + message);
  59. else
  60. Debug.LogWarning(Prefix + message, go);
  61. }
  62. }
  63. public static void LogError(string message, GameObject go = null)
  64. {
  65. UpdateCurrentLogLevel();
  66. if (IsLogLevelVisible(LogLevel.Error))
  67. {
  68. if (go == null)
  69. Debug.LogError(Prefix + message);
  70. else
  71. Debug.LogError(Prefix + message, go);
  72. }
  73. }
  74. public static void LogMessage(string message, GameObject go = null)
  75. {
  76. UpdateCurrentLogLevel();
  77. if (IsLogLevelVisible(LogLevel.Message))
  78. {
  79. if (go == null)
  80. Debug.Log(Prefix + message);
  81. else
  82. Debug.Log(Prefix + message, go);
  83. }
  84. }
  85. }
  86. }