Skip to content

Commit 79a9d0c

Browse files
author
Jiang Yin
committed
优化 ConsoleDebugger 的内存开销
1 parent 4592a58 commit 79a9d0c

File tree

5 files changed

+154
-79
lines changed

5 files changed

+154
-79
lines changed

Scripts/Runtime/Debugger/DebuggerComponent.ConsoleWindow.LogNode.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.

Scripts/Runtime/Debugger/DebuggerComponent.ConsoleWindow.cs

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ namespace UnityGameFramework.Runtime
1616
public partial class DebuggerComponent
1717
{
1818
[Serializable]
19-
private sealed partial class ConsoleWindow : IDebuggerWindow
19+
private partial class ConsoleWindow : IDebuggerWindow
2020
{
2121
private SettingComponent m_SettingComponent = null;
22-
private Queue<LogNode> m_Logs = new Queue<LogNode>();
22+
private Queue<LogNode> m_LogNodes = new Queue<LogNode>();
2323
private Vector2 m_LogScrollPosition = Vector2.zero;
2424
private Vector2 m_StackScrollPosition = Vector2.zero;
2525
private int m_InfoCount = 0;
@@ -325,9 +325,9 @@ public void OnDraw()
325325
m_LogScrollPosition = GUILayout.BeginScrollView(m_LogScrollPosition);
326326
{
327327
bool selected = false;
328-
foreach (LogNode i in m_Logs)
328+
foreach (LogNode logNode in m_LogNodes)
329329
{
330-
switch (i.LogType)
330+
switch (logNode.LogType)
331331
{
332332
case LogType.Log:
333333
if (!m_InfoFilter)
@@ -354,12 +354,12 @@ public void OnDraw()
354354
}
355355
break;
356356
}
357-
if (GUILayout.Toggle(m_SelectedNode == i, GetLogString(i)))
357+
if (GUILayout.Toggle(m_SelectedNode == logNode, GetLogString(logNode)))
358358
{
359359
selected = true;
360-
if (m_SelectedNode != i)
360+
if (m_SelectedNode != logNode)
361361
{
362-
m_SelectedNode = i;
362+
m_SelectedNode = logNode;
363363
m_StackScrollPosition = Vector2.zero;
364364
}
365365
}
@@ -400,7 +400,7 @@ public void OnDraw()
400400

401401
private void Clear()
402402
{
403-
m_Logs.Clear();
403+
m_LogNodes.Clear();
404404
}
405405

406406
public void RefreshCount()
@@ -409,9 +409,9 @@ public void RefreshCount()
409409
m_WarningCount = 0;
410410
m_ErrorCount = 0;
411411
m_FatalCount = 0;
412-
foreach (LogNode i in m_Logs)
412+
foreach (LogNode logNode in m_LogNodes)
413413
{
414-
switch (i.LogType)
414+
switch (logNode.LogType)
415415
{
416416
case LogType.Log:
417417
m_InfoCount++;
@@ -429,17 +429,65 @@ public void RefreshCount()
429429
}
430430
}
431431

432+
public void GetRecentLogs(List<LogNode> results)
433+
{
434+
if (results == null)
435+
{
436+
Log.Error("Results is invalid.");
437+
return;
438+
}
439+
440+
results.Clear();
441+
foreach (LogNode logNode in m_LogNodes)
442+
{
443+
results.Add(logNode);
444+
}
445+
}
446+
447+
public void GetRecentLogs(List<LogNode> results, int count)
448+
{
449+
if (results == null)
450+
{
451+
Log.Error("Results is invalid.");
452+
return;
453+
}
454+
455+
if (count <= 0)
456+
{
457+
Log.Error("Count is invalid.");
458+
return;
459+
}
460+
461+
int position = m_LogNodes.Count - count;
462+
if (position < 0)
463+
{
464+
position = 0;
465+
}
466+
467+
int index = 0;
468+
results.Clear();
469+
foreach (LogNode logNode in m_LogNodes)
470+
{
471+
if (index++ < position)
472+
{
473+
continue;
474+
}
475+
476+
results.Add(logNode);
477+
}
478+
}
479+
432480
private void OnLogMessageReceived(string logMessage, string stackTrace, LogType logType)
433481
{
434482
if (logType == LogType.Assert)
435483
{
436484
logType = LogType.Error;
437485
}
438486

439-
m_Logs.Enqueue(new LogNode(logType, logMessage, stackTrace));
440-
while (m_Logs.Count > m_MaxLine)
487+
m_LogNodes.Enqueue(ReferencePool.Acquire<LogNode>().Fill(logType, logMessage, stackTrace));
488+
while (m_LogNodes.Count > m_MaxLine)
441489
{
442-
m_Logs.Dequeue();
490+
ReferencePool.Release(m_LogNodes.Dequeue());
443491
}
444492
}
445493

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//------------------------------------------------------------
2+
// Game Framework v3.x
3+
// Copyright © 2013-2018 Jiang Yin. All rights reserved.
4+
// Homepage: http://gameframework.cn/
5+
// Feedback: mailto:[email protected]
6+
//------------------------------------------------------------
7+
8+
using GameFramework;
9+
using System;
10+
using UnityEngine;
11+
12+
namespace UnityGameFramework.Runtime
13+
{
14+
public partial class DebuggerComponent
15+
{
16+
public sealed class LogNode : IReference
17+
{
18+
private DateTime m_LogTime;
19+
private LogType m_LogType;
20+
private string m_LogMessage;
21+
private string m_StackTrack;
22+
23+
public LogNode Fill(LogType logType, string logMessage, string stackTrack)
24+
{
25+
m_LogTime = DateTime.Now;
26+
m_LogType = logType;
27+
m_LogMessage = logMessage;
28+
m_StackTrack = stackTrack;
29+
30+
return this;
31+
}
32+
33+
public DateTime LogTime
34+
{
35+
get
36+
{
37+
return m_LogTime;
38+
}
39+
}
40+
41+
public LogType LogType
42+
{
43+
get
44+
{
45+
return m_LogType;
46+
}
47+
}
48+
49+
public string LogMessage
50+
{
51+
get
52+
{
53+
return m_LogMessage;
54+
}
55+
}
56+
57+
public string StackTrack
58+
{
59+
get
60+
{
61+
return m_StackTrack;
62+
}
63+
}
64+
65+
public void Clear()
66+
{
67+
m_LogTime = default(DateTime);
68+
m_LogType = default(LogType);
69+
m_LogMessage = default(string);
70+
m_StackTrack = default(string);
71+
}
72+
}
73+
}
74+
}

Scripts/Runtime/Debugger/DebuggerComponent.ConsoleWindow.LogNode.cs.meta renamed to Scripts/Runtime/Debugger/DebuggerComponent.LogNode.cs.meta

File renamed without changes.

Scripts/Runtime/Debugger/DebuggerComponent.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,25 @@ public bool SelectDebuggerWindow(string path)
284284
return m_DebuggerManager.SelectDebuggerWindow(path);
285285
}
286286

287+
/// <summary>
288+
/// 获取记录的全部日志。
289+
/// </summary>
290+
/// <param name="results">要获取的日志。</param>
291+
public void GetRecentLogs(List<LogNode> results)
292+
{
293+
m_ConsoleWindow.GetRecentLogs(results);
294+
}
295+
296+
/// <summary>
297+
/// 获取记录的最近日志。
298+
/// </summary>
299+
/// <param name="results">要获取的日志。</param>
300+
/// <param name="count">要获取最近日志的数量。</param>
301+
public void GetRecentLogs(List<LogNode> results, int count)
302+
{
303+
m_ConsoleWindow.GetRecentLogs(results, count);
304+
}
305+
287306
private void DrawWindow(int windowId)
288307
{
289308
GUI.DragWindow(m_DragRect);

0 commit comments

Comments
 (0)