IConsoleService.cs 3.1 KB

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