Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local time_url = "http://www.timeapi.org/pdt/now?\\Q"
- local filename_url = "http://www.timeapi.org/pdt/now?\\Y-\\m-\\d%23\\H"
- if not fs.exists(".logs") then
- fs.makeDir(".logs")
- end
- -- Time
- local realTime = tonumber(http.get(time_url).readAll())
- local osTime = os.clock()
- local lagAdjust = 1
- local function getTime()
- return realTime/1000 + (os.clock() - osTime)*lagAdjust
- end
- local logname = ".logs/"..http.get(filename_url).readAll()
- local logfile = io.open(logname, "a")
- local loghour = math.floor(((realTime/1000)%86400)/3600)
- local notLogged = {}
- local function log(line)
- local hr = math.floor((getTime()%86400)/3600)
- if hr ~= loghour then
- if #notLogged == 0 then
- http.request(filename_url)
- end
- table.insert(notLogged)
- else
- logfile:write(line.."\n")
- end
- end
- local function pad(s, n, char)
- char = char or "0"
- return char:rep(n-tostring(s):len())..s
- end
- term.clear()
- term.setCursorPos(1, 1)
- local timer = os.startTimer(60)
- local terminated = false
- while true do
- local e = {os.pullEventRaw()}
- if e[1] == "chat_message" or e[1] == "chatbox_command" then
- if e[1] == "chatbox_command" then e[4] = "##"..e[4] end
- local t = getTime()
- local timestamp = "[" ..pad(math.floor((t%86400)/3600),2)..":"..pad(math.floor((t%3600)/60),2)..":"..pad(math.floor((t%60)),2).."] "
- local sender = e[3]..": "
- local message = e[4]
- term.setTextColor(colors.yellow)
- write(timestamp)
- term.setTextColor(colors.red)
- write(sender)
- term.setTextColor(colors.white)
- print(message)
- log(timestamp..sender..message)
- elseif e[1] == "http_success" then
- if e[2] == time_url then
- local t = tonumber(e[3].readAll())
- if t then
- now = os.clock()
- lagAdjust = ((t-realTime)/1000) / (now-osTime)
- realTime = t
- osTime = now
- end
- elseif e[2] == filename_url then
- logfile:close()
- logname = ".logs/"..e[3].readAll()
- logfile = io.open(logname, "a")
- loghour = math.floor((getTime()%86400)/3600)
- for i=1,#notLogged do
- logfile:write(notLogged[i].."\n")
- end
- if terminated then break end
- notLogged = {}
- end
- elseif e[1] == "http_failure" and e[2] == filename_url and terminated then
- term.setTextColor(colors.red)
- print("Could not log "..#notLogged.." messages")
- break
- elseif e[1] == "timer" and e[2] == timer then
- http.request(time_url)
- timer = os.startTimer(60)
- elseif e[1] == "terminate" then
- if #notLogged == 0 then
- break
- else
- term.setTextColor(colors.orange)
- print("Waiting for logging to finish...")
- terminated = true
- end
- end
- end
- print("Exiting...")
- logfile:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement