Advertisement
Kodos

logger.lua

Jun 25th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. -- You can ignore this snippet of code, unless you're
  20. -- ping. Or unless you really want to wait half an hour
  21. -- for all ports to open. Luckily if you're running this
  22. -- on a Minecraft server, you will only need to do this once,
  23. -- so long as you set a high enough message count.
  24. if ports == "potato" then
  25.   ports = 65535
  26. end
  27.  
  28. -- This is to check whether or not the argument being passed
  29. -- as ports is actually a number, and not words or symbols.
  30. if not tonumber(ports) then
  31.   error("The port needs to be a number!",0)
  32.   return
  33. end
  34.  
  35. -- Same thing as our first check, only here we're checking the
  36. -- messages amount argument.
  37. if not tonumber(msgs) then
  38.   error("The amount of messages needs to be a number!", 0)
  39.   return
  40. end
  41.  
  42. -- Now, we have to open our ports that we want to listen to.
  43. -- Note the maximum on our range is the variable we defined in our
  44. -- first argument.
  45. for prt = 1,ports do
  46.   modem.open(prt)
  47. end
  48.  
  49. -- This is where the magic happens. This is where we open our log file, or
  50. -- create one if it doesn't exist, and listen for messages to write to our
  51. -- file until we've caught enough messages to satisfy our second argument.
  52. local h = io.open("Logs.txt", "a")
  53. for n = 1, tonumber(msgs) do
  54.   local _, _, from, port, _, message = event.pull("modem_message")
  55.   print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message))
  56.   h:write("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message) .. "\n")
  57. end
  58.  
  59. -- Now that we have enough messages, we can close the ports we opened earlier on.
  60. modem.close()
  61. -- Notice I didn't specify a port?  That's because
  62. -- by default in OC, when a port isn't defined, all
  63. -- ports are closed when this method is called.
  64.  
  65. -- Now we must close our log file to ensure that it saves.
  66. h:close()
  67.  
  68. -- This lets the user know that the logs were successfully saved.
  69. -- Huzzah!
  70. print("Saved logs to Logs.txt.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement