DefaultLogger.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace BestHTTP.Logger
  6. {
  7. /// <summary>
  8. /// A basic logger implementation to be able to log intelligently additional informations about the plugin's internal mechanism.
  9. /// </summary>
  10. public class DefaultLogger : BestHTTP.Logger.ILogger
  11. {
  12. public Loglevels Level { get; set; }
  13. public string FormatVerbose { get; set; }
  14. public string FormatInfo { get; set; }
  15. public string FormatWarn { get; set; }
  16. public string FormatErr { get; set; }
  17. public string FormatEx { get; set; }
  18. public DefaultLogger()
  19. {
  20. FormatVerbose = "[{0}] D [{1}]: {2}";
  21. FormatInfo = "[{0}] I [{1}]: {2}";
  22. FormatWarn = "[{0}] W [{1}]: {2}";
  23. FormatErr = "[{0}] Err [{1}]: {2}";
  24. FormatEx = "[{0}] Ex [{1}]: {2} - Message: {3} StackTrace: {4}";
  25. Level = UnityEngine.Debug.isDebugBuild ? Loglevels.Warning : Loglevels.Error;
  26. }
  27. public void Verbose(string division, string verb)
  28. {
  29. if (Level <= Loglevels.All)
  30. {
  31. try
  32. {
  33. UnityEngine.Debug.Log(string.Format(FormatVerbose, GetFormattedTime(), division, verb));
  34. }
  35. catch
  36. { }
  37. }
  38. }
  39. public void Information(string division, string info)
  40. {
  41. if (Level <= Loglevels.Information)
  42. {
  43. try
  44. {
  45. UnityEngine.Debug.Log(string.Format(FormatInfo, GetFormattedTime(), division, info));
  46. }
  47. catch
  48. { }
  49. }
  50. }
  51. public void Warning(string division, string warn)
  52. {
  53. if (Level <= Loglevels.Warning)
  54. {
  55. try
  56. {
  57. UnityEngine.Debug.LogWarning(string.Format(FormatWarn, GetFormattedTime(), division, warn));
  58. }
  59. catch
  60. { }
  61. }
  62. }
  63. public void Error(string division, string err)
  64. {
  65. if (Level <= Loglevels.Error)
  66. {
  67. try
  68. {
  69. UnityEngine.Debug.LogError(string.Format(FormatErr, GetFormattedTime(), division, err));
  70. }
  71. catch
  72. { }
  73. }
  74. }
  75. public void Exception(string division, string msg, Exception ex)
  76. {
  77. if (Level <= Loglevels.Exception)
  78. {
  79. try
  80. {
  81. string exceptionMessage = string.Empty;
  82. if (ex == null)
  83. exceptionMessage = "null";
  84. else
  85. {
  86. StringBuilder sb = new StringBuilder();
  87. Exception exception = ex;
  88. int counter = 1;
  89. while (exception != null)
  90. {
  91. sb.AppendFormat("{0}: {1} {2}", counter++.ToString(), exception.Message, exception.StackTrace);
  92. exception = exception.InnerException;
  93. if (exception != null)
  94. sb.AppendLine();
  95. }
  96. exceptionMessage = sb.ToString();
  97. }
  98. UnityEngine.Debug.LogError(string.Format(FormatEx,
  99. GetFormattedTime(),
  100. division,
  101. msg,
  102. exceptionMessage,
  103. ex != null ? ex.StackTrace : "null"));
  104. }
  105. catch
  106. { }
  107. }
  108. }
  109. private string GetFormattedTime()
  110. {
  111. return DateTime.Now.Ticks.ToString();
  112. }
  113. }
  114. }