Advertisement
Guest User

logger.lua

a guest
Jun 26th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.52 KB | None | 0 0
  1. -- We require our libraries and modules here
  2. local component = require("component")
  3. local modem = component.modem
  4. local io = require("io")
  5. local event = require("event")
  6. local fs = require("filesystem")
  7.  
  8. -- This line will allow the user to easily specify how many
  9. -- ports, and messages, we want to use with the program.
  10. local ports, msgs = ...
  11.  
  12. -- This ensures that the proper amount of arguments are being used,
  13. -- and if not, will display proper usage of running the program
  14. if #({...}) > 2 then
  15.   error("Usage: Logger (# of ports to listen to, starting at 1) (# of messages to write to file before program ends.", 0)
  16.   return
  17. end
  18.  
  19. if #({...}) == 1 then
  20.   runtokill()
  21.   return
  22. end
  23.  
  24. function runtokill()
  25.   while true do
  26.    
  27.  
  28. -- You can ignore this snippet of code, unless you're
  29. -- ping. Or unless you really wanna wait half an hour
  30. -- for all ports to open. Luckily if you're running this
  31. -- on a Minecraft server, you only need to do this once.
  32. if ports == "potato" then
  33.   ports = 65535
  34. end
  35.  
  36. -- This is to check whether or not the argument being passed
  37. -- as ports is actually a number, and not words or symbols.
  38. if not tonumber(ports) then
  39.   error("The port needs to be a number!",0)
  40.   return
  41. end
  42.  
  43. -- Same thing as our first check, only here we're checking the
  44. -- messages amount argument.
  45. if not tonumber(msgs) then
  46.   error("The amount of messages needs to be a number!", 0)
  47.   return
  48. end
  49.  
  50. -- First, we have to open our ports that we want to listen to.
  51. -- Note the maximum on our range is the variable we defined in our
  52. -- first argument.
  53. for prt = 1,ports do
  54.   modem.open(prt)
  55. end
  56.  
  57. -- This is where the magic happens. This is where we open our log file, or
  58. -- create one if it doesn't exist, and listen for messages to write to our
  59. -- file until we've caught enough messages to satisfy our second argument.
  60. local h = io.open("Logs.txt", "a")
  61. for n = 1, tonumber(msgs) do
  62.   local _, _, from, port, _, message = event.pull("modem_message")
  63.   print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message))
  64.   h:write("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message) .. "\n")
  65. end
  66.  
  67. -- Now that we have enough messages, we can close the ports we opened earlier on.
  68. modem.close()
  69. -- Notice I didn't specify a port?  That's because
  70. -- by default in OC, when a port isn't defined, all
  71. -- ports are closed when this method is called.
  72.  
  73. -- Now we must close our log file to ensure that it saves.
  74. h:close()
  75.  
  76. -- This lets the user know that the logs were successfully saved.
  77. -- Huzzah!
  78. print("Saved logs to Logs.txt.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement