Logger.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace UniRx.Diagnostics
  6. {
  7. public partial class Logger
  8. {
  9. static bool isInitialized = false;
  10. static bool isDebugBuild = false;
  11. public string Name { get; private set; }
  12. protected readonly Action<LogEntry> logPublisher;
  13. public Logger(string loggerName)
  14. {
  15. this.Name = loggerName;
  16. this.logPublisher = ObservableLogger.RegisterLogger(this);
  17. }
  18. /// <summary>Output LogType.Log but only enables isDebugBuild</summary>
  19. public virtual void Debug(object message, UnityEngine.Object context = null)
  20. {
  21. if (!isInitialized)
  22. {
  23. isInitialized = true;
  24. isDebugBuild = UnityEngine.Debug.isDebugBuild;
  25. }
  26. if (isDebugBuild)
  27. {
  28. logPublisher(new LogEntry(
  29. message: (message != null) ? message.ToString() : "",
  30. logType: LogType.Log,
  31. timestamp: DateTime.Now,
  32. loggerName: Name,
  33. context: context));
  34. }
  35. }
  36. /// <summary>Output LogType.Log but only enables isDebugBuild</summary>
  37. public virtual void DebugFormat(string format, params object[] args)
  38. {
  39. if (!isInitialized)
  40. {
  41. isInitialized = true;
  42. isDebugBuild = UnityEngine.Debug.isDebugBuild;
  43. }
  44. if (isDebugBuild)
  45. {
  46. logPublisher(new LogEntry(
  47. message: (format != null) ? string.Format(format, args) : "",
  48. logType: LogType.Log,
  49. timestamp: DateTime.Now,
  50. loggerName: Name,
  51. context: null));
  52. }
  53. }
  54. public virtual void Log(object message, UnityEngine.Object context = null)
  55. {
  56. logPublisher(new LogEntry(
  57. message: (message != null) ? message.ToString() : "",
  58. logType: LogType.Log,
  59. timestamp: DateTime.Now,
  60. loggerName: Name,
  61. context: context));
  62. }
  63. public virtual void LogFormat(string format, params object[] args)
  64. {
  65. logPublisher(new LogEntry(
  66. message: (format != null) ? string.Format(format, args) : "",
  67. logType: LogType.Log,
  68. timestamp: DateTime.Now,
  69. loggerName: Name,
  70. context: null));
  71. }
  72. public virtual void Warning(object message, UnityEngine.Object context = null)
  73. {
  74. logPublisher(new LogEntry(
  75. message: (message != null) ? message.ToString() : "",
  76. logType: LogType.Warning,
  77. timestamp: DateTime.Now,
  78. loggerName: Name,
  79. context: context));
  80. }
  81. public virtual void WarningFormat(string format, params object[] args)
  82. {
  83. logPublisher(new LogEntry(
  84. message: (format != null) ? string.Format(format, args) : "",
  85. logType: LogType.Warning,
  86. timestamp: DateTime.Now,
  87. loggerName: Name,
  88. context: null));
  89. }
  90. public virtual void Error(object message, UnityEngine.Object context = null)
  91. {
  92. logPublisher(new LogEntry(
  93. message: (message != null) ? message.ToString() : "",
  94. logType: LogType.Error,
  95. timestamp: DateTime.Now,
  96. loggerName: Name,
  97. context: context));
  98. }
  99. public virtual void ErrorFormat(string format, params object[] args)
  100. {
  101. logPublisher(new LogEntry(
  102. message: (format != null) ? string.Format(format, args) : "",
  103. logType: LogType.Error,
  104. timestamp: DateTime.Now,
  105. loggerName: Name,
  106. context: null));
  107. }
  108. public virtual void Exception(Exception exception, UnityEngine.Object context = null)
  109. {
  110. logPublisher(new LogEntry(
  111. message: (exception != null) ? exception.ToString() : "",
  112. exception: exception,
  113. logType: LogType.Exception,
  114. timestamp: DateTime.Now,
  115. loggerName: Name,
  116. context: context));
  117. }
  118. /// <summary>Publish raw LogEntry.</summary>
  119. public virtual void Raw(LogEntry logEntry)
  120. {
  121. logPublisher(logEntry);
  122. }
  123. }
  124. }