Skip to content

Commit 1bfa731

Browse files
committed
Pre-emptively truncate message to fit in GUI.Label
The stack trace too, which will be important when we start to display it.
1 parent bcfdedc commit 1bfa731

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

Console.cs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ void Update()
179179
void DrawLog(int logIndex, GUIStyle logStyle)
180180
{
181181
var log = logs[logIndex];
182-
var truncatedMessage = log.GetTruncatedMessage();
183182

184183
GUI.contentColor = logTypeColors[log.Type];
185184

@@ -188,7 +187,7 @@ void DrawLog(int logIndex, GUIStyle logStyle)
188187
// Draw collapsed log with badge indicating count.
189188
GUILayout.BeginHorizontal();
190189
{
191-
GUILayout.Label(truncatedMessage, logStyle);
190+
GUILayout.Label(log.Message, logStyle);
192191
GUILayout.FlexibleSpace();
193192
GUILayout.Label(log.Count.ToString(), GUI.skin.box);
194193
}
@@ -200,7 +199,7 @@ void DrawLog(int logIndex, GUIStyle logStyle)
200199

201200
for (var i = 0; i < labelCount; i += 1)
202201
{
203-
GUILayout.Label(truncatedMessage, logStyle);
202+
GUILayout.Label(log.Message, logStyle);
204203
}
205204
}
206205

@@ -307,16 +306,9 @@ void UpdateQueuedLogs()
307306

308307
void HandleLogThreaded(string message, string stackTrace, LogType type)
309308
{
310-
var log = new Log
311-
{
312-
Count = 1,
313-
Message = message,
314-
StackTrace = stackTrace,
315-
Type = type,
316-
};
317-
318309
// Queue the log into a ConcurrentQueue to be processed later in the Unity main thread,
319310
// so that we don't get GUI-related errors for logs coming from other threads
311+
var log = new Log(message, stackTrace, type);
320312
queuedLogs.Enqueue(log);
321313
}
322314

@@ -328,8 +320,7 @@ void ProcessLogItem(Log log)
328320
if (isDuplicateOfLastLog)
329321
{
330322
// Replace previous log with incremented count instead of adding a new one.
331-
log.Count = lastLog.Value.Count + 1;
332-
logs[logs.Count - 1] = log;
323+
logs[logs.Count - 1] = lastLog.Value.IncrementedCount();
333324
}
334325
else
335326
{
@@ -427,35 +418,51 @@ static void EnableMultiTouch()
427418
/// <summary>
428419
/// A basic container for log details.
429420
/// </summary>
430-
struct Log
421+
readonly struct Log
431422
{
432-
public int Count;
433-
public string Message;
434-
public string StackTrace;
435-
public LogType Type;
423+
public readonly int Count;
424+
public readonly string Message;
425+
public readonly string StackTrace;
426+
public readonly LogType Type;
436427

437-
/// <summary>
438-
/// The max string length supported by UnityEngine.GUILayout.Label without triggering this error:
439-
/// "String too long for TextMeshGenerator. Cutting off characters."
440-
/// </summary>
441-
const int maxMessageLength = 16382;
428+
public Log(string message, string stackTrace, LogType type)
429+
{
430+
Count = 1;
431+
Message = TruncateForGUILabel(message);
432+
StackTrace = TruncateForGUILabel(stackTrace);
433+
Type = type;
434+
}
435+
436+
Log(string message, string stackTrace, LogType type, int count)
437+
{
438+
Count = count;
439+
Message = message;
440+
StackTrace = stackTrace;
441+
Type = type;
442+
}
442443

443444
public bool Equals(Log log)
444445
{
445446
return Message == log.Message && StackTrace == log.StackTrace && Type == log.Type;
446447
}
447448

449+
public Log IncrementedCount()
450+
{
451+
return new Log(Message, StackTrace, Type, Count + 1);
452+
}
453+
448454
/// <summary>
449-
/// Return a truncated Message if it exceeds the max Message length.
455+
/// Returns text shortened to fit in a GUILayout.Label.
450456
/// </summary>
451-
public string GetTruncatedMessage()
457+
static string TruncateForGUILabel(string text)
452458
{
453-
if (string.IsNullOrEmpty(Message))
454-
{
455-
return Message;
456-
}
459+
// The max string length supported by UnityEngine.GUILayout.Label without triggering this error:
460+
// "String too long for TextMeshGenerator. Cutting off characters."
461+
const int maxLabelLength = 16382;
457462

458-
return Message.Length <= maxMessageLength ? Message : Message.Substring(0, maxMessageLength);
463+
return string.IsNullOrEmpty(text) || text.Length <= maxLabelLength
464+
? text
465+
: text.Substring(0, maxLabelLength);
459466
}
460467
}
461468

0 commit comments

Comments
 (0)