diff --git a/itemcontroller.lua b/itemcontroller.lua index ac47199..0c5f02c 100644 --- a/itemcontroller.lua +++ b/itemcontroller.lua @@ -1,13 +1,12 @@ local Logging = require("logging") +local LogLevel = Logging.LogLevel + local CACHE_FILE = "/.storage.cache" local LOCK_FILE = "/.storage.lock" local LOG_FILE_PATH = "latest.log" local Logger = Logging.firstLoad( - Logging.LogLevel.TRACE, - Logging.OUTPUTS.combine( - Logging.OUTPUTS.file(LOG_FILE_PATH) - --Logging.OUTPUTS.stdout - ) + Logging.OUTPUTS.file(LOG_FILE_PATH), + Logging.OUTPUTS.stdout(LogLevel.CRITICAL) ) local Chest = require("storage.chest") diff --git a/logging.lua b/logging.lua index df0b22d..d46d40d 100644 --- a/logging.lua +++ b/logging.lua @@ -9,6 +9,8 @@ local LogLevel = { ERROR = 6 } +LogLevel._DEFAULT = LogLevel.TRACE + for k,v in pairs(LogLevel) do LogLevel[v] = k end @@ -30,18 +32,24 @@ local Logger = { } Logger.__index = Logger -function Logger:new(o) - local logger = {} - local output = print - if type(o) == "table" then - if type(o.level) == "number" and LogLevel.isValid(o.level) then - logger.level = o.level - end - if type(o.output) == "function" then - output = o.output + +local function combineOutputs(...) + local args = {...} + if #args == 0 then + return function() end + elseif #args == 1 then + return args[1] + end + return function(text, level) + for _,v in ipairs(args) do + v(text, level) end end - logger.output = output +end + +-- TODO: Split logger outputs and specify LogLevel for each +function Logger:new(...) + local logger = { output = combineOutputs(...) or function() end } setmetatable(logger, self) logger.__index = logger @@ -216,13 +224,11 @@ local function deepStringify(value) end function Logger:_doPrint(level, message, ...) - if LogLevel.isGreaterOrEqual(level, self.level) then - local result = { tostring(LogLevel[level]), deepStringify(message) } - for _,v in ipairs({...}) do - table.insert(result, deepStringify(v)) - end - self.output(table.concat(result, " ")) + local result = { tostring(LogLevel[level]), deepStringify(message) } + for _,v in ipairs({...}) do + table.insert(result, deepStringify(v)) end + self.output(table.concat(result, " "), level) end function Logger:trace(message, ...) @@ -249,14 +255,8 @@ function Logger:error(message, ...) self:_doPrint(LogLevel.ERROR, message, ...) end -function Logger:setOutput(output) - self.output = output -end - -function Logger:setLevel(level) - if LogLevel.isValid(level) then - self.level = LogLevel.asNumber(level) - end +function Logger:setOutput(...) + self.output = combineOutputs(...) end function Logger.plain(value, deep) @@ -268,28 +268,33 @@ Logging.LogLevel = LogLevel function Logging.getGlobalLogger() if _G._GLOBAL_LOGGER == nil then - _G._GLOBAL_LOGGER = Logger:new{ level = LogLevel.DEBUG, output = print } + _G._GLOBAL_LOGGER = Logger:new{ output = function() end } end return _G._GLOBAL_LOGGER end -function Logging.firstLoad(level, output) +function Logging.firstLoad(...) Logging.resetAll() local logger = Logging.getGlobalLogger() - logger:setOutput(output) - logger:setLevel(level) + logger:setOutput(...) return logger end Logging.OUTPUTS = {} -function Logging.OUTPUTS.file(name) +function Logging.OUTPUTS.file(name, logLevel) + logLevel = logLevel or LogLevel._DEFAULT + local doWrite = function(file, text) file.write(text) file.write("\n") file.flush() end local file = fs.open(name, "w+") - return function(text) + return function(text, level) + if level < logLevel then + return + end + if not pcall(doWrite, file, text) then local dRes, dRet = pcall(fs.delete, name) if not dRes then @@ -304,17 +309,20 @@ function Logging.OUTPUTS.file(name) end end end -Logging.OUTPUTS.stdout = print -function Logging.OUTPUTS.combine(...) - local args = {...} - if #args == 0 then - return function() end - end - return function(text) - for _,v in ipairs(args) do - v(text) +function Logging.OUTPUTS.transmit(modem, channel, logLevel) + logLevel = logLevel or LogLevel._DEFAULT +end + +function Logging.OUTPUTS.stdout(logLevel) + logLevel = logLevel or LogLevel._DEFAULT + + return function(text, level) + if level < logLevel then + return end + + print(text) end end