有没有一种方法可以在lua中捕获输出?

我正在尝试捕获例如print('Hello')的输出并将其存储在变量/表中。

如果可能,请告诉我。 如果不行,感谢您的回答。

点赞
用户3574628
用户3574628

你不能直接拦截标准输出,但是你可以在全局范围内改变 print 函数:

local outputs = {}
local function storeOutputs(...)
  table.insert(outputs, {...})
end

local oldPrint = print
function print(...)
  storeOutputs(...)
  oldPrint(...)
end

我不确定是否有一种方法来处理 io.write 调用。

2020-03-01 18:50:05