UniWebViewLogger.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // UniWebViewLogger.cs
  3. // Created by Wang Wei(@onevcat) on 2017-04-11.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. /// <summary>
  18. /// A leveled logger which could log UniWebView related messages in
  19. /// both development environment and final product.
  20. /// </summary>
  21. public class UniWebViewLogger {
  22. /// <summary>
  23. /// Logger level.
  24. /// </summary>
  25. public enum Level {
  26. /// <summary>
  27. /// Lowest level. When set to `Verbose`, the logger will log out all messages.
  28. /// </summary>
  29. Verbose = 0,
  30. /// <summary>
  31. /// Debug level. When set to `Debug`, the logger will log out most of messages up to this level.
  32. /// </summary>
  33. Debug = 10,
  34. /// <summary>
  35. /// Info level. When set to `Info`, the logger will log out up to info messages.
  36. /// </summary>
  37. Info = 20,
  38. /// <summary>
  39. /// Warning level. When set to `Warning`, the logger will log out warnings and non-critical error messages.
  40. /// </summary>
  41. Warning = 30,
  42. /// <summary>
  43. /// Critical level. When set to `Critical`, the logger will only log out errors or exceptions.
  44. /// </summary>
  45. Critical = 80,
  46. /// <summary>
  47. /// Off level. When set to `Off`, the logger will log out nothing.
  48. /// </summary>
  49. Off = 99
  50. }
  51. private static UniWebViewLogger instance;
  52. private Level level;
  53. /// <summary>
  54. /// Current level of this logger. All messages above current level will be logged out.
  55. /// Default is `Critical`, which means the logger only prints errors and exceptions.
  56. /// </summary>
  57. public Level LogLevel {
  58. get => level;
  59. set {
  60. Log(Level.Off, "Setting UniWebView logger level to: " + value);
  61. level = value;
  62. UniWebViewInterface.SetLogLevel((int)value);
  63. }
  64. }
  65. private UniWebViewLogger(Level level) {
  66. this.level = level;
  67. }
  68. /// <summary>
  69. /// Instance of the UniWebView logger across the process. Normally you should use this for logging purpose
  70. /// in UniWebView, instead of creating a new logger yourself.
  71. /// </summary>
  72. public static UniWebViewLogger Instance {
  73. get {
  74. if (instance == null) {
  75. instance = new UniWebViewLogger(Level.Critical);
  76. }
  77. return instance;
  78. }
  79. }
  80. /// <summary>
  81. /// Log a verbose message.
  82. /// </summary>
  83. /// <param name="message">The message to log.</param>
  84. public void Verbose(string message) { Log(Level.Verbose, message); }
  85. /// <summary>
  86. /// Log a verbose message with lazy evaluation.
  87. /// </summary>
  88. /// <param name="messageProvider">The message provider function.</param>
  89. public void Verbose(System.Func<string> messageProvider) { Log(Level.Verbose, messageProvider); }
  90. /// <summary>
  91. /// Log a debug message.
  92. /// </summary>
  93. /// <param name="message">The message to log.</param>
  94. public void Debug(string message) { Log(Level.Debug, message); }
  95. /// <summary>
  96. /// Log a debug message with lazy evaluation.
  97. /// </summary>
  98. /// <param name="messageProvider">The message provider function.</param>
  99. public void Debug(System.Func<string> messageProvider) { Log(Level.Debug, messageProvider); }
  100. /// <summary>
  101. /// Log an info message.
  102. /// </summary>
  103. /// <param name="message">The message to log.</param>
  104. public void Info(string message) { Log(Level.Info, message); }
  105. /// <summary>
  106. /// Log an info message with lazy evaluation.
  107. /// </summary>
  108. /// <param name="messageProvider">The message provider function.</param>
  109. public void Info(System.Func<string> messageProvider) { Log(Level.Info, messageProvider); }
  110. /// <summary>
  111. /// Log a warning message.
  112. /// </summary>
  113. /// <param name="message">The message to log.</param>
  114. public void Warning(string message) { Log(Level.Warning, message); }
  115. /// <summary>
  116. /// Log a warning message with lazy evaluation.
  117. /// </summary>
  118. /// <param name="messageProvider">The message provider function.</param>
  119. public void Warning(System.Func<string> messageProvider) { Log(Level.Warning, messageProvider); }
  120. /// <summary>
  121. /// Log a critical message.
  122. /// </summary>
  123. /// <param name="message">The message to log.</param>
  124. public void Critical(string message) { Log(Level.Critical, message); }
  125. /// <summary>
  126. /// Log a critical message with lazy evaluation.
  127. /// </summary>
  128. /// <param name="messageProvider">The message provider function.</param>
  129. public void Critical(System.Func<string> messageProvider) { Log(Level.Critical, messageProvider); }
  130. // ReSharper disable Unity.PerformanceAnalysis
  131. private void Log(Level targetLevel, string message) {
  132. if (targetLevel >= this.LogLevel) {
  133. var truncatedMessage = TruncateMessage(message);
  134. var logMessage = "<UniWebView> " + truncatedMessage;
  135. if (targetLevel == Level.Critical) {
  136. UnityEngine.Debug.LogError(logMessage);
  137. } else {
  138. UnityEngine.Debug.Log(logMessage);
  139. }
  140. }
  141. }
  142. // ReSharper disable Unity.PerformanceAnalysis
  143. private void Log(Level targetLevel, System.Func<string> messageProvider) {
  144. if (targetLevel >= this.LogLevel) {
  145. Log(targetLevel, messageProvider());
  146. }
  147. }
  148. private string TruncateMessage(string message) {
  149. if (string.IsNullOrEmpty(message)) {
  150. return message;
  151. }
  152. if (message.Length > 5000) {
  153. return message.Substring(0, 5000) + "...truncated";
  154. }
  155. return message;
  156. }
  157. }