Logger.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.IO;
  4. using System;
  5. namespace LunarCatsStudio.SuperCombiner
  6. {
  7. public class Logger
  8. {
  9. string _logs = " ";
  10. static Logger _instance;
  11. static bool _display = true;
  12. /// <summary>
  13. /// logs level
  14. /// </summary>
  15. public enum LogLevel
  16. {
  17. LOG_DEBUG,
  18. LOG_WARNING,
  19. LOG_ERROR
  20. }
  21. /// <summary>
  22. /// get instance of the logger singleton
  23. /// </summary>
  24. public static Logger Instance
  25. {
  26. get
  27. {
  28. if (_instance == null)
  29. {
  30. _instance = new Logger();
  31. }
  32. return _instance;
  33. }
  34. }
  35. /// <summary>
  36. /// General switch for display or not new logs add in logger store
  37. /// </summary>
  38. public static bool Display
  39. {
  40. get
  41. {
  42. return _display;
  43. }
  44. set
  45. {
  46. _display = value;
  47. }
  48. }
  49. /// <summary>
  50. /// add new log in the logger store
  51. /// </summary>
  52. /// <param name="tag"> log tag</param>
  53. /// <param name="log">log message</param>
  54. /// <param name="level">log level</param>
  55. /// <param name="display">display or not this new log in editor console</param>
  56. public void AddLog(string tag, string log, LogLevel level = LogLevel.LOG_DEBUG, bool display = true)
  57. {
  58. string log_type = " ";
  59. string new_log = "[" + tag + "] " + log;
  60. switch (level)
  61. {
  62. case LogLevel.LOG_WARNING:
  63. log_type = "WARNING: ";
  64. if (display && _display) Debug.LogWarning(new_log);
  65. break;
  66. case LogLevel.LOG_ERROR:
  67. log_type = "ERROR: ";
  68. if (display && _display) Debug.LogError(new_log);
  69. break;
  70. default:
  71. log_type = "DEBUG: ";
  72. if (display && _display) Debug.Log(new_log);
  73. break;
  74. }
  75. _logs = _logs + log_type + new_log + "\n";
  76. }
  77. /// <summary>
  78. /// clear logs store
  79. /// </summary>
  80. public void ClearLogs()
  81. {
  82. _logs = " ";
  83. }
  84. /// <summary>
  85. /// get logs store content
  86. /// </summary>
  87. /// <returns></returns>
  88. public string GetLogs()
  89. {
  90. return _logs;
  91. }
  92. }
  93. }