StandardConsoleService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Diagnostics;
  3. using SRF.Service;
  4. using UnityEngine;
  5. namespace SRDebugger.Services.Implementation
  6. {
  7. [Service(typeof(IConsoleService))]
  8. public class StandardConsoleService : IConsoleService
  9. {
  10. public StandardConsoleService()
  11. {
  12. Application.logMessageReceivedThreaded += this.UnityLogCallback;
  13. SRServiceManager.RegisterService<IConsoleService>(this);
  14. this._collapseEnabled = Settings.Instance.CollapseDuplicateLogEntries;
  15. this._allConsoleEntries = new CircularBuffer<ConsoleEntry>(Settings.Instance.MaximumConsoleEntries);
  16. }
  17. public int ErrorCount { get; private set; }
  18. public int WarningCount { get; private set; }
  19. public int InfoCount { get; private set; }
  20. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  21. public event ConsoleUpdatedEventHandler Updated;
  22. public IReadOnlyList<ConsoleEntry> Entries
  23. {
  24. get
  25. {
  26. if (!this._hasCleared)
  27. {
  28. return this._allConsoleEntries;
  29. }
  30. return this._consoleEntries;
  31. }
  32. }
  33. public IReadOnlyList<ConsoleEntry> AllEntries
  34. {
  35. get
  36. {
  37. return this._allConsoleEntries;
  38. }
  39. }
  40. public void Clear()
  41. {
  42. object threadLock = this._threadLock;
  43. lock (threadLock)
  44. {
  45. this._hasCleared = true;
  46. if (this._consoleEntries == null)
  47. {
  48. this._consoleEntries = new CircularBuffer<ConsoleEntry>(Settings.Instance.MaximumConsoleEntries);
  49. }
  50. int num = 0;
  51. this.InfoCount = num;
  52. num = num;
  53. this.WarningCount = num;
  54. this.ErrorCount = num;
  55. }
  56. this.OnUpdated();
  57. }
  58. protected void OnEntryAdded(ConsoleEntry entry)
  59. {
  60. if (this._hasCleared)
  61. {
  62. if (this._consoleEntries.IsFull)
  63. {
  64. this.AdjustCounter(this._consoleEntries.Front().LogType, -1);
  65. this._consoleEntries.PopFront();
  66. }
  67. this._consoleEntries.PushBack(entry);
  68. }
  69. else if (this._allConsoleEntries.IsFull)
  70. {
  71. this.AdjustCounter(this._allConsoleEntries.Front().LogType, -1);
  72. this._allConsoleEntries.PopFront();
  73. }
  74. this._allConsoleEntries.PushBack(entry);
  75. this.OnUpdated();
  76. }
  77. protected void OnEntryDuplicated(ConsoleEntry entry)
  78. {
  79. entry.Count++;
  80. this.OnUpdated();
  81. if (this._hasCleared && this._consoleEntries.Count == 0)
  82. {
  83. this.OnEntryAdded(new ConsoleEntry(entry)
  84. {
  85. Count = 1
  86. });
  87. }
  88. }
  89. private void OnUpdated()
  90. {
  91. if (this.Updated != null)
  92. {
  93. try
  94. {
  95. this.Updated(this);
  96. }
  97. catch
  98. {
  99. }
  100. }
  101. }
  102. private void UnityLogCallback(string condition, string stackTrace, LogType type)
  103. {
  104. object threadLock = this._threadLock;
  105. lock (threadLock)
  106. {
  107. ConsoleEntry consoleEntry = (!this._collapseEnabled || this._allConsoleEntries.Count <= 0) ? null : this._allConsoleEntries[this._allConsoleEntries.Count - 1];
  108. if (consoleEntry != null && consoleEntry.LogType == type && consoleEntry.Message == condition && consoleEntry.StackTrace == stackTrace)
  109. {
  110. this.OnEntryDuplicated(consoleEntry);
  111. }
  112. else
  113. {
  114. ConsoleEntry entry = new ConsoleEntry
  115. {
  116. LogType = type,
  117. StackTrace = stackTrace,
  118. Message = condition
  119. };
  120. this.OnEntryAdded(entry);
  121. }
  122. this.AdjustCounter(type, 1);
  123. }
  124. }
  125. private void AdjustCounter(LogType type, int amount)
  126. {
  127. switch (type)
  128. {
  129. case LogType.Error:
  130. case LogType.Assert:
  131. case LogType.Exception:
  132. this.ErrorCount += amount;
  133. break;
  134. case LogType.Warning:
  135. this.WarningCount += amount;
  136. break;
  137. case LogType.Log:
  138. this.InfoCount += amount;
  139. break;
  140. }
  141. }
  142. private readonly bool _collapseEnabled;
  143. private bool _hasCleared;
  144. private readonly CircularBuffer<ConsoleEntry> _allConsoleEntries;
  145. private CircularBuffer<ConsoleEntry> _consoleEntries;
  146. private readonly object _threadLock = new object();
  147. }
  148. }