DebugLogEntry.cs 4.3 KB

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