I'm writing library for logging in Lua with some advanced features like log updating. Are there any exit event in pure Lua? I'm going to use it for avoiding cursor hides after process exit.
As Egor wrote in the comment, you can use __gc metamethod to catch the event of the final garbage collection in Lua 5.2+; you'll need to use undocumented newproxy in Lua 5.1. The following code should work in Lua 5.1 and later interpreters:
local m = {onexit = function() print("exiting...") end}
if _VERSION >= "Lua 5.2" then
setmetatable(m, {__gc = m.onexit})
else
m.sentinel = newproxy(true)
getmetatable(m.sentinel).__gc = m.onexit
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With