Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua: exit event

Tags:

logging

exit

lua

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.

like image 561
Malyutin Egor Avatar asked Jan 28 '26 10:01

Malyutin Egor


1 Answers

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
like image 105
Paul Kulchenko Avatar answered Jan 31 '26 00:01

Paul Kulchenko