Advertisement
AquaJD

EMAL

Aug 9th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. --[[
  2.  
  3.     EMAL: Event Monitoring and Logging
  4.     Created by Dave-ee Jones
  5.    
  6.     Logs all events (except 'char' events because they are already monitored by 'key' events)
  7.     and writes them to a log file.
  8.     Useful if you want to monitor what happens to your computer while you're away from it,
  9.     or you just want to have an event logger like Windows does..
  10.    
  11. --]]
  12.  
  13. -- CONFIG: CHANGE AS NEEDED --
  14. local sLogPath = "/emal.log" -- Log path of the event log
  15. local bReplaceStartup = false -- Replace startup file and run at startup
  16. os.pullEvent = os.pullEventRaw
  17. -- END CONFIG --
  18.  
  19. -- Replaces startup if not already startup
  20. if bReplaceStartup then
  21.     local prog = shell.getRunningProgram()
  22.     if prog == "startup" then
  23.         if fs.exists("/.emal") then
  24.             local file = fs.open("/.emal","r")
  25.             local file = fs.open("/.emal","r")
  26.             fs.delete(file.readLine())
  27.             file.close()
  28.             fs.delete("/.emal")
  29.             os.reboot()
  30.         end
  31.     else
  32.         if fs.exists("/startup") then
  33.             fs.move("/startup","/.startup")
  34.         end
  35.         local file = fs.open("/.emal")
  36.         file.writeLine("/"..prog)
  37.         file.close()
  38.         fs.copy("/"..prog,"/startup")
  39.         os.reboot()
  40.     end
  41. end
  42.  
  43. -- LOG ALL THE THINGS
  44. local function fLog()
  45.     local _logFile = fs.open(sLogPath,"a")
  46.     _logFile.writeLine("-- LOG START: "..os.clock().." --")
  47.     while true do
  48.         local _e = { os.pullEvent() }
  49.         if _e[1] ~= "char" then
  50.             _logFile.write("["..os.clock().."] ")
  51.             _logFile.write(_e[1].." ")
  52.             if string.sub(_e[1],1,3) == "key" then
  53.                 _logFile.write(keys.getName(_e[2]))
  54.             else
  55.                 if _e[2] then
  56.                     _logFile.write(_e[2].." ")
  57.                 end
  58.                 if _e[3] then
  59.                     _logFile.write(_e[3].." ")
  60.                 end
  61.                 if _e[4] then
  62.                     _logFile.write(_e[4].." ")
  63.                 end
  64.             end
  65.             _logFile.writeLine("")
  66.         end
  67.     end
  68. end
  69.  
  70. local function fRun() end
  71. if bReplaceStartup then
  72.     function fRun() term.clear() term.setCursorPos(1,1) shell.run("/.startup") end
  73. else
  74.     function fRun() term.clear() term.setCursorPos(1,1) shell.run("/rom/programs/shell") end
  75. end
  76.  
  77. -- The coroutines
  78. local co1 = coroutine.create(fRun)
  79. local co2 = coroutine.create(fLog)
  80.  
  81. -- The loop that makes all loops happen
  82. local e = {}
  83. while true do
  84.     coroutine.resume(co1,unpack(e))
  85.     coroutine.resume(co2,unpack(e))
  86.     e = { os.pullEvent() }
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement