【问题标题】:Is it possible to change the output of Lua error messages?是否可以更改 Lua 错误消息的输出?
【发布时间】:2019-01-20 13:33:01
【问题描述】:

我设法通过修改 dbg_printf 方法来更改错误消息的输出。但是,该方法不处理以下错误消息:

lua: ?:0: attempt to call global 'log' (a nil value)

哪些方法可以处理这些类型的错误?

【问题讨论】:

  • 我不知道Lua在这些平台上的详细情况,但是Lua内核并没有输出任何错误信息。在 Lua 中捕获错误的正确方法是使用 pcalllua_pcall
  • @lhf 我发现了一个名为 lua_cpcall 的函数声明。我会尝试修改它,看看是不是那个。

标签: lua esp8266 nodemcu firmware


【解决方案1】:

错误消息来自函数luaG_typeerror 中的文件ldebug.c。但我猜你使用的是较旧的 Lua 版本,因为我的信息有点不同:

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

如果可以的话,你应该尽量避免错误:

if type(log) == "function" then
   log() 
end

或如@lhf 所说,使用pcall

if pcall(log) then
    -- no errors while running 'log'
    ...
else
    -- 'log' raised an error: take appropriate actions
    ...
end

【讨论】:

  • “您使用的是较旧的 Lua 版本” - 正确,NodeMCU 固件使用 Lua 5.1.3。
【解决方案2】:

它应该比深入研究 C api 更简单。

就像@lhf 说的:

if pcal(risky) then 
  print("this works")
else
  print("phooey!")
end

或者,您可以停止程序并收到如下错误消息:

if pcal(risky) then 
  print("this works")
else
  error("your error message here")
end

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 2020-06-02
    • 2021-02-01
    • 2015-08-18
    • 2019-11-30
    • 2013-08-03
    • 1970-01-01
    • 2022-06-13
    • 2013-04-11
    相关资源
    最近更新 更多