luci 打印函数封装
2016-01-11 20:37
507 查看
最近把luci代码深入地剖析了一遍.说实话刚开始看的时候也是云里雾里,特别是dispatch函数, 这其间也是参考了几篇文章, 特此感谢.
刚开始看luci代码确实就和逆向没啥区别, 需要揣摩作者对于各个变量的用途. 于是我就想了一个办法, 就是把每个变量都打印出来.
为此我在/usr/lib/lua/luci目录下引入了log.lua模块:
你可以在luci目录下任何一个地方调用
另外, log模块将输出信息到/tmp/luci.ouput下面, 我们可以用tail命令跟踪.
于是通过这个小小的模块, 得以一窥这个openwrt上著名的程序. 确实很有趣, 有时间我会详细的把luci框架分析写下来.
另外luci程序自带了一个debug模块, 这是一个用来分析内存占用情况的模块, 你也可以在dispatcher.lua模块中开启.它的信息记录在/tmp/memtrace中.
Luci实现框架
Luci tutorials
wifidog在OpenWrt路由器上添加LuCI的模块
转自https://forestgump.me/2015/03/28/luci-debug/
刚开始看luci代码确实就和逆向没啥区别, 需要揣摩作者对于各个变量的用途. 于是我就想了一个办法, 就是把每个变量都打印出来.
为此我在/usr/lib/lua/luci目录下引入了log.lua模块:
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..'\n') 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("\n") 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
你可以在luci目录下任何一个地方调用
local log = require "luci.log" log.print("Hello World") log.print_r({"Hello", "World"})
另外, log模块将输出信息到/tmp/luci.ouput下面, 我们可以用tail命令跟踪.
# tail -f /tmp/luci.output
于是通过这个小小的模块, 得以一窥这个openwrt上著名的程序. 确实很有趣, 有时间我会详细的把luci框架分析写下来.
另外luci程序自带了一个debug模块, 这是一个用来分析内存占用情况的模块, 你也可以在dispatcher.lua模块中开启.它的信息记录在/tmp/memtrace中.
参考
Luci实现框架Luci tutorials
wifidog在OpenWrt路由器上添加LuCI的模块
转自https://forestgump.me/2015/03/28/luci-debug/
相关文章推荐
- 完全指南:在 Linux 中如何打印和管理打印机
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言