2020-12-28 14:45:50 +01:00
|
|
|
-- Created by Jef Roosens
|
|
|
|
--
|
|
|
|
-- Module to add logging to a program. This module is integrated in all jlib
|
|
|
|
-- functions.
|
|
|
|
|
|
|
|
|
|
|
|
local module = {}
|
|
|
|
|
|
|
|
|
|
|
|
-- Define class with default values
|
|
|
|
local Logger = {
|
|
|
|
level = 2,
|
|
|
|
level_colors = {
|
|
|
|
colors.red, colors.orange, colors.yellow, colors.blue, colors.white},
|
|
|
|
prefixes = { "CRIT", "ERROR", "WARN", "INFO", "DEBUG" },
|
|
|
|
filename = nil
|
|
|
|
}
|
|
|
|
Logger.__index = Logger
|
|
|
|
|
|
|
|
|
|
|
|
-- Return a new logger 'object'.
|
|
|
|
--
|
|
|
|
-- @param level log level:
|
|
|
|
-- (0) critical, (1) errors, (2) warnings, (3) info,(4) debug
|
|
|
|
-- @param level_colors table containing color names for messages; order is
|
|
|
|
-- { critical, errors, warnings, info, debug }. Only colors for the needed log
|
|
|
|
-- level need to be provided: e.g. if the log level is (2) warnings, then only
|
|
|
|
-- two colors need to be provided. If not provided, defaults to
|
|
|
|
-- { red, orange, yellow, blue, white }.
|
|
|
|
-- @param prefixes prefix to show for each log level
|
|
|
|
-- @param filename log file to write to. If not supplied, log will only be
|
|
|
|
-- outputted to stdout
|
|
|
|
-- @return a logger 'object'
|
|
|
|
function Logger:new(o)
|
|
|
|
o = o or {}
|
|
|
|
setmetatable(o, self)
|
|
|
|
-- Fill in default values where needed
|
|
|
|
-- TODO find out if this is needed or not
|
|
|
|
-- for k, v in pairs(Logger) do
|
|
|
|
-- o[k] = o[k] or Logger[k]
|
|
|
|
-- end
|
|
|
|
|
|
|
|
-- Open file handler
|
|
|
|
if o.filename then
|
|
|
|
o._file = open(filename, "a")
|
|
|
|
end
|
|
|
|
|
|
|
|
return o
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Log a message with the given level
|
|
|
|
--
|
|
|
|
-- @param level log level; levels higher than logger level get discarded
|
|
|
|
-- @param message message to log
|
|
|
|
-- @return wether or not the message was actually logged
|
|
|
|
function Logger:log(level, message)
|
|
|
|
-- Exit if the level is too high
|
|
|
|
if level > self.level then return false end
|
|
|
|
|
|
|
|
-- Log to screen
|
|
|
|
local cur_color = term.getTextColor()
|
|
|
|
term.setTextColor(self.colors[level + 1])
|
|
|
|
term.write("[" .. self.prefixes[level + 1] .. "] ")
|
|
|
|
term.setTextColor(cur_color)
|
|
|
|
print(message)
|
|
|
|
|
|
|
|
-- Log to file
|
|
|
|
if self._file then
|
|
|
|
local line = "[" .. self.prefixes[level + 1] .. "] " .. message .. '\n'
|
|
|
|
self._file.write(line)
|
|
|
|
self._file.flush()
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Just some convenience functions; wrappers around Logger:log
|
2020-12-28 15:44:58 +01:00
|
|
|
function Logger:critical(message) self:log(0, message) end
|
|
|
|
function Logger:error(message) self:log(1, message) end
|
|
|
|
function Logger:warning(message) self:log(2, message) end
|
|
|
|
function Logger:info(message) self:log(3, message) end
|
|
|
|
function Logger:debug(message) self:log(4, message) end
|
2020-12-28 14:45:50 +01:00
|
|
|
|
|
|
|
-- Close the logger a.k.a. close the log file handler
|
|
|
|
function Logger:close()
|
|
|
|
if self._file then self._file.close() end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
module.Logger = Logger
|
|
|
|
return module
|