Reputation: 169
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.
Upvotes: 3
Views: 683
Reputation: 26774
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
Upvotes: 4