| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using UnityEngine; |
| | 7 | | using System.Collections; |
| | 8 | | using GDX.Collections.Generic; |
| | 9 | |
|
| | 10 | | namespace GDX.Developer |
| | 11 | | { |
| | 12 | | public class ConsoleLog : IList |
| | 13 | | { |
| 14 | 14 | | public uint Version = 0; |
| | 15 | |
|
| 14 | 16 | | ConcurrentCircularBuffer<ConsoleLogEntry> m_LogHistory = new ConcurrentCircularBuffer<ConsoleLogEntry>(1000); |
| | 17 | |
|
| | 18 | | public ConsoleLogEntry GetEntryAt(int index) |
| 0 | 19 | | { |
| 0 | 20 | | return m_LogHistory[index]; |
| 0 | 21 | | } |
| | 22 | |
|
| | 23 | | public ConsoleLogEntry GetLastEntry() |
| 2 | 24 | | { |
| 2 | 25 | | return m_LogHistory[Count - 1]; |
| 2 | 26 | | } |
| | 27 | |
|
| 14 | 28 | | public ConsoleLog() |
| 14 | 29 | | { |
| 14 | 30 | | Application.logMessageReceivedThreaded += OnMessageReceived; |
| 14 | 31 | | } |
| | 32 | |
|
| | 33 | | ~ConsoleLog() |
| 0 | 34 | | { |
| 0 | 35 | | Application.logMessageReceivedThreaded -= OnMessageReceived; |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | void OnMessageReceived(string message, string stacktrace, LogType type) |
| 13 | 39 | | { |
| 13 | 40 | | m_LogHistory.Add(new ConsoleLogEntry(type, message, stacktrace)); |
| 13 | 41 | | Version++; |
| 13 | 42 | | } |
| | 43 | |
|
| | 44 | | public IEnumerator GetEnumerator() |
| 0 | 45 | | { |
| 0 | 46 | | int count = m_LogHistory.Count; |
| 0 | 47 | | for (int i = 0; i < count; i++) |
| 0 | 48 | | { |
| 0 | 49 | | yield return m_LogHistory[i]; |
| 0 | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | public void CopyTo(Array array, int index) |
| 0 | 54 | | { |
| | 55 | | // Immutable |
| 0 | 56 | | } |
| | 57 | |
|
| 2 | 58 | | public int Count => m_LogHistory.Count; |
| | 59 | |
|
| 0 | 60 | | public bool IsSynchronized => false; |
| 0 | 61 | | public object SyncRoot => this; |
| | 62 | |
|
| | 63 | | public int Add(object value) |
| 0 | 64 | | { |
| 0 | 65 | | return -1; |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | public void Clear() |
| 0 | 69 | | { |
| 0 | 70 | | m_LogHistory.Clear(); |
| 0 | 71 | | Version++; |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | public bool Contains(object value) |
| 0 | 75 | | { |
| 0 | 76 | | if (value == null) |
| 0 | 77 | | { |
| 0 | 78 | | return false; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | ConsoleLogEntry unboxed = (ConsoleLogEntry)value; |
| 0 | 82 | | int count = m_LogHistory.Count; |
| 0 | 83 | | for (int i = 0; i < count; i++) |
| 0 | 84 | | { |
| 0 | 85 | | ConsoleLogEntry currentEntry = m_LogHistory[i]; |
| 0 | 86 | | if (currentEntry.CompareTo(unboxed) == 0) |
| 0 | 87 | | { |
| 0 | 88 | | return true; |
| | 89 | | } |
| 0 | 90 | | } |
| | 91 | |
|
| 0 | 92 | | return false; |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public int IndexOf(object value) |
| 0 | 96 | | { |
| 0 | 97 | | if (value == null) |
| 0 | 98 | | { |
| 0 | 99 | | return -1; |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | ConsoleLogEntry unboxed = (ConsoleLogEntry)value; |
| 0 | 103 | | int count = m_LogHistory.Count; |
| 0 | 104 | | for (int i = 0; i < count; i++) |
| 0 | 105 | | { |
| 0 | 106 | | ConsoleLogEntry currentEntry = m_LogHistory[i]; |
| 0 | 107 | | if (currentEntry.CompareTo(unboxed) == 0) |
| 0 | 108 | | { |
| 0 | 109 | | return i; |
| | 110 | | } |
| 0 | 111 | | } |
| | 112 | |
|
| 0 | 113 | | return -1; |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | public void Insert(int index, object value) |
| 0 | 117 | | { |
| | 118 | | // Immutable |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | public void Remove(object value) |
| 0 | 122 | | { |
| | 123 | | // Immutable |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | public void RemoveAt(int index) |
| 0 | 127 | | { |
| | 128 | | // Immutable |
| 0 | 129 | | } |
| | 130 | |
|
| 0 | 131 | | public bool IsFixedSize => false; |
| 0 | 132 | | public bool IsReadOnly => true; |
| | 133 | |
|
| | 134 | | public object this[int index] |
| | 135 | | { |
| 0 | 136 | | get => m_LogHistory[index]; |
| | 137 | | set |
| 0 | 138 | | { |
| 0 | 139 | | } |
| | 140 | | } |
| | 141 | | } |
| | 142 | | } |