LogData.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #region License
  2. /*
  3. * LogData.cs
  4. *
  5. * The MIT License
  6. *
  7. * Copyright (c) 2013-2022 sta.blockhead
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. #endregion
  28. using System;
  29. using System.Diagnostics;
  30. using System.Text;
  31. namespace WebSocketSharp
  32. {
  33. /// <summary>
  34. /// Represents a log data used by the <see cref="Logger"/> class.
  35. /// </summary>
  36. public class LogData
  37. {
  38. #region Private Fields
  39. private StackFrame _caller;
  40. private DateTime _date;
  41. private LogLevel _level;
  42. private string _message;
  43. #endregion
  44. #region Internal Constructors
  45. internal LogData (LogLevel level, StackFrame caller, string message)
  46. {
  47. _level = level;
  48. _caller = caller;
  49. _message = message ?? String.Empty;
  50. _date = DateTime.Now;
  51. }
  52. #endregion
  53. #region Public Properties
  54. /// <summary>
  55. /// Gets the information of the logging method caller.
  56. /// </summary>
  57. /// <value>
  58. /// A <see cref="StackFrame"/> that provides the information of
  59. /// the logging method caller.
  60. /// </value>
  61. public StackFrame Caller {
  62. get {
  63. return _caller;
  64. }
  65. }
  66. /// <summary>
  67. /// Gets the date and time when the log data was created.
  68. /// </summary>
  69. /// <value>
  70. /// A <see cref="DateTime"/> that represents the date and time when
  71. /// the log data was created.
  72. /// </value>
  73. public DateTime Date {
  74. get {
  75. return _date;
  76. }
  77. }
  78. /// <summary>
  79. /// Gets the logging level of the log data.
  80. /// </summary>
  81. /// <value>
  82. /// One of the <see cref="LogLevel"/> enum values that represents
  83. /// the logging level of the log data.
  84. /// </value>
  85. public LogLevel Level {
  86. get {
  87. return _level;
  88. }
  89. }
  90. /// <summary>
  91. /// Gets the message of the log data.
  92. /// </summary>
  93. /// <value>
  94. /// A <see cref="string"/> that represents the message of the log data.
  95. /// </value>
  96. public string Message {
  97. get {
  98. return _message;
  99. }
  100. }
  101. #endregion
  102. #region Public Methods
  103. /// <summary>
  104. /// Returns a string that represents the current instance.
  105. /// </summary>
  106. /// <returns>
  107. /// A <see cref="string"/> that represents the current instance.
  108. /// </returns>
  109. public override string ToString ()
  110. {
  111. var date = String.Format ("[{0}]", _date);
  112. var level = String.Format ("{0,-5}", _level.ToString ().ToUpper ());
  113. var method = _caller.GetMethod ();
  114. var type = method.DeclaringType;
  115. #if DEBUG
  116. var num = _caller.GetFileLineNumber ();
  117. var caller = String.Format ("{0}.{1}:{2}", type.Name, method.Name, num);
  118. #else
  119. var caller = String.Format ("{0}.{1}", type.Name, method.Name);
  120. #endif
  121. var msgs = _message.Replace ("\r\n", "\n").TrimEnd ('\n').Split ('\n');
  122. if (msgs.Length <= 1)
  123. return String.Format ("{0} {1} {2} {3}", date, level, caller, _message);
  124. var buff = new StringBuilder (64);
  125. buff.AppendFormat ("{0} {1} {2}\n\n", date, level, caller);
  126. for (var i = 0; i < msgs.Length; i++)
  127. buff.AppendFormat (" {0}\n", msgs[i]);
  128. return buff.ToString ();
  129. }
  130. #endregion
  131. }
  132. }