DefaultLogger.cs 5.0 KB

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