DebugLogEntry.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using UnityEngine;
  2. // Container for a simple debug entry
  3. namespace IngameDebugConsole
  4. {
  5. public class DebugLogEntry : System.IEquatable<DebugLogEntry>
  6. {
  7. private const int HASH_NOT_CALCULATED = -623218;
  8. public string logString;
  9. public string stackTrace;
  10. private string completeLog;
  11. // Sprite to show with this entry
  12. public Sprite logTypeSpriteRepresentation;
  13. // Collapsed count
  14. public int count;
  15. private int hashValue;
  16. public void Initialize( string logString, string stackTrace )
  17. {
  18. this.logString = logString;
  19. this.stackTrace = stackTrace;
  20. completeLog = null;
  21. count = 1;
  22. hashValue = HASH_NOT_CALCULATED;
  23. }
  24. // Check if two entries have the same origin
  25. public bool Equals( DebugLogEntry other )
  26. {
  27. return this.logString == other.logString && this.stackTrace == other.stackTrace;
  28. }
  29. // Checks if logString or stackTrace contains the search term
  30. public bool MatchesSearchTerm( string searchTerm )
  31. {
  32. return ( logString != null && logString.IndexOf( searchTerm, System.StringComparison.OrdinalIgnoreCase ) >= 0 ) ||
  33. ( stackTrace != null && stackTrace.IndexOf( searchTerm, System.StringComparison.OrdinalIgnoreCase ) >= 0 );
  34. }
  35. // Return a string containing complete information about this debug entry
  36. public override string ToString()
  37. {
  38. if( completeLog == null )
  39. completeLog = string.Concat( logString, "\n", stackTrace );
  40. return completeLog;
  41. }
  42. // Credit: https://stackoverflow.com/a/19250516/2373034
  43. public override int GetHashCode()
  44. {
  45. if( hashValue == HASH_NOT_CALCULATED )
  46. {
  47. unchecked
  48. {
  49. hashValue = 17;
  50. hashValue = hashValue * 23 + logString == null ? 0 : logString.GetHashCode();
  51. hashValue = hashValue * 23 + stackTrace == null ? 0 : stackTrace.GetHashCode();
  52. }
  53. }
  54. return hashValue;
  55. }
  56. }
  57. public struct QueuedDebugLogEntry
  58. {
  59. public readonly string logString;
  60. public readonly string stackTrace;
  61. public readonly LogType logType;
  62. public QueuedDebugLogEntry( string logString, string stackTrace, LogType logType )
  63. {
  64. this.logString = logString;
  65. this.stackTrace = stackTrace;
  66. this.logType = logType;
  67. }
  68. // Checks if logString or stackTrace contains the search term
  69. public bool MatchesSearchTerm( string searchTerm )
  70. {
  71. return ( logString != null && logString.IndexOf( searchTerm, System.StringComparison.OrdinalIgnoreCase ) >= 0 ) ||
  72. ( stackTrace != null && stackTrace.IndexOf( searchTerm, System.StringComparison.OrdinalIgnoreCase ) >= 0 );
  73. }
  74. }
  75. }