如何在LuCI里添加print?

来源:互联网 发布:淘宝自动刷单平台 编辑:程序博客网 时间:2024/05/16 10:47

这个方式是从openwrt的maillist里找到的,适合那些想修改luci,但是对lua语言又不熟悉的人。

大意如下:

某人问在LuCI里添加print("hello"),出现错误。为啥?

attempt to call global 'print' (a nil value)

然后,下面有个人回复说,需要在文件开头添加这么一行:

local print = print
因为这个文件是一个module,它调用了module函数。详细信息,可以参考Lua手册


原文如下:

Hi all,when i add above    'print("hello")'  in /usr/lib/lua/luci/model/uci.luagot follow error:/usr/lib/lua/luci/dispatcher.lua:448: Failed to execute call dispatchertarget for entry '/servicectl/restart/firewall'.The called action terminated with an exception:/usr/lib/lua/luci/model/uci.lua:71: attempt to call global 'print' (a nilvalue)stack traceback:[C]: in function 'assert'/usr/lib/lua/luci/dispatcher.lua:448: in function 'dispatch'/usr/lib/lua/luci/dispatcher.lua:195: in function </usr/lib/lua/luci/dispatcher.lua:194>why cannot use print to print some value?thanks all!elvis--------------------------------------------------------------------------------------You need to define it first at the top of the file:diff --git a/libs/core/luasrc/model/uci.lua b/libs/core/luasrc/model/uci.luaindex a394563..f513c77 100644--- a/libs/core/luasrc/model/uci.lua+++ b/libs/core/luasrc/model/uci.lua@@ -27,6 +27,7 @@ local os    = require "os" local uci   = require "uci" local util  = require "luci.util" local table = require "table"+local print = printWhy? It's because of the following function call:module "luci.model.uci"For the details, read the fine Lua manual[1]. BTW, it's better to use file fordebugging output, since you can easily loose some hairs with stdout/stderrdebugging in the Luci development :-)1. http://www.lua.org/manual/5.1/manual.html#pdf-module


0 0