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