lua如何打印行号_打印lua堆栈,包括文件名,行号,函数

function printCallStack()

local startLevel = 2 --0表示getinfo本身,1表示调用getinfo的函数(printCallStack),2表示调用printCallStack的函数,可以想象一个getinfo(0级)在顶的栈.

local maxLevel = 10--最大递归10层

for level = startLevel, maxLevel do

-- 打印堆栈每一层

local info = debug.getinfo( level, "nSl")

if info == nil then break end

print( string.format("[ line : %-4d] %-20s :: %s", info.currentline, info.name or "", info.source or "" ) )

-- 打印该层的参数与局部变量

local index = 1 --1表示第一个参数或局部变量, 依次类推

while true do

local name, value = debug.getlocal( level, index )

if name == nil then break end

local valueType = type( value )

local valueStr

if valueType == 'string' then

valueStr = value

elseif valueType == "number" then

valueStr = string.format("%.2f", value)

end

if valueStr ~= nil then

print( string.format( "\t%s = %s\n", name, value ) )

end

index = index + 1

end

end

end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值