lua中如何实现打印行号和当前函数名?
lua中可以使用debug的get_info方法来获取当前的堆栈信息,进而可以打印出相关的环境信息,具体信息包括:
table: 0x7f92b8f19e80
{
isvararg = false ,
what = Lua ,
func = function: 0x7f92b7b8d600 ,
namewhat = ,
istailcall = true ,
lastlinedefined = 16 ,
source = @service/db/db_interactionemotion.lua ,
linedefined = 7 ,
currentline = 11 ,
ntransfer = 0 ,
short_src = service/db/db_interactionemotion.lua ,
ftransfer = 0 ,
nups = 3 ,
nparams = 0 ,
}
lua中需要打印行号多半是日志需求,下面是一个demo:
local logger_api = require 'common.logger_api'
local tag = "test"
local test_id = 0
local M = {}
local function get_debuginfo(info)
--utils.var_dump(info)
local d = ""
if info ~= nil and info.short_src ~= nil and info.currentline ~= nil then
d = info.short_src .. ":" .. info.currentline .. ":"
end
return d
end
function M.ELOG(...)
local info = debug.getinfo(2)
local d = get_debuginfo(info)
logger_api.ERROR(string.format("%s[E]no_%s_%s",d,test_id,tag),...)
end
function M.WLOG(...)
local info = debug.getinfo(2)
local d = get_debuginfo(info)
logger_api.WARN(string.format("%s[W]no_%s_%s",d,test_id,tag),...)
end
function M.NLOG(...)
logger_api.INFO(string.format("[N]no_%s_%s", test_id,tag), ...)
end
function M.DLOG(...)
local info = debug.getinfo(2)
local d = get_debuginfo(info)
logger_api.DEBUG(string.format("%s[D]no_%s_%s",d,test_id,tag),...)
end
return M
这篇博客介绍了在 Lua 中如何利用 debug.getinfo 方法获取当前堆栈信息,从而实现打印行号和函数名的功能。示例代码展示了如何创建一个日志模块,通过 ELOG, WLOG, NLOG 和 DLOG 函数进行不同级别的日志输出,便于调试和记录程序运行状态。
7005

被折叠的 条评论
为什么被折叠?



