98 lines
No EOL
2.5 KiB
Lua
98 lines
No EOL
2.5 KiB
Lua
local LOG = {
|
|
target = term,
|
|
history = {},
|
|
history_cutoff = 50,
|
|
log_path = "./logs/",
|
|
debug = false
|
|
}
|
|
|
|
-- Set target's colorscheme to gruvbox for better visuals
|
|
function LOG:SetColours()
|
|
self.target.setPaletteColour(colours.red, 0xfb4934)
|
|
self.target.setPaletteColour(colours.blue, 0x83a598)
|
|
self.target.setPaletteColour(colours.green, 0xb8bb26)
|
|
self.target.setPaletteColour(colours.purple, 0xd3869b)
|
|
self.target.setPaletteColour(colours.cyan, 0x8ec07c)
|
|
self.target.setPaletteColour(colours.white, 0xf9f5d7)
|
|
self.target.setPaletteColour(colours.black, 0x1d2021)
|
|
end
|
|
|
|
-- Set target output for our log
|
|
function LOG:SetTarget(target)
|
|
self.target = target
|
|
target.clear()
|
|
target.setTextScale(0.5)
|
|
end
|
|
|
|
function LOG:SetDebug(value)
|
|
self.debug = value
|
|
end
|
|
|
|
-- Utility function to output some kind of log message
|
|
local function genericLog(log, level, message, col)
|
|
if #level ~= 4 or #col ~= 1 then
|
|
LOG:Error("Incorrect generic log format")
|
|
return
|
|
end
|
|
|
|
local target = log.target
|
|
target.scroll(1)
|
|
local _, y = target.getSize()
|
|
target.setCursorPos(1, y)
|
|
|
|
local time = os.date("%T", os.epoch("local") / 1000)
|
|
local output = "[" .. time .. "|" .. level .. "] " .. message
|
|
local fg_blit = ("0"):rep(#time + 2) .. (col):rep(4) .. ("0"):rep(#message + 2)
|
|
target.blit(output, fg_blit, ("f"):rep(#output))
|
|
|
|
table.insert(log.history, output)
|
|
if #log.history > log.history_cutoff then
|
|
table.remove(log.history, 1)
|
|
end
|
|
end
|
|
|
|
function LOG:Info(message)
|
|
genericLog(self, "INFO", message, "b")
|
|
end
|
|
|
|
function LOG:Success(message)
|
|
genericLog(self, "SCCS", message, "d")
|
|
end
|
|
|
|
function LOG:Debug(message)
|
|
if not self.debug then return end
|
|
genericLog(self, "DBUG", message, "8")
|
|
end
|
|
|
|
function LOG:Warning(message)
|
|
genericLog(self, "WARN", message, "4")
|
|
end
|
|
|
|
function LOG:Error(message)
|
|
genericLog(self, "ERRO", message, "e")
|
|
end
|
|
|
|
function LOG:Critical(message)
|
|
genericLog(self, "CRIT", message, "e")
|
|
self:Dump()
|
|
error("critical exception occured, read above")
|
|
end
|
|
|
|
function LOG:Clear()
|
|
self.history = {}
|
|
end
|
|
|
|
-- Put all recent log messages into a file for later study
|
|
function LOG:Dump()
|
|
local out = ""
|
|
for _, v in ipairs(self.history) do
|
|
out = out + v .. "\n"
|
|
end
|
|
local time = os.date("%F-%T", os.epoch("local") / 1000)
|
|
local file = fs.open(string.format("%s/dump_%s.log", self.log_path, time), "w")
|
|
file.write(out)
|
|
file.close()
|
|
return out
|
|
end
|
|
|
|
return LOG |