-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBurgerHandler.cs
More file actions
87 lines (79 loc) · 2.7 KB
/
BurgerHandler.cs
File metadata and controls
87 lines (79 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.IO;
using System.Text;
using UnityEngine;
public static class BurgerHandler
{
private static string fileName;
private static string logPath;
private static StringBuilder builder;
private static (string message, string stacktrace, LogType logType) prevMessage;
private static int lastLength;
private static int repeatCount;
private static object threadLock = new();
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Init()
{
logPath = $"{Application.persistentDataPath}/Logs";
if (!Directory.Exists(logPath))
{
Directory.CreateDirectory(logPath);
}
Application.logMessageReceivedThreaded -= UnityLogCallback;
Application.logMessageReceivedThreaded += UnityLogCallback;
}
private static void UnityLogCallback(string condition, string stackTrace, LogType type)
{
lock (threadLock)
{
if (builder == null || builder.Length > 10000)
{
builder = new StringBuilder();
fileName = $"{logPath}/{DateTime.UtcNow.Ticks}.txt";
prevMessage = default;
lastLength = 0;
repeatCount = 1;
}
if (prevMessage != default && prevMessage.logType == type && prevMessage.message == condition &&
prevMessage.stacktrace == stackTrace)
{
repeatCount++;
if (Burger.logToFile)
{
builder.Remove(lastLength, builder.Length - lastLength);
var repeatCountText = $"({repeatCount}) ";
builder.Append(repeatCountText);
Build();
lastLength -= repeatCountText.Length;
}
else
{
builder = null;
}
}
else
{
repeatCount = 1;
if (Burger.logToFile)
{
Build();
}
else
{
builder = null;
}
}
prevMessage.logType = type;
prevMessage.message = condition;
prevMessage.stacktrace = stackTrace;
void Build()
{
lastLength = builder.Length;
builder.Append($"[{type}] ");
builder.AppendLine(condition);
builder.AppendLine(stackTrace);
File.WriteAllText(fileName, builder.ToString());
}
}
}
}