Advertisement
AlexCatze2005

sysutils.lua

Feb 25th, 2021
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. local io = require("io")
  2. local filesystem = require("filesystem")
  3. local serialization = require("serialization")
  4. local color = require("rainbow")
  5. local component = require("component")
  6. local buf = color.buffer()
  7.  
  8. local racoon = {}
  9. racoon.init = 0
  10. racoon.info = 1
  11. racoon.warning = 2
  12. racoon.error = 3
  13. racoon.critical = 4
  14. racoon.starttime = os.date("%x_%X"):gsub("[/:]", ".")
  15. racoon.logmode = "both"
  16.  
  17. function racoon.log(text, mtype, progname)
  18.   if mtype == 4 then
  19.     racoon.lograw("["..os.date("%x %X").."] [CRITICAL]: "..text, progname)
  20.   elseif mtype == 3 then
  21.     racoon.lograw("["..os.date("%x %X").."] [ERROR]: "..text, progname)
  22.   elseif mtype == 2 then
  23.     racoon.lograw("["..os.date("%x %X").."] [WARNING]: "..text, progname)
  24.   elseif mtype == 0 then
  25.     racoon.lograw("["..os.date("%x %X").."] [INIT]: "..text, progname)
  26.   else
  27.     racoon.lograw("["..os.date("%x %X").."] [INFO]: "..text, progname)
  28.   end
  29. end
  30.  
  31. function racoon.logtofile(text, progname)
  32.  if progname == nil then progname = "" end
  33.   if not filesystem.exists("/etc/log/") then filesystem.makeDirectory("/etc/log/") end
  34.   logfile = io.open("/etc/log/"..progname:gsub("[^%a%d_]*", "").."_"..racoon.starttime..".log","a")
  35.   logfile:write(text.."\n")
  36.   logfile:close()
  37. end
  38.  
  39. function racoon.logtoscreen(text)
  40. if text:find("CRITICAL") then
  41.   print(buf(buf.bg_red(buf.fg_black(text)), buf.bg_black(buf.fg_white(""))))
  42. elseif text:find("ERROR") then
  43.   print(buf(buf.fg_red(text), buf.bg_black(buf.fg_white(""))))
  44. elseif text:find("WARNING") then
  45.   print(buf(buf.fg_yellow(text), buf.bg_black(buf.fg_white(""))))
  46. elseif text:find("INIT") then
  47.   print(buf(buf.fg_green(text), buf.bg_black(buf.fg_white(""))))
  48. else
  49.   print(buf(buf.fg_white(text), buf.bg_black(buf.fg_white(""))))
  50. end
  51. end
  52.  
  53. function racoon.lograw(text, progname)
  54.   if racoon.logmode == "both" then
  55.     racoon.logtoscreen(text)
  56.     racoon.logtofile(text, progname)
  57.   elseif racoon.logmode == "print" then
  58.     racoon.logtoscreen(text)
  59.   else
  60.     racoon.logtofile(text, progname)
  61.   end
  62. end
  63.  
  64. function racoon.writeconfig(progname, config)
  65.   if progname == nil then return false end
  66.   if not filesystem.exists("/etc/config/") then filesystem.makeDirectory("/etc/config/") end
  67.   local configfile = io.open("/etc/config/"..progname:gsub("[^%a%d_]*", "")..".cfg","w")
  68.   configfile:write(serialization.serialize(config))
  69.   configfile:close()
  70.   return true
  71. end
  72.  
  73. function racoon.readconfig(progname)
  74.   if progname == nil then return nil end
  75.   local configfile = io.open("/etc/config/"..progname:gsub("[^%a%d_]*", "")..".cfg","r")
  76.   if not configfile then configfile = io.open("/etc/config/"..progname..".cfg","w") end
  77.   local file = configfile:read()
  78.   if not file then file = "{}" end
  79.   local config = serialization.unserialize(file)
  80.   configfile:close()
  81.   return config
  82. end
  83.  
  84. function racoon.readlang(progname)
  85.   if progname == nil then return nil end
  86.   local lang = sysconfig.lang
  87.   if not filesystem.exists("/etc/lang/") then filesystem.makeDirectory("/etc/lang/") end
  88.   local langfile = io.open("/etc/lang/"..lang.."."..progname:gsub("[^%a%d_]*", "")..".lang","r")
  89.   if not langfile then return nil end
  90.   local lang = serialization.unserialize(langfile:read())
  91.   if not lang then return nil end
  92.   langfile:close()
  93.   return lang
  94. end
  95.  
  96. function racoon.gettheme()
  97.   local themefile
  98.   if not filesystem.exists("/etc/themes/") then filesystem.makeDirectory("/etc/themes/") end
  99.   if component.gpu.maxDepth() > 1 then
  100.     themefile = io.open("/etc/themes/"..sysconfig.theme..".thm","r")
  101.   else
  102.     themefile = io.open("/etc/themes/monochrome.thm","r")
  103.   end
  104.   if not themefile then return nil end
  105.   local theme = serialization.unserialize(themefile:read())
  106.   if not theme then return nil end
  107.   themefile:close()
  108.   return theme
  109. end
  110. return racoon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement