-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtabDebuggerTrace.lua
More file actions
163 lines (144 loc) · 5.69 KB
/
tabDebuggerTrace.lua
File metadata and controls
163 lines (144 loc) · 5.69 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
local cjson = require("cjson")
local kTid = 1024
local kPid = 1024
local kCat = "tabMachine"
local Frequency = CS.System.Diagnostics.Stopwatch.Frequency
local microsecondPerTick = (1000 * 1000) / Frequency
local tabDebuggerTrace = {}
function tabDebuggerTrace.new()
local t = {}
setmetatable(t, {__index = t})
t._ContextStartAsDurationEvents = false
t._traceEvents = {}
t._stopwatch = CS.System.Diagnostics.Stopwatch.StartNew()
return t
end
function tabDebuggerTrace:close()
local success, filePath, fileSize = self:saveTraceFile()
print(filePath, success, fileSize)
end
function tabDebuggerTrace:getTimestamp()
local ticks = self._stopwatch.ElapsedTicks
return microsecondPerTick * ticks
end
function tabDebuggerTrace:getDetailedPath(context)
local c = context
local name = nil
if c then
name = c.__name
else
name = ""
end
return tostring(name)
end
local function on_error(e)
printError(e)
end
function tabDebuggerTrace:saveTraceFile()
local context = ""
local success = false
local fileSize = 0
local fileName = os.date('trace_%Y-%m-%d-%H-%M-%S')
local filePath = CS.System.IO.Path.Combine(CS.UnityEngine.Application.persistentDataPath, "tab_trace/" .. fileName .. ".json")
success = xpcall(function()
CS.SystemIOUtils.EnsureFileDirectoryExists(filePath)
local data = {
traceEvents = self._traceEvents,
}
context = cjson.encode(data)
local file, err = io.open(filePath, "wb")
if not err and file then
file:write(context)
fileSize = file:seek()
file:close()
end
end, on_error)
return success, filePath, fileSize
end
function tabDebuggerTrace:onMachineStart(machine, scName)
local msg = g_frameIndex .. " tab start machine"
if self._traceback then
msg = msg .. "\n" .. debug.traceback()
end
print(msg)
end
function tabDebuggerTrace:onContextStart(context, scName)
local fullName = context:getDetailedPath(context) .. "." .. scName
if self._ContextStartAsDurationEvents then
-- As Duration Events
local name = scName
local ts = self:getTimestamp()
local args = {fullName = fullName}
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "B", name = name, args = args }
table.insert(self._traceEvents, traceEvent)
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "E", name = name, args = args }
table.insert(self._traceEvents, traceEvent)
else
-- As Instant Events
local name = "[tab start]" .. scName
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "i", name = name}
table.insert(self._traceEvents, traceEvent)
end
end
function tabDebuggerTrace:onTabCall(context, scName, tabName)
local name = scName
local fullName = context:getDetailedPath(context) .. "." .. scName
local args = {fullName = fullName}
if type(tabName) == "string" and tabName ~= "context" then
args.tabName = tabName
end
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "B", name = name, args = args }
table.insert(self._traceEvents, traceEvent)
end
function tabDebuggerTrace:onContextStop(context)
local name = self:getDetailedPath(context)
local fullName = context:getDetailedPath(context)
local args = {fullName = fullName}
if type(context.tabName) == "string" and context.tabName ~= "context" then
args.tabName = context.tabName
end
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "E", name = name, args = args }
table.insert(self._traceEvents, traceEvent)
end
function tabDebuggerTrace:onContextQuit(context)
local name = "[tab quit]" ..self:getDetailedPath(context)
local fullName = context:getDetailedPath(context)
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "i", name = name }
table.insert(self._traceEvents, traceEvent)
end
function tabDebuggerTrace:onContextException(context, exception)
local name = "[tab exception]" ..self:getDetailedPath(context)
local fullName = context:getDetailedPath(context)
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "i", name = name }
table.insert(self._traceEvents, traceEvent)
end
function tabDebuggerTrace:onTabJoin(context, scName, scNames)
local joins = table.concat(scNames, "")
local name = "[tab join]" .. scName
local fullName = context:getDetailedPath(context) .. "." .. scName .. " " .. joins
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "i", name = name}
table.insert(self._traceEvents, traceEvent)
end
function tabDebuggerTrace:onTabSuspend(context, scName)
local name = "[tab suspend]" .. scName
local fullName = context:getDetailedPath(context) .. "." .. scName
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "i", name = name}
table.insert(self._traceEvents, traceEvent)
end
function tabDebuggerTrace:onTabResume(context, scName)
local name = "[tab resume]" .. scName
local name = "[tab resume]" .. scName
local fullName = context:getDetailedPath(context) .. "." .. scName
local ts = self:getTimestamp()
local traceEvent = { cat = kCat, pid = kPid, tid = kTid, ts = ts, ph = "i", name = name}
table.insert(self._traceEvents, traceEvent)
end
return tabDebuggerTrace