IConsoleService.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. namespace SRDebugger.Services
  2. {
  3. using UnityEngine;
  4. public delegate void ConsoleUpdatedEventHandler(IConsoleService console);
  5. public interface IConsoleService
  6. {
  7. int ErrorCount { get; }
  8. int WarningCount { get; }
  9. int InfoCount { get; }
  10. /// <summary>
  11. /// List of ConsoleEntry objects since the last clear.
  12. /// </summary>
  13. IReadOnlyList<ConsoleEntry> Entries { get; }
  14. /// <summary>
  15. /// List of all ConsoleEntry objects, regardless of clear.
  16. /// </summary>
  17. IReadOnlyList<ConsoleEntry> AllEntries { get; }
  18. event ConsoleUpdatedEventHandler Updated;
  19. event ConsoleUpdatedEventHandler Error;
  20. bool LoggingEnabled { get; set; }
  21. bool LogHandlerIsOverriden { get; }
  22. void Clear();
  23. }
  24. public class ConsoleEntry
  25. {
  26. private const int MessagePreviewLength = 180;
  27. private const int StackTracePreviewLength = 120;
  28. private string _messagePreview;
  29. private string _stackTracePreview;
  30. /// <summary>
  31. /// Number of times this log entry has occured (if collapsing is enabled)
  32. /// </summary>
  33. public int Count = 1;
  34. public LogType LogType;
  35. public string Message;
  36. public string StackTrace;
  37. public ConsoleEntry() {}
  38. public ConsoleEntry(ConsoleEntry other)
  39. {
  40. Message = other.Message;
  41. StackTrace = other.StackTrace;
  42. LogType = other.LogType;
  43. Count = other.Count;
  44. }
  45. public string MessagePreview
  46. {
  47. get
  48. {
  49. if (_messagePreview != null)
  50. {
  51. return _messagePreview;
  52. }
  53. if (string.IsNullOrEmpty(Message))
  54. {
  55. return "";
  56. }
  57. _messagePreview = Message.Split('\n')[0];
  58. _messagePreview = _messagePreview.Substring(0, Mathf.Min(_messagePreview.Length, MessagePreviewLength));
  59. return _messagePreview;
  60. }
  61. }
  62. public string StackTracePreview
  63. {
  64. get
  65. {
  66. if (_stackTracePreview != null)
  67. {
  68. return _stackTracePreview;
  69. }
  70. if (string.IsNullOrEmpty(StackTrace))
  71. {
  72. return "";
  73. }
  74. _stackTracePreview = StackTrace.Split('\n')[0];
  75. _stackTracePreview = _stackTracePreview.Substring(0,
  76. Mathf.Min(_stackTracePreview.Length, StackTracePreviewLength));
  77. return _stackTracePreview;
  78. }
  79. }
  80. public bool Matches(ConsoleEntry other)
  81. {
  82. if (ReferenceEquals(null, other))
  83. {
  84. return false;
  85. }
  86. if (ReferenceEquals(this, other))
  87. {
  88. return true;
  89. }
  90. return string.Equals(Message, other.Message) && string.Equals(StackTrace, other.StackTrace) &&
  91. LogType == other.LogType;
  92. }
  93. }
  94. }