Log.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using UnityEngine;
  3. using Conditional = System.Diagnostics.ConditionalAttribute;
  4. namespace Mirror.SimpleWeb
  5. {
  6. public static class Log
  7. {
  8. // used for Conditional
  9. const string SIMPLEWEB_LOG_ENABLED = nameof(SIMPLEWEB_LOG_ENABLED);
  10. const string DEBUG = nameof(DEBUG);
  11. public enum Levels
  12. {
  13. none = 0,
  14. error = 1,
  15. warn = 2,
  16. info = 3,
  17. verbose = 4,
  18. }
  19. public static Levels level = Levels.none;
  20. public static string BufferToString(byte[] buffer, int offset = 0, int? length = null)
  21. {
  22. return BitConverter.ToString(buffer, offset, length ?? buffer.Length);
  23. }
  24. [Conditional(SIMPLEWEB_LOG_ENABLED)]
  25. public static void DumpBuffer(string label, byte[] buffer, int offset, int length)
  26. {
  27. if (level < Levels.verbose)
  28. return;
  29. Debug.Log($"VERBOSE: <color=blue>{label}: {BufferToString(buffer, offset, length)}</color>");
  30. }
  31. [Conditional(SIMPLEWEB_LOG_ENABLED)]
  32. public static void DumpBuffer(string label, ArrayBuffer arrayBuffer)
  33. {
  34. if (level < Levels.verbose)
  35. return;
  36. Debug.Log($"VERBOSE: <color=blue>{label}: {BufferToString(arrayBuffer.array, 0, arrayBuffer.count)}</color>");
  37. }
  38. [Conditional(SIMPLEWEB_LOG_ENABLED)]
  39. public static void Verbose(string msg, bool showColor = true)
  40. {
  41. if (level < Levels.verbose)
  42. return;
  43. if (showColor)
  44. Debug.Log($"VERBOSE: <color=blue>{msg}</color>");
  45. else
  46. Debug.Log($"VERBOSE: {msg}");
  47. }
  48. [Conditional(SIMPLEWEB_LOG_ENABLED)]
  49. public static void Info(string msg, bool showColor = true)
  50. {
  51. if (level < Levels.info)
  52. return;
  53. if (showColor)
  54. Debug.Log($"INFO: <color=blue>{msg}</color>");
  55. else
  56. Debug.Log($"INFO: {msg}");
  57. }
  58. /// <summary>
  59. /// An expected Exception was caught, useful for debugging but not important
  60. /// </summary>
  61. /// <param name="msg"></param>
  62. /// <param name="showColor"></param>
  63. [Conditional(SIMPLEWEB_LOG_ENABLED)]
  64. public static void InfoException(Exception e)
  65. {
  66. if (level < Levels.info)
  67. return;
  68. Debug.Log($"INFO_EXCEPTION: <color=blue>{e.GetType().Name}</color> Message: {e.Message}");
  69. }
  70. [Conditional(SIMPLEWEB_LOG_ENABLED), Conditional(DEBUG)]
  71. public static void Warn(string msg, bool showColor = true)
  72. {
  73. if (level < Levels.warn)
  74. return;
  75. if (showColor)
  76. Debug.LogWarning($"WARN: <color=orange>{msg}</color>");
  77. else
  78. Debug.LogWarning($"WARN: {msg}");
  79. }
  80. [Conditional(SIMPLEWEB_LOG_ENABLED), Conditional(DEBUG)]
  81. public static void Error(string msg, bool showColor = true)
  82. {
  83. if (level < Levels.error)
  84. return;
  85. if (showColor)
  86. Debug.LogError($"ERROR: <color=red>{msg}</color>");
  87. else
  88. Debug.LogError($"ERROR: {msg}");
  89. }
  90. public static void Exception(Exception e)
  91. {
  92. // always log Exceptions
  93. Debug.LogError($"EXCEPTION: <color=red>{e.GetType().Name}</color> Message: {e.Message}");
  94. }
  95. }
  96. }