Advertisement
Qivex

Eventlog

Jan 30th, 2021 (edited)
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.77 KB | None | 0 0
  1. local args = {...}
  2.  
  3. if #args < 2 then
  4.     print("Usage: eventlog <logfile> <program> <arguments>")
  5.     return
  6. end
  7.  
  8. local logfile = table.remove(args, 1)
  9. local program = table.remove(args, 1)
  10.  
  11. local log = function(text)
  12.     local file = fs.open(logfile, "a")
  13.     file.writeLine(text)
  14.     file.close()
  15. end
  16.  
  17. local main = coroutine.create(function() shell.run(program, unpack(args)) end)
  18. log("Started " .. program .. " @ " .. os.clock())
  19. coroutine.resume(main)
  20. while coroutine.status(main) ~= "dead" do
  21.     local params = {coroutine.yield()}  -- equals os.pullEventRaw()
  22.     local event = table.remove(params, 1)
  23.     log(event .. " @ " .. os.clock() .. ": " .. textutils.serialize(params))
  24.     coroutine.resume(main, event, unpack(params))
  25. end
  26. log("Stopped " .. program .. " @ " .. os.clock())
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement