Skip to content

Commit 787f52a

Browse files
committed
Rewrite debug logging system
1 parent fd93d47 commit 787f52a

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

main/env.lua

+38-16
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,39 @@ end
5252
-- It's dangerous to go alone; take this!
5353
local dir = GAMESTATE:GetCurrentSong():GetSongDir()
5454

55-
-- Debug and Error prints
56-
function sudo.print(s, ret)
57-
if s and type(s) == 'table' then
58-
print('KITSU: Printing '..tostring(s))
59-
PrintTable(s)
60-
else
61-
print('KITSU: '..tostring(s))
55+
-- Debug logging
56+
local function log(level, ...)
57+
local str = ''
58+
for _, v in ipairs {...} do
59+
str = str..' '..tostring(v)
60+
end
61+
lua.ReportScriptError(level..(level ~= '' and ':' or '')..str)
62+
end
63+
function sudo.info(...)
64+
log('INFO', ...)
65+
end
66+
function sudo.warn(level, ...)
67+
local info = debug.getinfo((type(level) == 'number' and level or 2), 'Sln')
68+
log(
69+
'WARNING',
70+
(type(level) == 'string' and level or '')..table.concat({...}, ' '),
71+
'\n',
72+
((info.what == 'Lua' and info.name..'()') or info.what),
73+
'in', info.short_src,
74+
'at line', info.currentline
75+
)
76+
end
77+
function sudo.err(...)
78+
log('ERROR', ...)
79+
local level = 2
80+
while true do
81+
local info = debug.getinfo(level, 'Sln')
82+
if not info then break end
83+
log('', ((info.what == 'Lua' and info.name..'()') or info.what), 'in', info.short_src, 'at line', info.currentline)
84+
level = level + 1
6285
end
63-
return ret or nil
6486
end
65-
function sudo.printerr(s, ret) lua.ReportScriptError('KITSU: '..tostring(s)) return ret end
87+
sudo.printerr = sudo.err -- for compatibility
6688

6789
-- Library importer
6890
function sudo.import(lib)
@@ -72,7 +94,7 @@ function sudo.import(lib)
7294
-- Make sure the file is there
7395
local file = dir..'lib/'..lib..'.lua'
7496
if not assert(loadfile(file)) then
75-
sudo.printerr('Unable to import library "'..lib..'": No file found.')
97+
sudo.err('Unable to import library "'..lib..'": No file found.')
7698
return
7799
end
78100
-- Return our file in our environment
@@ -87,7 +109,7 @@ function sudo.run(path)
87109
-- Make sure the file is there
88110
local file = dir..'lua/'..path..'.lua'
89111
if not assert(loadfile(file)) then
90-
sudo.printerr('Unable to run file "'..path..'": No file found.')
112+
sudo.err('Unable to run file "'..path..'": No file found.')
91113
return
92114
end
93115
-- Return our file in our environment
@@ -102,7 +124,7 @@ function sudo.depend(lib, dependency, name)
102124
end
103125
-- If we still don't have it, throw an error.
104126
if dependency == nil then
105-
sudo.printerr('Error importing library "'..lib..'": Unable to load '..name..' dependency')
127+
sudo.err('Error importing library "'..lib..'": Unable to load '..name..' dependency')
106128
end
107129
end
108130

@@ -136,11 +158,11 @@ function sudo.getfrom(ns, deep)
136158
local target = env[ns] or sudo[ns]
137159
return function(t)
138160
if not target then
139-
sudo.printerr('No table or environment "'..ns..'" found (Is table local?)')
161+
sudo.err('No table or environment "'..ns..'" found (Is table local?)')
140162
else
141163
for _, v in ipairs(t) do
142164
if not target[v] then
143-
sudo.printerr('No variable "'..v..'" found (Is variable local?)')
165+
sudo.err('No variable "'..v..'" found (Is variable local?)')
144166
else
145167
if deep and type(target[v]) == 'table' then
146168
env[v] = DeepCopy(target[v])
@@ -156,13 +178,13 @@ end
156178
function sudo.switch(var)
157179
local ret = nop
158180
if not var then
159-
return sudo.printerr('switch: given variable is nil')
181+
return sudo.err('switch: given variable is nil')
160182
else
161183
return function(t)
162184
ret = t['_'] or ret
163185
for k, v in pairs(t) do
164186
if type(v) ~= 'function' then
165-
return sudo.printerr('switch: expected case argument of type function, got '..type(v))
187+
return sudo.err('switch: expected case argument of type function, got '..type(v))
166188
elseif tostring(var) == k then
167189
ret = v or ret
168190
end

0 commit comments

Comments
 (0)