No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UniWebViewLogger.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /// Critical level. When set to `Critical`, the logger will only log out errors or exceptions.
  40. /// </summary>
  41. Critical = 80,
  42. /// <summary>
  43. /// Off level. When set to `Off`, the logger will log out nothing.
  44. /// </summary>
  45. Off = 99
  46. }
  47. private static UniWebViewLogger instance;
  48. private Level level;
  49. /// <summary>
  50. /// Current level of this logger. All messages above current level will be logged out.
  51. /// Default is `Critical`, which means the logger only prints errors and exceptions.
  52. /// </summary>
  53. public Level LogLevel {
  54. get { return level; }
  55. set {
  56. Log(Level.Off, "Setting UniWebView logger level to: " + value);
  57. level = value;
  58. UniWebViewInterface.SetLogLevel((int)value);
  59. }
  60. }
  61. private UniWebViewLogger(Level level) {
  62. this.level = level;
  63. }
  64. /// <summary>
  65. /// Instance of the UniWebView logger across the process. Normally you should use this for logging purpose
  66. /// in UniWebView, instead of creating a new logger yourself.
  67. /// </summary>
  68. public static UniWebViewLogger Instance {
  69. get {
  70. if (instance == null) {
  71. instance = new UniWebViewLogger(Level.Critical);
  72. }
  73. return instance;
  74. }
  75. }
  76. /// <summary>
  77. /// Log a verbose message.
  78. /// </summary>
  79. /// <param name="message">The message to log.</param>
  80. public void Verbose(string message) { Log(Level.Verbose, message); }
  81. /// <summary>
  82. /// Log a debug message.
  83. /// </summary>
  84. /// <param name="message">The message to log.</param>
  85. public void Debug(string message) { Log(Level.Debug, message); }
  86. /// <summary>
  87. /// Log an info message.
  88. /// </summary>
  89. /// <param name="message">The message to log.</param>
  90. public void Info(string message) { Log(Level.Info, message); }
  91. /// <summary>
  92. /// Log a critical message.
  93. /// </summary>
  94. /// <param name="message">The message to log.</param>
  95. public void Critical(string message) { Log(Level.Critical, message); }
  96. // ReSharper disable Unity.PerformanceAnalysis
  97. private void Log(Level targetLevel, string message) {
  98. if (targetLevel >= this.LogLevel) {
  99. var logMessage = "<UniWebView> " + message;
  100. if (targetLevel == Level.Critical) {
  101. UnityEngine.Debug.LogError(logMessage);
  102. } else {
  103. UnityEngine.Debug.Log(logMessage);
  104. }
  105. }
  106. }
  107. }