diff --git a/.gitignore b/.gitignore index aa87397..4f1bd55 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,13 @@ # .settings .settings +# Cloud catcher +cloud.lua + +# Logs + +*.log + # Compiled Lua sources luac.out diff --git a/src/init.lua b/src/init.lua index 05ea9ef..4686efb 100644 --- a/src/init.lua +++ b/src/init.lua @@ -16,7 +16,8 @@ } -- Logger - ctx.logger = require("logger"):new({ debugging = settings.get("kristify.debug") }) + ctx.logger = require("logger"):new({ debugging = settings.get("kristify.debug"), + log2file = settings.get("kristify.log2file") }) -- [Pages] @@ -84,11 +85,20 @@ type = "boolean" }) + settings.define("kristify.log2file", { + description = "If kristify should log to a file", + default = false, + type = "boolean" + }) + -- Load scripts ctx.kristly = require(fs.combine("libs", "kristly")) ctx.utils = require("utils") ctx.webhooks = require("webhook") ctx.speakerLib = require("speaker") + + ctx.logger.debug("Loading in inv lib") + ctx.logger.debug("Configured storage: " .. textutils.serialize(ctx.config.storage)) ctx.storage = require(fs.combine("libs", "inv"))(ctx.config.storage or {}) return ctx @@ -96,7 +106,7 @@ -- INIT term.clear() -term.setCursorPos(1,1) +term.setCursorPos(1, 1) local args = table.pack(...) xpcall(function() init(table.unpack(args, 1, args.n)) @@ -124,4 +134,4 @@ end ) -ctx.logger:debug("Something exited (init.lua)") \ No newline at end of file +ctx.logger:debug("Something exited (init.lua)") diff --git a/src/logger.lua b/src/logger.lua index 7648210..c2bb3a4 100644 --- a/src/logger.lua +++ b/src/logger.lua @@ -5,6 +5,11 @@ ---@return table function logger:new(o) o = o or {} -- create object if user does not provide one + if o.log2file ~= nil and o.log2file == true then + print("Logging to file is enabled.") + o.f = fs.open("/kristify-latest.log", "w") + end + setmetatable(o, self) self.__index = self term.clear() @@ -32,18 +37,36 @@ ---Print to the terminal with log level `info` ---@param ... any function logger:info(...) + + if self.log2file == true then + self.f.write("\n" .. textutils.serialize(arg)) + self.f.flush() + end + colorPrint(colors.lightGray, "[", colors.green, "INFO", colors.lightGray, "] ", colors.white, table.unpack(arg)) end ---Print to the terminal with log level `warn` ---@param ... any function logger:warn(...) + + if self.log2file == true then + self.f.write("\n" .. textutils.serialize(arg)) + self.f.flush() + end + colorPrint(colors.lightGray, "[", colors.orange, "WARN", colors.lightGray, "] ", colors.white, table.unpack(arg)) end ---Print to the terminal with log level `error` ---@param ... any function logger:error(...) + + if self.log2file == true then + self.f.write("\n" .. textutils.serialize(arg)) + self.f.flush() + end + colorPrint(colors.lightGray, "[", colors.red, "ERROR", colors.lightGray, "] ", colors.red, table.unpack(arg)) end @@ -51,6 +74,12 @@ ---@param ... any function logger:debug(...) if self.debugging ~= true then return end + + if self.log2file == true then + self.f.write("\n" .. textutils.serialize(arg)) + self.f.flush() + end + colorPrint(colors.lightGray, "[", colors.gray, "DEBUG", colors.lightGray, "] ", colors.gray, table.unpack(arg)) end