52
52
-- It's dangerous to go alone; take this!
53
53
local dir = GAMESTATE :GetCurrentSong ():GetSongDir ()
54
54
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
62
85
end
63
- return ret or nil
64
86
end
65
- function sudo .printerr ( s , ret ) lua . ReportScriptError ( ' KITSU: ' .. tostring ( s )) return ret end
87
+ sudo .printerr = sudo . err -- for compatibility
66
88
67
89
-- Library importer
68
90
function sudo .import (lib )
@@ -72,7 +94,7 @@ function sudo.import(lib)
72
94
-- Make sure the file is there
73
95
local file = dir .. ' lib/' .. lib .. ' .lua'
74
96
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.' )
76
98
return
77
99
end
78
100
-- Return our file in our environment
@@ -87,7 +109,7 @@ function sudo.run(path)
87
109
-- Make sure the file is there
88
110
local file = dir .. ' lua/' .. path .. ' .lua'
89
111
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.' )
91
113
return
92
114
end
93
115
-- Return our file in our environment
@@ -102,7 +124,7 @@ function sudo.depend(lib, dependency, name)
102
124
end
103
125
-- If we still don't have it, throw an error.
104
126
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' )
106
128
end
107
129
end
108
130
@@ -136,11 +158,11 @@ function sudo.getfrom(ns, deep)
136
158
local target = env [ns ] or sudo [ns ]
137
159
return function (t )
138
160
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?)' )
140
162
else
141
163
for _ , v in ipairs (t ) do
142
164
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?)' )
144
166
else
145
167
if deep and type (target [v ]) == ' table' then
146
168
env [v ] = DeepCopy (target [v ])
@@ -156,13 +178,13 @@ end
156
178
function sudo .switch (var )
157
179
local ret = nop
158
180
if not var then
159
- return sudo .printerr (' switch: given variable is nil' )
181
+ return sudo .err (' switch: given variable is nil' )
160
182
else
161
183
return function (t )
162
184
ret = t [' _' ] or ret
163
185
for k , v in pairs (t ) do
164
186
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 ))
166
188
elseif tostring (var ) == k then
167
189
ret = v or ret
168
190
end
0 commit comments