Savage_Me55iah

CC_API_logger

Jul 19th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.12 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au--
  2.  
  3. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  4.  
  5. --INSTRUCTIONS ON USE--
  6. --Instruction video on http://youtube.com/savagemessiah5
  7.  
  8. --Contact Savage_Me55iah @ CraftAU.com.au or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  9.  
  10. local scriptName = "logger"
  11. fileDetail = fileDetail or {}
  12. fileDetail[scriptName] = {["version"] = 0.1, ["pastebin"] = "9zBzHK0N"}
  13.  
  14. local tArgs = {...}
  15. if tArgs[1] == "update" then
  16.     print("UPDATING") shell.run("pastebin get "..fileDetail[scriptName].pastebin.." "..scriptName.."_update")
  17.     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
  18. end
  19.  
  20. -----------------------------------------------------------------------------------------------------------
  21. -- Variables --
  22. -----------------------------------------------------------------------------------------------------------
  23.  
  24. local datastore = "DataStore/"
  25. local currLogDir, prevLogDir = datastore.."currentLog/", datastore.."previousLogs/"
  26. local currLog = currLogDir.."LOG:"..os.day()--.."-"..os.time()
  27. local currLogTable = currLogDir.."LOGTABLE-"..os.day()--.."-"..os.time()
  28. local errorLog = "DataStore/Errors/Error"
  29. logTable = {}
  30.  
  31. -----------------------------------------------------------------------------------------------------------
  32. -- API Functions --
  33. -----------------------------------------------------------------------------------------------------------
  34.  
  35. -- fileList(functionNum, string) -- [1] returns true if function ran correctly OR if false variable was nil [2] returns variable OR false if nil
  36. -- fixes the fs.list() returning a function
  37. --function fileList(param_functionNum, param_location)
  38. --  local result, functionNum = {false, false}, param_functionNum.."/002"
  39. --  local tempFunc = nil
  40. --  if tostring(param_location) ~= "nil" then
  41. --      tempFunc = fs.list(tostring(param_location))
  42. --      local hold = tostring(tempFunc())
  43. --      result = {true, {}}
  44. --      while hold ~= "nil" do
  45. --          table.insert(result[2], hold)
  46. --          hold = tostring(tempFunc())
  47. --  end end
  48. --  return result
  49. --end
  50.  
  51. -- logger.logSetup(functionNum) -- [1] returns true if function ran correctly OR if false variable was nil
  52. -- runs at the beginning of a program, moves old logs to previous logs folder
  53. function logSetup(param_functionNum)
  54.     local result, functionNum = {false}, param_functionNum.."/002"
  55.     local tempTable = {}
  56.     if fs.exists(currLogDir) then
  57.         tempTable = fs.list(currLogDir) or false
  58.         if tempTable ~= false then
  59.             for a,b in pairs(tempTable) do
  60.                 if fs.copy(currLogDir..b, prevLogDir..b) then
  61.                     result[1] = true
  62.                     fs.delete(currLogDir..b)
  63.         end end end
  64.     end
  65.     if fs.exists(errorLog) then fs.delete(errorLog) end
  66.     return result
  67. end
  68.  
  69. -- logger.saveToFile(functionNum, string, 'a', string) -- [1] returns true if function ran correctly OR if false variable was nil
  70. -- shortcut for the open/write/close funcitons
  71. function saveToFile(param_functionNum, param_loc, param_mode, param_text)
  72.     local result, functionNum = {false, false}, param_functionNum.."/002"
  73.     local fh = fs.open(param_loc, param_mode) or false
  74. --  print(param_functionNum..param_loc..param_mode..param_text)
  75.     if tostring(fh) ~= "false" then
  76.         fh.write(tostring(param_text))
  77.         fh.close()
  78.     end
  79.     return result
  80. end
  81.  
  82. -- logger.logger(functionNum, {string, table, etc}, number, string) -- [1] returns true if function ran correctly OR if false variable was nil
  83. -- main function. assign, records and displays logged text, use separate program to overview the log table
  84. function logger(param_functionNum, param_text, param_logLevel)
  85.     local result, functionNum = {false, false}, param_functionNum.."/002"
  86.     local text, attachN, attachE, tempList, separators = nil, ">", "--", {}, param_separators or false
  87.    
  88.     if tostring(param_text) ~= nil then
  89.         if type(param_text) == "table" and tostring(param_text[1]) ~= "nil" then
  90.             for a,b in pairs(param_text) do
  91.                 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
  92.             end
  93.             for num=1,table.maxn(tempList) do
  94.                 if num == 1 then text = tempList[num] else if not separators then text = text..tempList[num] else text = text..separators..tempList[num] end end
  95.             end
  96.            
  97.             table.insert(logTable, {["text"] = text, ["logLevel"] = param_logLevel, ["time"] = os.time(), ["functionNum"] = param_functionNum})
  98.            
  99.             if param_logLevel > 0 then
  100.                 text = attachE..param_logLevel.." "..text.." @ "..param_functionNum
  101.                 saveToFile(functionNum, errorLog, 'a', text)
  102.             else text = attachN..param_logLevel..text end
  103.             saveToFile(functionNum, currLog, 'a', text)
  104.             saveToFile(functionNum, currLogTable, 'w', textutils.serialize(logTable))
  105.            
  106.             if tostring(gui) ~= "nil" then
  107.                 for a,b in pairs(gui) do
  108.                     if tostring(computerType) == "computerCraft" then
  109.  
  110.                     elseif tostring(computerType) == "openComputers" then
  111.                         if gui[a]["currWindow"] == "console" then
  112.                             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"]
  113.                             while y > gui[a]["console"]["TLY"] and num > (numStart - gap) do
  114.                                 gui[a]["gpu"](functionNum, gui[a]["component"], "clearLine", {y})
  115.                                 gui[a]["gpu"](functionNum, gui[a]["component"], "set", {x, y, togTable[num]["text"]})
  116.                                 y, num = y - 1, num - 1
  117.                     end end end
  118.                 end
  119.         --  else
  120.         --      print(text)
  121.             end
  122.         else
  123.             error(functionNum.." - Function Error, Expected: logger(functionNum, table, number, string)")
  124.         end
  125.     else
  126.         error(functionNum.." - Function Error, Expected: logger(functionNum, table, number, string)")
  127.     end
  128.     if param_logLevel > 2 then error(text) end
  129.     return result
  130. end
  131.  
  132.  
  133. -----------------------------------------------------------------------------------------------------------
  134. -- Old Functions --
  135. -----------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment