DebugLogEntry.cs 4.7 KB

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