-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbg.lua
189 lines (141 loc) · 3.83 KB
/
dbg.lua
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
-- global Debug
-- requirements
-- global Session (search code for usage)
-- todo: show errors always, event if channel is disabled
local _debug={}
-- connected from uplib
_debug.pow=nil
_debug.useConsole=true
_debug.useFile=true
_debug.messages={}
local _contexts={}
local channels={}
_debug.channel=channels
local netChannel={
name="net",
useConsole=true,
useFile=true,
messages={},
}
channels.net=netChannel
-- todo: настройка какие из ченнелов попадают в мейн
-- todo: multichannel messages
local newChannel=function(name)
local result={}
result.name=name
result.useConsole=_debug.useConsole
result.useFile=_debug.useFile
result.messages={}
return result
end
channels.db=newChannel("db")
channels.event=newChannel("event")
channels.verbose=newChannel("verbose")
channels.entity=newChannel("entity")
channels.input=newChannel("input")
channels.collision=newChannel("collision")
-- all console settings
channels.entity.useConsole=false
channels.input.useConsole=false
channels.verbose.useConsole=false
channels.event.useConsole=false
channels.db.useConsole=false
channels.collision.useConsole=false
-- used if no channel name provided
local _mainChannel=newChannel("main")
channels.main=_mainChannel
-- forceConsole: show message in console even if its channel is disabled
_debug.log=function(message,channelName,forceConsole)
-- local time = love.timer.getTime() -- "\t"..time
if string.find(message,"error") then
-- todo: reimplement
-- Session.hasErrors=true
message=message+"\n"+debug.traceback()
elseif string.find(message,"warn") then
-- Session.hasWarnings=true
message=message+"\n"+debug.traceback()
end
local preparedMessage = _debug.pow.getFrame().."\t"
if _debug.useConsole and channelName then
preparedMessage=preparedMessage.."["..channelName.."]\t"
end
-- contexts
local hasContexts=false
local contextsText=""
for k,context in ipairs(_contexts) do
if hasContexts then
contextsText=contextsText+","
end
contextsText=contextsText+context
hasContexts=true
end
if hasContexts then
preparedMessage=preparedMessage+"|"+contextsText+"|\t"
end
preparedMessage=preparedMessage..message
local channel
if channelName then
channel=channels[channelName]
if not channel then
channel=newChannel(channelName)
channels[channelName]=channel
end
else
channel=_mainChannel
end
if channel.useFile then
table.insert(channel.messages, preparedMessage)
-- trace log, full timeline
table.insert(_debug.messages, preparedMessage)
else
table.insert(_debug.messages, preparedMessage)
end
if channel.useConsole or forceConsole then
local consoleMessage
-- todo: reimplement
-- if not Config.isFullLog then
-- consoleMessage=Util.oneLine(preparedMessage)
-- else
consoleMessage=preparedMessage
-- end
print(consoleMessage)
end
end
local writeChannels=function()
local writeChannel=function(channel)
local log = ""
for k, message in ipairs(channel.messages) do
log=log..message.."\n"
end
love.filesystem.append(_debug.pow.saveDir.."log_"..channel.name..".txt", log)
channel.messages={}
end
for k,channel in pairs(channels) do
if channel.useFile then
writeChannel(channel)
end
end
end
-- вызывать например из love.quit, и в других gc friendly местах
_debug.writeLogs=function()
if _debug.useFile then
local log = ""
for k, v in ipairs(_debug.messages) do
log=log..v.."\n"
end
love.filesystem.append(_debug.pow.saveDir.."log.txt", log)
_debug.messages={}
end
writeChannels()
end
-- extra text to log messages
_debug.enterContext=function(context)
table.insert(_contexts, context)
end
_debug.exitContext=function()
local lastPos=#_contexts
local context=_contexts[lastPos]
log("exit context:"..context)
table.remove(_contexts,lastPos)
end
return _debug