AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DebugPanel.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System.Collections.Generic;
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity
8 {
12  public class DebugPanel : SingleInstance<DebugPanel>
13  {
17  private TextMesh textMesh;
18 
22  private Queue<string> logMessages = new Queue<string>();
23  private Queue<string> nextLogMessages = new Queue<string>();
24 
28  private int maxLogMessages = 30;
29 
34  public delegate string GetLogLine();
35 
39  private List<GetLogLine> externalLogs = new List<GetLogLine>();
40 
41  private void Awake()
42  {
43  textMesh = GetComponent<TextMesh>();
44  Application.logMessageReceivedThreaded += Application_logMessageReceivedThreaded;
45  }
46 
47  private void Update()
48  {
49  string logMessageString = CalculateLogMessageString();
50  textMesh.text = logMessageString;
51  }
52 
57  private string CalculateLogMessageString()
58  {
59  string logMessageString = "Per Frame Data:\n------\n";
60  for (int index = 0; index < externalLogs.Count; index++)
61  {
62  string nextExternalLine = externalLogs[index]();
63  if (!string.IsNullOrEmpty(nextExternalLine))
64  {
65  logMessageString += string.Format("{0}\n", nextExternalLine);
66  }
67  }
68 
69  logMessageString += "------\n";
70 
71  lock (logMessages)
72  {
73  while (logMessages.Count > 0)
74  {
75  string nextMessage = logMessages.Dequeue();
76  logMessageString += string.Format("{0}\n", nextMessage);
77  // for the next frame...
78  nextLogMessages.Enqueue(nextMessage);
79  }
80 
81  Queue<string> tmp = logMessages;
82  logMessages = nextLogMessages;
83  nextLogMessages = tmp;
84  nextLogMessages.Clear();
85  }
86 
87  return logMessageString;
88  }
89 
96  private void Application_logMessageReceivedThreaded(string condition, string stackTrace, LogType type)
97  {
98  LogCallback(condition, stackTrace, type);
99  }
100 
101  private void LogCallback(string message, string stack, LogType logType)
102  {
103  lock (logMessages)
104  {
105  while (logMessages.Count > maxLogMessages)
106  {
107  logMessages.Dequeue();
108  }
109 
110  logMessages.Enqueue(message);
111  if (logType != LogType.Log)
112  {
113  // Also add the stack. This will push us beyond our max messages, but it is a soft limit,
114  // and the stack information is valuable in error/warning cases.
115  logMessages.Enqueue(stack);
116  }
117  }
118  }
119 
124  public void RegisterExternalLogCallback(GetLogLine callback)
125  {
126  externalLogs.Add(callback);
127  }
128 
133  public void UnregisterExternalLogCallback(GetLogLine callback)
134  {
135  if (externalLogs.Contains(callback))
136  {
137  externalLogs.Remove(callback);
138  }
139  }
140  }
141 }
Script to control writing the Debug.Log output to a control.
Definition: DebugPanel.cs:12
A simplified version of the Singleton class which doesn&#39;t depend on the Instance being set in Awake ...
void RegisterExternalLogCallback(GetLogLine callback)
Call this to register your script to provide a debug string each frame.
Definition: DebugPanel.cs:124
void UnregisterExternalLogCallback(GetLogLine callback)
Call this when you no longer want to provide a debug string each frame.
Definition: DebugPanel.cs:133