如何在LuCI里添加print?

这个方式是从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.lua

got follow error:

/usr/lib/lua/luci/dispatcher.lua:448: Failed to execute call dispatcher
target 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 nil
value)
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.lua
	index 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 = print

Why? 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 for
debugging output, since you can easily loose some hairs with stdout/stderr
debugging in the Luci development :-)

1. http://www.lua.org/manual/5.1/manual.html#pdf-module


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值