-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvardump.lua
More file actions
113 lines (95 loc) · 3.05 KB
/
Copy pathvardump.lua
File metadata and controls
113 lines (95 loc) · 3.05 KB
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
-- Copyright 2022 xresloader. All rights reserved.
--region vardump.lua
--Author : owent
--Date : 2014-10-22
_G.vardump_default = {
show_all = false, -- 显示隐藏对象(以__开头)
show_metatable = false, -- 展开metatable
show_table_once = false, -- 每个名称的table只显示一次
ident = " ", -- 缩进符号
symbol_name = "__classname", -- 符号名称保存位置
recursive = nil, -- 递归打印的层数(nil表示不限制打印层数 -_-||)
ostream = io.stdout -- 输出目标
}
-- 额外拓展 -- vardump
_G.vardump = function(var, conf)
local rm = {}
local cfg = {};
for k, v in pairs(_G.vardump_default) do
cfg[k] = _G.vardump_default[k]
end
if "table" == type(conf) then
for k, v in pairs(conf) do
cfg[k] = v
end
end
local function pvar(obj, ident, root, recursive)
local t = type(obj)
local ident_symbol = cfg.ident
if nil ~= recursive then
recursive = recursive - 1
end
if nil == t then
cfg.ostream:write("nil")
elseif "table" == t and (nil == recursive or recursive >= 0) then
local name = tostring(obj)
cfg.ostream:write(name)
if nil == rawget(rm, name) then
-- 防止环形引用
rawset(rm, name, " ### [reference to " .. root .. "]")
cfg.ostream:write(" {\n")
-- metatable
if cfg.show_all or cfg.show_metatable then
local meta = getmetatable(obj)
cfg.ostream:write(ident .. ident_symbol .. "__metatable : ")
if cfg.show_metatable then
pvar(meta, ident .. ident_symbol, name, recursive)
else
cfg.ostream:write(tostring(meta))
if "table" == type(meta) and cfg.symbol_name and nil ~= meta[cfg.symbol_name] then
cfg.ostream:write(" [" .. tostring(meta[cfg.symbol_name]) .. "]")
end
end
cfg.ostream:write("\n")
end
for k, v in pairs(obj) do
if cfg.show_all or '__' ~= string.sub(k, 1, 2) then
cfg.ostream:write(ident .. ident_symbol)
pvar(k, ident .. ident_symbol, name, recursive)
cfg.ostream:write(" : ")
pvar(v, ident .. ident_symbol, name, recursive)
cfg.ostream:write("\n")
end
end
cfg.ostream:write(ident .. "}")
-- 如果不是强制只显示一次table则允许再显示
if cfg.show_table_once then
rawset(rm, name, nil)
end
else
cfg.ostream:write(rawget(rm, name))
end
else
cfg.ostream:write(tostring(obj))
end
end
pvar(var, "", "{root}", cfg.recursive)
cfg.ostream:write("\n")
cfg.ostream:flush()
end
-- 额外拓展 -- vardbg_assert
_G.vardbg_assert = function(var, check, conf, callback)
if check then
return
end
vardump(var, conf)
if nil ~= callback then
callback()
end
end
-- 额外拓展 -- stackdump -- 用于c api的lua_pcall传入的hmsg,追加堆栈信息
_G.stackdump = function(msg)
return debug.traceback(msg, 2)
end
return _G.vardump, _G.vardbg_assert, _G.stackdump
--endregion