-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.lua
59 lines (53 loc) · 1.3 KB
/
logger.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
local subst = require("util").subst
local logger_i = {}
local logger_m = { __index = logger_i }
function logger_m:__call(...)
self.func_(subst("[$] $$", ("% 8i"):format(self.fragment_), self.prefix_, subst(...)))
end
function logger_i:sub(thing, ...)
local next_fragment = self.next_fragment_()
if thing then
self.func_(subst("[$] $$ => $", ("% 8i"):format(self.fragment_), self.prefix_, subst(thing, ...), next_fragment))
end
local level = self.level_ + 1
return setmetatable({
func_ = self.func_,
level_ = level,
prefix_ = (" "):rep(level),
next_fragment_ = self.next_fragment_,
fragment_ = next_fragment,
}, logger_m)
end
local function new(func)
local fragment = 0
local function next_fragment()
fragment = fragment + 1
return fragment
end
return setmetatable({
func_ = func,
level_ = 0,
prefix_ = "",
next_fragment_ = next_fragment,
fragment_ = next_fragment(),
}, logger_m)
end
local function dump(thing, level)
level = level or 0
if type(thing) == "table" then
print("{")
for key, value in pairs(thing) do
io.stdout:write((" "):rep(level + 1) .. key .. " => ")
dump(value, level + 1)
end
print((" "):rep(level) .. "}")
elseif type(thing) == "string" then
print(("%q"):format(thing))
else
print(tostring(thing))
end
end
return {
new = new,
dump = dump,
}