当前位置:网站首页>Lua log implementation -- print table
Lua log implementation -- print table
2022-07-29 01:52:00 【wufeng_ asia】
Print the results

usage
print_r(table,"post method:Login")
Code
local rpc_log_off = 0 -- 1 close log,0 open log
local function write_file(fd,str)
if fd then
fd:write(str .. '\n')
end
end
local function dump_value_(v)
if type(v) == "string" then
v = "\"" .. v .. "\""
end
return tostring(v)
end
local function split(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter == "") then return false end
local pos, arr = 0, {
}
for st, sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
local function trim(input)
return (string.gsub(input, "^%s*(.-)%s*$", "%1"))
end
local function dump(dbg,value, desciption, nesting)
if type(nesting) ~= "number" then nesting = 3 end
local lookupTable = {
}
local result = {
}
local traceback = split(debug.traceback("", 2), "\n")
-- print("dump from: " .. trim(traceback[3]))
local function dump_(value, desciption, indent, nest, keylen)
desciption = desciption or "<var>"
local spc = ""
if type(keylen) == "number" then
spc = string.rep(" ", keylen - string.len(dump_value_(desciption)))
end
if type(value) ~= "table" then
result[#result +1 ] = string.format("%s%s%s = %s", indent, dump_value_(desciption), spc, dump_value_(value))
elseif lookupTable[tostring(value)] then
result[#result +1 ] = string.format("%s%s%s = *REF*", indent, dump_value_(desciption), spc)
else
lookupTable[tostring(value)] = true
if nest > nesting then
result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, dump_value_(desciption))
else
result[#result +1 ] = string.format("%s%s = {", indent, dump_value_(desciption))
local indent2 = indent.." "
local keys = {
}
local keylen = 0
local values = {
}
for k, v in pairs(value) do
keys[#keys + 1] = k
local vk = dump_value_(k)
local vkl = string.len(vk)
if vkl > keylen then keylen = vkl end
values[k] = v
end
table.sort(keys, function(a, b)
if type(a) == "number" and type(b) == "number" then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
dump_(values[k], k, indent2, nest + 1, keylen)
end
result[#result +1] = string.format("%s}", indent)
end
end
end
dump_(value, desciption, "- ", 1)
local len = dbg:seek("end")
if len > (2*1024*1024) then
os.execute("echo '' > /tmp/rpc.log")
end
for i, line in ipairs(result) do
write_file(dbg,line)
end
end
local function print_string(dbg,str)
write_file(dbg,str)
end
--[[ Print table The tool function of @params data What needs to be printed @params key describe @params depth Nested levels of printed content , Default 3 level ]]
function print_r(data, key, depth)
if not key then
key = ""
end
if not depth then
depth = 3
end
local dbg = io.open("/tmp/rpc.log", "w+")
if rpc_log_off == 0 then
if (type(data) == "table") then
dump(dbg,data,key,depth)
elseif (type(data) == "string") then
print_string(dbg,key .. data);
end
else
--log off
end
if dbg then
dbg:close()
end
end
边栏推荐
- [search] - iteration deepening / bidirectional dfs/ida*
- DSP vibration seat
- New upgrade: get Taobao product details "advanced version" API
- What are source code, inverse code and complement code
- Data platform data access practice
- Embedded sharing collection 23
- Practical experience of Google cloud spanner
- New 1688 API access instructions
- 【流放之路-第八章】
- BOM系列之window对象
猜你喜欢

Super scientific and technological data leakage prevention system, control illegal Internet behaviors, and ensure enterprise information security
![[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] ranked first in SaaS of pure public cloud in three years, and yonsuite's "flywheel effect"

Analyzing the function of human-computer interface module of runtime manager based on autoware

What are source code, inverse code and complement code

Leetcode 112: path sum

matplotlib中文问题

J9 number theory: what factors determine the value of NFT?

Sigma-DSP-OUTPUT

把逻辑做在Sigma-DSP中的优化实例-数据分配器

Basic label in body
随机推荐
New upgrade: get Taobao product details "advanced version" API
Platofarm community ecological gospel, users can get premium income with elephant swap
关于df[‘某一列名’][序号]
【流放之路-第三章】
JS 定时器setInterval clearInterval 延时器setTimeOut 异步 动画
In depth analysis of C language memory alignment
Tomorrow infinite plan, 2022 conceptual planning scheme for a company's yuanuniverse product launch
With the explosive growth of digital identity in 2022, global organizations are facing greater network security
HCIA configuration instance (ENSP)
Reinforcement learning (II): SARS, with code rewriting
秘术冬潮烙技能搭配
Timer of BOM series
Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen
[WesternCTF2018]shrine
【流放之路-第四章】
HCIA配置实例(eNSP)
Make logic an optimization example in sigma DSP - data distributor
560 and K
[WesternCTF2018]shrine
TDA75610-I2C-模拟功放I2C地址的确定