DebugLogEntry.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System.Collections.Generic;
  2. using System.Globalization;
  3. using System.Text;
  4. using UnityEngine;
  5. // Container for a simple debug entry
  6. namespace IngameDebugConsole
  7. {
  8. public class DebugLogEntry
  9. {
  10. private const int HASH_NOT_CALCULATED = -623218;
  11. public string logString;
  12. public string stackTrace;
  13. private string completeLog;
  14. // Sprite to show with this entry
  15. public LogType logType;
  16. // Collapsed count
  17. public int count;
  18. // Index of this entry among all collapsed entries
  19. public int collapsedIndex;
  20. private int hashValue;
  21. public void Initialize( string logString, string stackTrace )
  22. {
  23. this.logString = logString;
  24. this.stackTrace = stackTrace;
  25. completeLog = null;
  26. count = 1;
  27. hashValue = HASH_NOT_CALCULATED;
  28. }
  29. public void Clear()
  30. {
  31. logString = null;
  32. stackTrace = null;
  33. completeLog = null;
  34. }
  35. // Checks if logString or stackTrace contains the search term
  36. public bool MatchesSearchTerm( string searchTerm )
  37. {
  38. return ( logString != null && DebugLogConsole.caseInsensitiveComparer.IndexOf( logString, searchTerm, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) >= 0 ) ||
  39. ( stackTrace != null && DebugLogConsole.caseInsensitiveComparer.IndexOf( stackTrace, searchTerm, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) >= 0 );
  40. }
  41. // Return a string containing complete information about this debug entry
  42. public override string ToString()
  43. {
  44. if( completeLog == null )
  45. completeLog = string.Concat( logString, "\n", stackTrace );
  46. return completeLog;
  47. }
  48. // Credit: https://stackoverflow.com/a/19250516/2373034
  49. public int GetContentHashCode()
  50. {
  51. if( hashValue == HASH_NOT_CALCULATED )
  52. {
  53. unchecked
  54. {
  55. hashValue = 17;
  56. hashValue = hashValue * 23 + ( logString == null ? 0 : logString.GetHashCode() );
  57. hashValue = hashValue * 23 + ( stackTrace == null ? 0 : stackTrace.GetHashCode() );
  58. }
  59. }
  60. return hashValue;
  61. }
  62. }
  63. public struct QueuedDebugLogEntry
  64. {
  65. public readonly string logString;
  66. public readonly string stackTrace;
  67. public readonly LogType logType;
  68. public QueuedDebugLogEntry( string logString, string stackTrace, LogType logType )
  69. {
  70. this.logString = logString;
  71. this.stackTrace = stackTrace;
  72. this.logType = logType;
  73. }
  74. // Checks if logString or stackTrace contains the search term
  75. public bool MatchesSearchTerm( string searchTerm )
  76. {
  77. return ( logString != null && DebugLogConsole.caseInsensitiveComparer.IndexOf( logString, searchTerm, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) >= 0 ) ||
  78. ( stackTrace != null && DebugLogConsole.caseInsensitiveComparer.IndexOf( stackTrace, searchTerm, CompareOptions.IgnoreCase | CompareOptions.IgnoreNonSpace ) >= 0 );
  79. }
  80. }
  81. public struct DebugLogEntryTimestamp
  82. {
  83. public readonly System.DateTime dateTime;
  84. #if !IDG_OMIT_ELAPSED_TIME
  85. public readonly float elapsedSeconds;
  86. #endif
  87. #if !IDG_OMIT_FRAMECOUNT
  88. public readonly int frameCount;
  89. #endif
  90. #if !IDG_OMIT_ELAPSED_TIME && !IDG_OMIT_FRAMECOUNT
  91. public DebugLogEntryTimestamp( System.DateTime dateTime, float elapsedSeconds, int frameCount )
  92. #elif !IDG_OMIT_ELAPSED_TIME
  93. public DebugLogEntryTimestamp( System.DateTime dateTime, float elapsedSeconds )
  94. #elif !IDG_OMIT_FRAMECOUNT
  95. public DebugLogEntryTimestamp( System.DateTime dateTime, int frameCount )
  96. #else
  97. public DebugLogEntryTimestamp( System.DateTime dateTime )
  98. #endif
  99. {
  100. this.dateTime = dateTime;
  101. #if !IDG_OMIT_ELAPSED_TIME
  102. this.elapsedSeconds = elapsedSeconds;
  103. #endif
  104. #if !IDG_OMIT_FRAMECOUNT
  105. this.frameCount = frameCount;
  106. #endif
  107. }
  108. public void AppendTime( StringBuilder sb )
  109. {
  110. // Add DateTime in format: [HH:mm:ss]
  111. sb.Append( "[" );
  112. int hour = dateTime.Hour;
  113. if( hour >= 10 )
  114. sb.Append( hour );
  115. else
  116. sb.Append( "0" ).Append( hour );
  117. sb.Append( ":" );
  118. int minute = dateTime.Minute;
  119. if( minute >= 10 )
  120. sb.Append( minute );
  121. else
  122. sb.Append( "0" ).Append( minute );
  123. sb.Append( ":" );
  124. int second = dateTime.Second;
  125. if( second >= 10 )
  126. sb.Append( second );
  127. else
  128. sb.Append( "0" ).Append( second );
  129. sb.Append( "]" );
  130. }
  131. public void AppendFullTimestamp( StringBuilder sb )
  132. {
  133. AppendTime( sb );
  134. #if !IDG_OMIT_ELAPSED_TIME && !IDG_OMIT_FRAMECOUNT
  135. // Append elapsed seconds and frame count in format: [1.0s at #Frame]
  136. sb.Append( "[" ).Append( elapsedSeconds.ToString( "F1" ) ).Append( "s at " ).Append( "#" ).Append( frameCount ).Append( "]" );
  137. #elif !IDG_OMIT_ELAPSED_TIME
  138. // Append elapsed seconds in format: [1.0s]
  139. sb.Append( "[" ).Append( elapsedSeconds.ToString( "F1" ) ).Append( "s]" );
  140. #elif !IDG_OMIT_FRAMECOUNT
  141. // Append frame count in format: [#Frame]
  142. sb.Append( "[#" ).Append( frameCount ).Append( "]" );
  143. #endif
  144. }
  145. }
  146. public class DebugLogEntryContentEqualityComparer : EqualityComparer<DebugLogEntry>
  147. {
  148. public override bool Equals( DebugLogEntry x, DebugLogEntry y )
  149. {
  150. return x.logString == y.logString && x.stackTrace == y.stackTrace;
  151. }
  152. public override int GetHashCode( DebugLogEntry obj )
  153. {
  154. return obj.GetContentHashCode();
  155. }
  156. }
  157. }