Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Script by Savage_Me55iah of CraftAU.com.au--
- --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
- --INSTRUCTIONS ON USE--
- --Instruction video on http://youtube.com/savagemessiah5
- --Contact Savage_Me55iah @ CraftAU.com.au or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
- local scriptName = "logger"
- fileDetail = fileDetail or {}
- fileDetail[scriptName] = {["version"] = 0.1, ["pastebin"] = "9zBzHK0N"}
- local tArgs = {...}
- if tArgs[1] == "update" then
- print("UPDATING") shell.run("pastebin get "..fileDetail[scriptName].pastebin.." "..scriptName.."_update")
- if fs.exists(scriptName.."_update") then fs.remove(shell.getRunningProgram()) fs.copy(scriptName.."_update", shell.getRunningProgram()) fs.remove(scriptName.."_update") print("UPDATE SUCCESS") else print("UPDATE FAILED") end
- end
- -----------------------------------------------------------------------------------------------------------
- -- Variables --
- -----------------------------------------------------------------------------------------------------------
- local datastore = "DataStore/"
- local currLogDir, prevLogDir = datastore.."currentLog/", datastore.."previousLogs/"
- local currLog = currLogDir.."LOG:"..os.day()--.."-"..os.time()
- local currLogTable = currLogDir.."LOGTABLE-"..os.day()--.."-"..os.time()
- local errorLog = "DataStore/Errors/Error"
- logTable = {}
- -----------------------------------------------------------------------------------------------------------
- -- API Functions --
- -----------------------------------------------------------------------------------------------------------
- -- fileList(functionNum, string) -- [1] returns true if function ran correctly OR if false variable was nil [2] returns variable OR false if nil
- -- fixes the fs.list() returning a function
- --function fileList(param_functionNum, param_location)
- -- local result, functionNum = {false, false}, param_functionNum.."/002"
- -- local tempFunc = nil
- -- if tostring(param_location) ~= "nil" then
- -- tempFunc = fs.list(tostring(param_location))
- -- local hold = tostring(tempFunc())
- -- result = {true, {}}
- -- while hold ~= "nil" do
- -- table.insert(result[2], hold)
- -- hold = tostring(tempFunc())
- -- end end
- -- return result
- --end
- -- logger.logSetup(functionNum) -- [1] returns true if function ran correctly OR if false variable was nil
- -- runs at the beginning of a program, moves old logs to previous logs folder
- function logSetup(param_functionNum)
- local result, functionNum = {false}, param_functionNum.."/002"
- local tempTable = {}
- if fs.exists(currLogDir) then
- tempTable = fs.list(currLogDir) or false
- if tempTable ~= false then
- for a,b in pairs(tempTable) do
- if fs.copy(currLogDir..b, prevLogDir..b) then
- result[1] = true
- fs.delete(currLogDir..b)
- end end end
- end
- if fs.exists(errorLog) then fs.delete(errorLog) end
- return result
- end
- -- logger.saveToFile(functionNum, string, 'a', string) -- [1] returns true if function ran correctly OR if false variable was nil
- -- shortcut for the open/write/close funcitons
- function saveToFile(param_functionNum, param_loc, param_mode, param_text)
- local result, functionNum = {false, false}, param_functionNum.."/002"
- local fh = fs.open(param_loc, param_mode) or false
- -- print(param_functionNum..param_loc..param_mode..param_text)
- if tostring(fh) ~= "false" then
- fh.write(tostring(param_text))
- fh.close()
- end
- return result
- end
- -- logger.logger(functionNum, {string, table, etc}, number, string) -- [1] returns true if function ran correctly OR if false variable was nil
- -- main function. assign, records and displays logged text, use separate program to overview the log table
- function logger(param_functionNum, param_text, param_logLevel)
- local result, functionNum = {false, false}, param_functionNum.."/002"
- local text, attachN, attachE, tempList, separators = nil, ">", "--", {}, param_separators or false
- if tostring(param_text) ~= nil then
- if type(param_text) == "table" and tostring(param_text[1]) ~= "nil" then
- for a,b in pairs(param_text) do
- if type(b) == "table" then for c,d in pairs(b) do table.insert(tempList, tostring(d)) end else table.insert(tempList, tostring(b)) end
- end
- for num=1,table.maxn(tempList) do
- if num == 1 then text = tempList[num] else if not separators then text = text..tempList[num] else text = text..separators..tempList[num] end end
- end
- table.insert(logTable, {["text"] = text, ["logLevel"] = param_logLevel, ["time"] = os.time(), ["functionNum"] = param_functionNum})
- if param_logLevel > 0 then
- text = attachE..param_logLevel.." "..text.." @ "..param_functionNum
- saveToFile(functionNum, errorLog, 'a', text)
- else text = attachN..param_logLevel..text end
- saveToFile(functionNum, currLog, 'a', text)
- saveToFile(functionNum, currLogTable, 'w', textutils.serialize(logTable))
- if tostring(gui) ~= "nil" then
- for a,b in pairs(gui) do
- if tostring(computerType) == "computerCraft" then
- elseif tostring(computerType) == "openComputers" then
- if gui[a]["currWindow"] == "console" then
- local x, y, num, numStart, gap = gui[a]["console"]["TLX"], gui[a]["console"]["BRY"], table.maxn(logTable), table.maxn(logTable), gui[a]["console"]["BRY"] - gui[a]["console"]["TLY"]
- while y > gui[a]["console"]["TLY"] and num > (numStart - gap) do
- gui[a]["gpu"](functionNum, gui[a]["component"], "clearLine", {y})
- gui[a]["gpu"](functionNum, gui[a]["component"], "set", {x, y, togTable[num]["text"]})
- y, num = y - 1, num - 1
- end end end
- end
- -- else
- -- print(text)
- end
- else
- error(functionNum.." - Function Error, Expected: logger(functionNum, table, number, string)")
- end
- else
- error(functionNum.." - Function Error, Expected: logger(functionNum, table, number, string)")
- end
- if param_logLevel > 2 then error(text) end
- return result
- end
- -----------------------------------------------------------------------------------------------------------
- -- Old Functions --
- -----------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment