-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrisuDebug.lua
138 lines (123 loc) · 3.53 KB
/
GrisuDebug.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
-- GrisuDebug
--
-- @author Grisu118 - VertexDezign.net
-- @history v1.0 - 2016-10-26 - Initial implementation
-- @history v1.1 - 2017-09-15 - Add Off Level, add shorthand methods for logging, add support for closures
-- @history v1.2 - 2024-11-19 - Add function to print table
-- @history v1.3 - 2024-11-24 - Add support for string.format
-- @Descripion: Providing debug utils
-- @web: http://grisu118.ch or http://vertexdezign.net
-- Copyright (C) Grisu118, All Rights Reserved.
---@class GrisuDebug
GrisuDebug = {}
GrisuDebug.__index = GrisuDebug
GrisuDebug.TRACE = 1
GrisuDebug.DEBUG = 2
GrisuDebug.INFO = 3
GrisuDebug.WARNING = 4
GrisuDebug.ERROR = 5
GrisuDebug.OFF = 6
function GrisuDebug.parseLogLevel(txt)
local lvl = GrisuDebug[txt]
if lvl ~= nil and type(lvl) == "number" then
return lvl
else
return GrisuDebug.INFO
end
end
function GrisuDebug:create(name, id)
local d = {} -- our new object
setmetatable(d, GrisuDebug) -- make Account handle lookup
d.name = name -- initialize our object
d.id = id
d.lvl = GrisuDebug.DEBUG
return d
end
function GrisuDebug:setLogLvl(lvl)
self.lvl = lvl
end
function GrisuDebug:trace(txt, ...)
self:print(GrisuDebug.TRACE, txt, ...)
end
function GrisuDebug:debug(txt, ...)
self:print(GrisuDebug.DEBUG, txt, ...)
end
function GrisuDebug:info(txt, ...)
self:print(GrisuDebug.INFO, txt, ...)
end
function GrisuDebug:warn(txt, ...)
self:print(GrisuDebug.WARNING, txt, ...)
end
function GrisuDebug:error(txt, ...)
self:print(GrisuDebug.ERROR, txt, ...)
end
---@param name string The name of the table
---@param tbl table The table to print all members of
---@param recursive boolean
---@return void
function GrisuDebug:tPrint(name, tbl, recursive)
self:info("Debug Table: '" .. name .. "'")
print(self:_tprint(tbl, 0, recursive))
end
function GrisuDebug:print(lvl, txt, ...)
if lvl < self.lvl then
return
end
local level = "TRACE"
if lvl == GrisuDebug.ERROR then
level = "ERROR"
elseif lvl == GrisuDebug.WARNING then
level = "WARN"
elseif lvl == GrisuDebug.INFO then
level = "INFO"
elseif lvl == GrisuDebug.DEBUG then
level = "DEBUG"
end
local text
if type(txt) == "function" then
text = txt()
else
text = string.format(tostring(txt), ...)
end
if (self.id == nil) then
print(self.name .. " - " .. level .. ": " .. text)
else
print(self.name .. " - " .. level .. ": (" .. id .. " - " .. getName(id) .. ") " .. text)
end
end
---@param tbl table
---@param indent number
---@param recursive boolean
function GrisuDebug:_tprint (tbl, indent, recursive)
if not indent then
indent = 0
end
if tbl == nil then
return string.rep(" ", indent) .. "nil"
end
local toprint = string.rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
toprint = toprint .. string.rep(" ", indent)
if (type(k) == "number") then
toprint = toprint .. "[" .. k .. "] = "
elseif (type(k) == "string") then
toprint = toprint .. k .. "= "
end
if (type(v) == "number") then
toprint = toprint .. v .. ",\r\n"
elseif (type(v) == "string") then
toprint = toprint .. "\"" .. v .. "\",\r\n"
elseif (type(v) == "table") then
if recursive and indent < 10 then
toprint = toprint .. self:_tprint(v, indent + 2, recursive) .. ",\r\n"
else
toprint = toprint .. "table, \r\n"
end
else
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
end
end
toprint = toprint .. string.rep(" ", indent - 2) .. "}"
return toprint
end