145 lines
3.1 KiB
C#
145 lines
3.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using SRF;
|
|
using SRF.Service;
|
|
using UnityEngine;
|
|
|
|
namespace SRDebugger.Services.Implementation
|
|
{
|
|
[Service(typeof(IConsoleService))]
|
|
public class StandardConsoleService : IConsoleService
|
|
{
|
|
private readonly SRList<ConsoleEntry> _allConsoleEntries = new SRList<ConsoleEntry>();
|
|
|
|
private readonly bool _collapseEnabled;
|
|
|
|
private readonly SRList<ConsoleEntry> _consoleEntries = new SRList<ConsoleEntry>();
|
|
|
|
private readonly ReadOnlyCollection<ConsoleEntry> _consoleEntriesReadOnly;
|
|
|
|
private ReadOnlyCollection<ConsoleEntry> _allConsoleEntriesReadOnly;
|
|
|
|
private bool _hasCleared;
|
|
|
|
public int ErrorCount { get; private set; }
|
|
|
|
public int WarningCount { get; private set; }
|
|
|
|
public int InfoCount { get; private set; }
|
|
|
|
public IList<ConsoleEntry> Entries
|
|
{
|
|
get
|
|
{
|
|
if (!_hasCleared)
|
|
{
|
|
return _allConsoleEntriesReadOnly;
|
|
}
|
|
return _consoleEntriesReadOnly;
|
|
}
|
|
}
|
|
|
|
public IList<ConsoleEntry> AllEntries
|
|
{
|
|
get
|
|
{
|
|
return _consoleEntriesReadOnly;
|
|
}
|
|
}
|
|
|
|
public event ConsoleUpdatedEventHandler Updated;
|
|
|
|
public StandardConsoleService()
|
|
{
|
|
Application.RegisterLogCallback(UnityLogCallback);
|
|
SRServiceManager.RegisterService<IConsoleService>(this);
|
|
_collapseEnabled = Settings.Instance.CollapseDuplicateLogEntries;
|
|
_allConsoleEntriesReadOnly = _allConsoleEntries.AsReadOnly();
|
|
_consoleEntriesReadOnly = _consoleEntries.AsReadOnly();
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_hasCleared = true;
|
|
_consoleEntries.Clear();
|
|
int num = (InfoCount = 0);
|
|
num = (WarningCount = num);
|
|
ErrorCount = num;
|
|
OnUpdated();
|
|
}
|
|
|
|
public void ResetCounters()
|
|
{
|
|
}
|
|
|
|
protected void OnEntryAdded(ConsoleEntry entry)
|
|
{
|
|
if (_hasCleared)
|
|
{
|
|
_consoleEntries.Add(entry);
|
|
}
|
|
_allConsoleEntries.Add(entry);
|
|
OnUpdated();
|
|
}
|
|
|
|
protected void OnEntryDuplicated(ConsoleEntry entry)
|
|
{
|
|
entry.Count++;
|
|
OnUpdated();
|
|
if (_hasCleared && _consoleEntries.Count == 0)
|
|
{
|
|
OnEntryAdded(new ConsoleEntry(entry)
|
|
{
|
|
Count = 1
|
|
});
|
|
}
|
|
}
|
|
|
|
private void OnUpdated()
|
|
{
|
|
if (this.Updated != null)
|
|
{
|
|
try
|
|
{
|
|
this.Updated(this);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|
|
private void UnityLogCallback(string condition, string stackTrace, LogType type)
|
|
{
|
|
ConsoleEntry consoleEntry = ((!_collapseEnabled || _allConsoleEntries.Count <= 0) ? null : _allConsoleEntries[_allConsoleEntries.Count - 1]);
|
|
if (consoleEntry != null && consoleEntry.LogType == type && consoleEntry.Message == condition && consoleEntry.StackTrace == stackTrace)
|
|
{
|
|
OnEntryDuplicated(consoleEntry);
|
|
}
|
|
else
|
|
{
|
|
ConsoleEntry consoleEntry2 = new ConsoleEntry();
|
|
consoleEntry2.LogType = type;
|
|
consoleEntry2.StackTrace = stackTrace;
|
|
consoleEntry2.Message = condition;
|
|
ConsoleEntry entry = consoleEntry2;
|
|
OnEntryAdded(entry);
|
|
}
|
|
switch (type)
|
|
{
|
|
case LogType.Error:
|
|
case LogType.Assert:
|
|
case LogType.Exception:
|
|
ErrorCount++;
|
|
break;
|
|
case LogType.Warning:
|
|
WarningCount++;
|
|
break;
|
|
case LogType.Log:
|
|
InfoCount++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|