Advertisement
Kodos

Logger.lua (WIP)

Jun 26th, 2014
373
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.  
  15. -- if #({...}) > 2 then -- Uncomment this when the mechanics of function runtokill() works.
  16. if #({...}) ~= 2 then
  17.   error("Usage: Logger (# of ports to listen to, starting at 1) (# of messages to write to file before program ends.", 0)
  18.   return
  19. end
  20.  
  21. --[[ if #({...}) == 1 then
  22.   runtokill()
  23.   return
  24. end
  25.  
  26. function runtokill()
  27.   while true do
  28.     local h = io.open("Logs.txt", "a")
  29.       local _, _, from, port, _, message = event.pull("modem_message")
  30.       print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message))
  31.       h:write("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message) .. "\n")
  32.       local _, _, _, char, _, _ = event.pull("key_down")
  33.         if char == keyboard.keys.q then
  34.         break
  35.     end
  36.   end ]]--
  37.  
  38. -- You can ignore this snippet of code, unless you're
  39. -- ping. Or unless you really wanna wait half an hour
  40. -- for all ports to open. Luckily if you're running this
  41. -- on a Minecraft server, you only need to do this once.
  42. if ports == "potato" then
  43.   ports = 65535
  44. end
  45.  
  46. -- This is to check whether or not the argument being passed
  47. -- as ports is actually a number, and not words or symbols.
  48. if not tonumber(ports) then
  49.   error("The port needs to be a number!",0)
  50.   return
  51. end
  52.  
  53. -- Same thing as our first check, only here we're checking the
  54. -- messages amount argument.
  55. if not tonumber(msgs) then
  56.   error("The amount of messages needs to be a number!", 0)
  57.   return
  58. end
  59.  
  60. -- First, we have to open our ports that we want to listen to.
  61. -- Note the maximum on our range is the variable we defined in our
  62. -- first argument.
  63. for prt = 1,ports do
  64.   modem.open(prt)
  65. end
  66.  
  67. -- This is where the magic happens. This is where we open our log file, or
  68. -- create one if it doesn't exist, and listen for messages to write to our
  69. -- file until we've caught enough messages to satisfy our second argument.
  70. local h = io.open("Logs.txt", "a")
  71. for n = 1, tonumber(msgs) do
  72.   local _, _, from, port, _, message = event.pull("modem_message")
  73.   print("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message))
  74.   h:write("Got a message from " .. from .. " on port " .. port .. ": " .. tostring(message) .. "\n")
  75. end
  76.  
  77. -- Now that we have enough messages, we can close the ports we opened earlier on.
  78. modem.close()
  79. -- Notice I didn't specify a port?  That's because
  80. -- by default in OC, when a port isn't defined, all
  81. -- ports are closed when this method is called.
  82.  
  83. -- Now we must close our log file to ensure that it saves.
  84. h:close()
  85.  
  86. -- This lets the user know that the logs were successfully saved.
  87. -- Huzzah!
  88. print("Saved logs to Logs.txt.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement