OpenWrt Luci 调试日志输出的一种方法
luci原生的调试接口用起来不太舒服,在网上搜到如下调试方法。
将下面的lua代码保存为 log.lua,然后放置于 /usr/lib/lua/luci ,即可在 luci 任意目录进行调用
local M = {}
local tconcat = table.concat
local tinsert = table.insert
local srep = string.rep
local function local_print(str)
local dbg = io.open("/tmp/luci.output", "a+")
local str = str or ""
if dbg then
dbg:write(str..'
')
dbg:close()
end
end
function M.print(...)
local dbg = io.open("/tmp/luci.output", "a+")
if dbg then
dbg:write(os.date("[%H:%M:%S]: "))
for _, o in ipairs({...}) do
dbg:write(tostring(o)..' ')
end
dbg:write("
")
dbg:close()
end
end
function M.print_r(data, depth)
local depth = depth or 3
local cstring = "";
local top_flag = true
local function table_len(t)
local i = 0
for k, v in pairs(t) do
i = i + 1
end
return i
end
local function tableprint(data,cstring, local_depth)
if data == nil then
local_print("core.print data is nil");
end
local cs = cstring .. " ";
if top_flag then
local_print(cstring .."{");
top_flag = false
end
if(type(data)=="table") then
for k, v in pairs(data) do
if type(v) ~= "table" then
if type(v) == "string" then
local_print(cs..tostring(k).." = ".."'"..tostring(v).."'");
else
local_print(cs..tostring(k).." = "..tostring(v));
end
elseif table_len(v) == 0 then
local_print(cs..tostring(k).." = ".."{}")
elseif local_depth < depth then
local_print(cs..tostring(k).." = {");
tableprint(v,cs,local_depth+1);
else
local_print(cs..tostring(k).." = ".."{*}")
end
end
else
local_print(cs..tostring(data));
end
local_print(cstring .."}");
end
tableprint(data,cstring,0);
end
return M
调用方法:
-- 以刚刚代码块为例子
local password
-- 导入log模块
local log = require "luci.log"
-- 获取传入数据
password = luci.http.formvalue('password')
-- 打印log
log.print(password)
log.print_r(password)
在终端用tail -f跟踪日志输出文件/tmp/luci.output
tail -f /tmp/luci.output
转自https://blog.****.net/bailyzheng/article/details/48663369