Refactor logging in inventoryClient to use structured logging for improved clarity and consistency

This commit is contained in:
MayaTheShy
2026-03-22 02:06:02 -04:00
parent 304e779fd0
commit 0f9b7fcf68

View File

@@ -33,7 +33,7 @@ local function loadConfig()
f.close() f.close()
local ok, cfg = pcall(textutils.unserialiseJSON, data) local ok, cfg = pcall(textutils.unserialiseJSON, data)
if not ok or not cfg then if not ok or not cfg then
print("[WARN] Failed to parse " .. CLIENT_CONFIG_FILE) log.warn("CONFIG", "Failed to parse %s", CLIENT_CONFIG_FILE)
return return
end end
-- Channels -- Channels
@@ -47,7 +47,8 @@ local function loadConfig()
if cfg.barrelName then CLIENT_BARREL_NAME = cfg.barrelName end if cfg.barrelName then CLIENT_BARREL_NAME = cfg.barrelName end
-- Timing -- Timing
if cfg.dropperAnnounceInterval then DROPPER_ANNOUNCE_INTERVAL = cfg.dropperAnnounceInterval end if cfg.dropperAnnounceInterval then DROPPER_ANNOUNCE_INTERVAL = cfg.dropperAnnounceInterval end
print("[CONFIG] Loaded from " .. CLIENT_CONFIG_FILE) if cfg.logLevel then log.setLevel(cfg.logLevel) end
log.info("CONFIG", "Loaded from %s", CLIENT_CONFIG_FILE)
end end
loadConfig() loadConfig()
@@ -93,6 +94,7 @@ local connected = false -- true once first state received
------------------------------------------------- -------------------------------------------------
local ui = dofile("lib/ui.lua") local ui = dofile("lib/ui.lua")
local log = dofile("lib/log.lua")
------------------------------------------------- -------------------------------------------------
-- UI State (local to client) -- UI State (local to client)
@@ -1064,7 +1066,7 @@ local function handleTouch(x, y)
if action == "amount" then if action == "amount" then
selectedAmount = data selectedAmount = data
print("[UI] Amount set to " .. data) log.debug("UI", "Amount set to %s", data)
needsRedraw = true needsRedraw = true
elseif action == "order" then elseif action == "order" then
@@ -1082,7 +1084,7 @@ local function handleTouch(x, y)
amount = selectedAmount, amount = selectedAmount,
dropperName = CLIENT_DROPPER_NAME ~= "" and CLIENT_DROPPER_NAME or nil, dropperName = CLIENT_DROPPER_NAME ~= "" and CLIENT_DROPPER_NAME or nil,
}) })
print(string.format("[ORDER] Sent to master: %s x%d", itemName, selectedAmount)) log.info("ORDER", "Sent to master: %s x%d", itemName, selectedAmount)
end end
elseif action == "scan" then elseif action == "scan" then
@@ -1091,7 +1093,7 @@ local function handleTouch(x, y)
statusTimer = 3 statusTimer = 3
needsRedraw = true needsRedraw = true
sendToMaster({ type = "scan" }) sendToMaster({ type = "scan" })
print("[UI] Scan request sent to master") log.debug("UI", "Scan request sent to master")
elseif action == "kb_toggle" then elseif action == "kb_toggle" then
showKeyboard = not showKeyboard showKeyboard = not showKeyboard
@@ -1156,22 +1158,22 @@ local function handleSmelterTouch(x, y)
elseif action == "toggle_pause" then elseif action == "toggle_pause" then
sendToMaster({ type = "toggle_pause" }) sendToMaster({ type = "toggle_pause" })
print("[SMELT-UI] Toggle pause sent to master") log.debug("UI", "Toggle pause sent to master")
smelterNeedsRedraw = true smelterNeedsRedraw = true
elseif action == "toggle_recipe" then elseif action == "toggle_recipe" then
sendToMaster({ type = "toggle_recipe", recipe = data }) sendToMaster({ type = "toggle_recipe", recipe = data })
print("[SMELT-UI] Toggle recipe sent: " .. data) log.debug("UI", "Toggle recipe sent: %s", data)
smelterNeedsRedraw = true smelterNeedsRedraw = true
elseif action == "enable_all" then elseif action == "enable_all" then
sendToMaster({ type = "enable_all" }) sendToMaster({ type = "enable_all" })
print("[SMELT-UI] Enable all sent to master") log.debug("UI", "Enable all sent to master")
smelterNeedsRedraw = true smelterNeedsRedraw = true
elseif action == "disable_all" then elseif action == "disable_all" then
sendToMaster({ type = "disable_all" }) sendToMaster({ type = "disable_all" })
print("[SMELT-UI] Disable all sent to master") log.debug("UI", "Disable all sent to master")
smelterNeedsRedraw = true smelterNeedsRedraw = true
elseif action == "page_prev" then elseif action == "page_prev" then
@@ -1191,7 +1193,7 @@ local function handleSmelterTouch(x, y)
local recipe = CRAFTABLE[data] local recipe = CRAFTABLE[data]
if recipe then if recipe then
local short = recipe.output:gsub("^minecraft:", ""):gsub("_", " ") local short = recipe.output:gsub("^minecraft:", ""):gsub("_", " ")
print("[CRAFT-UI] Craft request sent: " .. short) log.info("CRAFT", "Craft request sent: %s", short)
end end
smelterNeedsRedraw = true smelterNeedsRedraw = true
end end
@@ -1208,15 +1210,15 @@ local function main()
print("") print("")
if setupMonitor() then if setupMonitor() then
print("[OK] Monitor: " .. (monName or MONITOR_SIDE)) log.info("INIT", "Monitor: %s", monName or MONITOR_SIDE)
else else
print("[WARN] No monitor on " .. MONITOR_SIDE) log.warn("INIT", "No monitor on %s", MONITOR_SIDE)
end end
if setupSmelterMonitor() then if setupSmelterMonitor() then
print("[OK] Smelter monitor: " .. (smelterMonName or SMELTER_MONITOR_SIDE)) log.info("INIT", "Smelter monitor: %s", smelterMonName or SMELTER_MONITOR_SIDE)
else else
print("[WARN] No smelter monitor on " .. SMELTER_MONITOR_SIDE) log.warn("INIT", "No smelter monitor on %s", SMELTER_MONITOR_SIDE)
end end
-- Find modem -- Find modem
@@ -1225,12 +1227,12 @@ local function main()
networkModem = peripheral.wrap(name) networkModem = peripheral.wrap(name)
networkModem.open(BROADCAST_CHANNEL) networkModem.open(BROADCAST_CHANNEL)
networkModem.open(CLIENT_CHANNEL) networkModem.open(CLIENT_CHANNEL)
print("[OK] Modem: " .. name) log.info("INIT", "Modem: %s", name)
break break
end end
end end
if not networkModem then if not networkModem then
print("[ERR] No modem found! Cannot receive data from master.") log.error("INIT", "No modem found! Cannot receive data from master.")
print(" Attach a wired modem and restart.") print(" Attach a wired modem and restart.")
return return
end end
@@ -1288,7 +1290,7 @@ local function main()
if not connected then if not connected then
connected = true connected = true
print("[OK] Connected to master!") log.info("NET", "Connected to master!")
end end
needsRedraw = true needsRedraw = true
@@ -1301,9 +1303,9 @@ local function main()
statusTimer = 5 statusTimer = 5
needsRedraw = true needsRedraw = true
if message.success then if message.success then
print("[OK] " .. statusMessage) log.info("ORDER", "%s", statusMessage)
else else
print("[WARN] " .. statusMessage) log.warn("ORDER", "%s", statusMessage)
end end
elseif channel == CLIENT_CHANNEL and type(message) == "table" and message.type == "craft_result" then elseif channel == CLIENT_CHANNEL and type(message) == "table" and message.type == "craft_result" then
@@ -1313,9 +1315,9 @@ local function main()
statusTimer = 5 statusTimer = 5
smelterNeedsRedraw = true smelterNeedsRedraw = true
if message.success then if message.success then
print("[OK] " .. statusMessage) log.info("CRAFT", "%s", statusMessage)
else else
print("[WARN] " .. statusMessage) log.warn("CRAFT", "%s", statusMessage)
end end
end end
end end
@@ -1369,7 +1371,7 @@ local function main()
-- Task 5: Client barrel auto-sort (sends to master only when barrel has items) -- Task 5: Client barrel auto-sort (sends to master only when barrel has items)
function() function()
if CLIENT_BARREL_NAME == "" then return end if CLIENT_BARREL_NAME == "" then return end
print("[OK] Client barrel: " .. CLIENT_BARREL_NAME) log.info("INIT", "Client barrel: %s", CLIENT_BARREL_NAME)
while true do while true do
local barrel = peripheral.wrap(CLIENT_BARREL_NAME) local barrel = peripheral.wrap(CLIENT_BARREL_NAME)
if barrel then if barrel then
@@ -1387,10 +1389,10 @@ local function main()
while true do while true do
local event, side, x, y = os.pullEvent("monitor_touch") local event, side, x, y = os.pullEvent("monitor_touch")
if smelterMonName and side == smelterMonName then if smelterMonName and side == smelterMonName then
print(string.format("[SMELT-TOUCH] x=%d y=%d", x, y)) log.debug("TOUCH", "x=%d y=%d", x, y)
handleSmelterTouch(x, y) handleSmelterTouch(x, y)
else else
print(string.format("[TOUCH] x=%d y=%d", x, y)) log.debug("TOUCH", "x=%d y=%d", x, y)
handleTouch(x, y) handleTouch(x, y)
end end
end end