diff --git a/src/backend.lua b/src/backend.lua index a19e5ba..805f0e0 100644 --- a/src/backend.lua +++ b/src/backend.lua @@ -1,17 +1,22 @@ local kristly = require("/src/libs/kristly") local utils = require("/src/utils") -print("Starting kristify") +local loggerImport = require("/src/libs/logger") +local logger = loggerImport:new({ debugging = true }) + +logger:info("Starting Kristify! Thanks for choosing us.") +logger:debug("Debugging mode is enabled!") local config = require("/data/config") local products = require("/data/products") if config == nil or config.pkey == nil then - print("Config not found!") + logger:error("Config not found! Check documentation for more info.") return end +-- TODO Make autofix if utils.endsWith(config.name, ".kst") then - print("The krist name in config should not include `.kst`.") + logger:error("The krist name configured contains `.kst`, which it should not.") return end @@ -19,21 +24,21 @@ local function startListening() ws:subscribe("transactions") - print("Subscribed to transactions! :D") + logger:info("Subscribed to transactions.") while true do local _, data = os.pullEvent("kristly") if data.type == "keepalive" then - print("Keep alive packet") + logger:debug("Keepalive packet") elseif data.type == "event" then - print("Event: " .. data.event) + logger:debug("Event: " .. data.event) if data.event == "transaction" then local transaction = data.transaction if transaction.sent_name == config.name and transaction.sent_metaname ~= nil then - print("Transaction to: " .. transaction.sent_metaname .. "@" .. transaction.sent_name .. ".kst") + logger:info("Received transaction to: " .. transaction.sent_metaname .. "@" .. transaction.sent_name .. ".kst") handleTransaction(transaction) elseif transaction.sent_name == config.name then @@ -43,7 +48,7 @@ end else - print("Ignoring packet: " .. data.type) + logger:debug("Ignoring packet: " .. data.type) end end end @@ -71,7 +76,7 @@ "message=Here is your change! Thanks for using our shop.") end - print("Dispensing " .. amount .. " item(s).") + logger:info("Dispensing " .. amount .. " item(s).") end local function startKristly() diff --git a/src/libs/logger.lua b/src/libs/logger.lua new file mode 100644 index 0000000..389fe4c --- /dev/null +++ b/src/libs/logger.lua @@ -0,0 +1,71 @@ +local logger = {} + +function logger:new(o) + o = o or {} -- create object if user does not provide one + setmetatable(o, self) + self.__index = self + term.clear() + return o +end + +-- Inspiration by "Mads" +-- Inspiration source: http://www.computercraft.info/forums2/index.php?/topic/11771-print-coloured-text-easily/ +local function printWithFormat(...) + local s = "&1" + for k, v in ipairs(arg) do + s = s .. v + end + s = s .. "&0" + + local fields = {} + local lastcolor, lastpos = "0", 0 + for pos, clr in s:gmatch "()&(%x)" do + table.insert(fields, { s:sub(lastpos + 2, pos - 1), lastcolor }) + lastcolor, lastpos = clr, pos + end + + for i = 2, #fields do + term.setTextColor(2 ^ (tonumber(fields[i][2], 16))) + io.write(fields[i][1]) + end +end + +local function fixup() + print(" ") + term.setTextColor(colors.white) + term.setBackgroundColor(colors.black) +end + +function logger:info(...) + local args = { ... } + + printWithFormat("&8[&dINFO&8] &0", table.unpack(args)) + fixup() +end + +function logger:warn(...) + local args = { ... } + + printWithFormat("&8[&1WARN&8] &0", table.unpack(args)) + fixup() +end + +function logger:error(...) + local args = { ... } + + printWithFormat("&8[&eERROR&8] &e", table.unpack(args)) + fixup() +end + +function logger:debug(...) + if self.debugging == nil or self.debugging == false then + return + end + + local args = { ... } + + printWithFormat("&8[&7DEBUG&8] &0", table.unpack(args)) + fixup() +end + +return logger