fatboychummy

KristShop Logger

May 31st, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.96 KB | None | 0 0
  1. --[[
  2. 2
  3. noRequire
  4. Made the logger open a single log file.  No more spammy names.
  5. Logger, designed for Krist Shops.  You may edit and reuse this to your heart's content.
  6. ]]
  7.  
  8. logVersion = 2
  9. local custom = false
  10.  
  11. local LOG_LOCATION = false
  12. local LOG_NAME = false
  13. local PLOG_LOCATION = false
  14. local PLOG_NAME = false
  15. local doInfoLogging = false
  16. local doWarnLogging = false
  17. canLogBeOpened = false
  18. canPurchaseLogBeOpened = false
  19. local fileHandle=false
  20. local pFileHandle = false
  21. if fs.exists("fatShopCustomization") then
  22.   custom = dofile("fatShopCustomization")
  23.  
  24.   custom = custom.LOGGER
  25.   LOG_LOCATION = custom.LOG_LOCATION
  26.   LOG_NAME = custom.LOG_NAME
  27.   PLOG_LOCATION = custom.PURCHASE_LOG_LOCATION
  28.   PLOG_NAME = custom.PURCHASE_LOG_NAME
  29.   canLogBeOpened = custom.doNormalLogging
  30.   canPurchaseLogBeOpened = custom.doPurchaseLogging
  31.   doInfoLogging = custom.doInfoLogging
  32.   doWarnLogging = custom.doWarnLogging
  33.  
  34.  
  35.   function openLog()
  36.     local logName = LOG_LOCATION..LOG_NAME
  37.     if not fs.exists(LOG_LOCATION) then
  38.       fs.makeDir(LOG_LOCATION)
  39.     end
  40.     fileHandle = fs.open(logName,"a")
  41.     if fileHandle then
  42.       info("File is opened for logging")
  43.     else
  44.       severe("Could not open a file for logging.")
  45.     end
  46.  
  47.   end
  48.   function openPurchaseLog()
  49.     local logName = PLOG_LOCATION..PLOG_NAME
  50.     if not fs.isDir(PLOG_LOCATION) then
  51.       fs.makeDir(PLOG_LOCATION)
  52.     end
  53.     if fs.exists(logName) then
  54.       pFileHandle = fs.open(logName,"a")
  55.     else
  56.       pFileHandle = fs.open(logName,"w")
  57.     end
  58.     if pFileHandle then
  59.       info("File is opened for purchase logging")
  60.       fileHandle.writeLine("-----[START LOG]-----")
  61.       fileHandle.flush()
  62.     else
  63.       severe("Could not open a file for purchase logging.")
  64.     end
  65.   end
  66.   function closeLog()
  67.     if fileHandle then
  68.       fileHandle.flush()
  69.       fileHandle.close()
  70.       info("Log has been closed")
  71.     else
  72.       warn("Cannot close a log if it is not opened.")
  73.     end
  74.  
  75.   end
  76.   function closePurchaseLog()
  77.     if pFileHandle then
  78.       pFileHandle.flush()
  79.       pFileHandle.close()
  80.       info("Purchase log has been closed.")
  81.     else
  82.       warn("Cannot close purchase log if it is not opened.")
  83.     end
  84.   end
  85. end
  86.  
  87. function isUpdate()
  88.   local handle = http.get("https://raw.githubusercontent.com/fatboychummy/Simplify-Shop/master/Logger.lua")
  89.   handle.readLine()
  90.   local v = tonumber(handle.readLine())
  91.   handle.close()
  92.   if v < logVersion then
  93.     print("LOGGER:",logVersion,">",v)
  94.   elseif v > logVersion then
  95.     print("LOGGER:",logVersion,"<",v)
  96.   else
  97.     print("LOGGER:",logVersion,"=",v)
  98.   end
  99.   return v > logVersion
  100. end
  101. function update()
  102.   print("An update to the logger is available.")
  103.   local h1 = http.get("https://raw.githubusercontent.com/fatboychummy/Simplify-Shop/master/Logger.lua")
  104.   h1.readLine()
  105.   local v = tonumber(h1.readLine())
  106.   local required = h1.readLine()
  107.   local notes = h1.readLine()
  108.   h1.close()
  109.   local doUpdate = false
  110.   if required == "REQUIRED" then
  111.     doUpdate = true
  112.     print("Update is a required update.  Updating in 5 seconds.")
  113.     print("-----------------")
  114.     print(notes)
  115.     print("-----------------")
  116.     os.sleep(5)
  117.   else
  118.     print("Update is not a required update.")
  119.     print("-----------------")
  120.     print(notes)
  121.     print("-----------------")
  122.     print("Would you like to update now? (y/n)")
  123.     local utm = os.startTimer(30)
  124.     while true do
  125.       local a = {os.pullEvent()}
  126.       if a[1] == "char" then
  127.         if a[2] == "y" then
  128.           doUpdate = true
  129.           break
  130.         elseif a[2] == "n" then
  131.           break
  132.         end
  133.       elseif a[1] == "timer" and a[2] == utm then
  134.         break
  135.       end
  136.     end
  137.     if not doUpdate then
  138.       print("Timed out or skipping update.")
  139.       return false
  140.     end
  141.   end
  142.  
  143.   if doUpdate then
  144.     local handle = http.get("https://raw.githubusercontent.com/fatboychummy/Simplify-Shop/master/Logger.lua")
  145.     fs.delete("logger.lua")
  146.     local h2 = fs.open("logger.lua","w")
  147.     h2.write(handle.readAll())
  148.     handle.close()
  149.     h2.close()
  150.     return true
  151.   end
  152.   return false
  153. end
  154.  
  155.  
  156. function purchase(a)
  157.   if term.isColor and term.isColor() then
  158.     local oldC = term.getTextColor()
  159.     term.write("[")
  160.     term.setTextColor(colors.green)
  161.     term.write("PURCHASE")
  162.     term.setTextColor(oldC)
  163.     term.write("]: ")
  164.   else
  165.     term.write("[PURCHASE]: ")
  166.   end
  167.   print(a)
  168.   if pFileHandle then
  169.     pFileHandle.writeLine(a)
  170.     pFileHandle.flush()
  171.   end
  172. end
  173. function purchaseLog(item,amount,price,addplay,tf)
  174.   local function err(nm,exp,tp)
  175.     return "purchaseLog Bad argument #"..tostring(nm)..": expected "..exp..", got "..tp.."."
  176.   end
  177.   assert(type(item) == "string",err(1,"string",type(item)))
  178.   assert(type(amount) == "number",err(2,"number",type(amount)))
  179.   assert(type(price) == "number",err(3,"number",type(price)))
  180.   assert(type(addplay) == "string",err(4,"string",type(addplay)))
  181.   assert(type(tf) == "boolean" ,err(5,"boolean",type(tf)))
  182.   if term.isColor and term.isColor() then
  183.     local oldC = term.getTextColor()
  184.     term.write("[")
  185.     term.setTextColor(colors.green)
  186.     term.write("PURCHASE")
  187.     term.setTextColor(oldC)
  188.     term.write("]: ")
  189.   else
  190.     term.write("[PURCHASE]: ")
  191.   end
  192.   print(tf and "player "..addplay.." bought "..item.."["..tostring(amount).."] for "..tostring(price).."."  or "address "..addplay.." bought "..item.."["..tostring(amount).."] for "..tostring(price)..".")
  193.   if pFileHandle then
  194.     pFileHandle.writeLine(tf and "--[PURCHASE]: player "..addplay.." bought "..item.."["..tostring(amount).."] for "..tostring(price).."."  or "--[PURCHASE]: address "..addplay.." bought "..item.."["..tostring(amount).."] for "..tostring(price)..".")
  195.     pFileHandle.flush()
  196.   end
  197. end
  198. function info(notif)
  199.     print(notif and "[INFO]: "..notif or "[INFO]: ?")
  200.     if doInfoLogging and fileHandle then
  201.       fileHandle.writeLine(notif and "[INFO]: "..notif or "[INFO]: ?")
  202.       fileHandle.flush()
  203.     end
  204. end
  205. function ree()
  206.     print("[REE]: REEEEEEEEEEEEEEEEEEEEEEEEE")
  207. end
  208. function warn(notif)
  209.     if term.isColor and term.isColor() then
  210.       local oldC = term.getTextColour()
  211.       term.write("[")
  212.       term.setTextColor(colors.yellow)
  213.       term.write("WARN")
  214.       term.setTextColor(oldC)
  215.     else
  216.       term.write("[WARN")
  217.     end
  218.     print(notif and "]: "..notif or "]: ?")
  219.     if doWarnLogging and fileHandle then
  220.       fileHandle.writeLine(notif and "[WARN]: "..notif or "[WARN]: ?")
  221.       fileHandle.flush()
  222.     end
  223. end
  224. function severe(notif)
  225.     if term.isColor and term.isColor() then
  226.       local oldBC = term.getTextColor()
  227.       term.write("[")
  228.       term.setTextColor(colors.red)
  229.       term.write("SEVERE")
  230.       term.setTextColor(oldBC)
  231.     else
  232.       term.write("[SEVERE")
  233.     end
  234.     print(notif and "]: "..notif or "]: ?")
  235.     if fileHandle then
  236.       fileHandle.writeLine(notif and "[SEVERE]: "..notif or "[SEVERE]: ?")
  237.       fileHandle.flush()
  238.     end
  239. end
Add Comment
Please, Sign In to add comment