Refactor main function to replace print statements with structured logging for improved clarity and debugging

This commit is contained in:
MayaTheShy
2026-03-22 02:02:58 -04:00
parent e61a13961a
commit 33cad06155

View File

@@ -2486,27 +2486,27 @@ local function main()
print("") print("")
if peripheral.isPresent(DROPPER_NAME) then if peripheral.isPresent(DROPPER_NAME) then
print("[OK] Dropper: " .. DROPPER_NAME) log.info("INIT", "Dropper: %s", DROPPER_NAME)
else else
print("[WARN] Dropper not found: " .. DROPPER_NAME) log.warn("INIT", "Dropper not found: %s", DROPPER_NAME)
end end
if peripheral.isPresent(BARREL_NAME) then if peripheral.isPresent(BARREL_NAME) then
print("[OK] Barrel: " .. BARREL_NAME) log.info("INIT", "Barrel: %s", BARREL_NAME)
else else
print("[WARN] Barrel not found: " .. BARREL_NAME) log.warn("INIT", "Barrel not found: %s", BARREL_NAME)
end end
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 for client communication -- Find modem for client communication
@@ -2520,9 +2520,9 @@ local function main()
end end
end end
if networkModem then if networkModem then
print("[OK] Network modem: " .. networkModemName) log.info("INIT", "Network modem: %s", networkModemName)
else else
print("[WARN] No modem found for client sync") log.warn("INIT", "No modem found for client sync")
end end
-- Detect crafting turtle on network -- Detect crafting turtle on network
@@ -2533,15 +2533,15 @@ local function main()
end end
end end
if craftTurtleName then if craftTurtleName then
print("[OK] Crafting turtle: " .. craftTurtleName) log.info("INIT", "Crafting turtle: %s", craftTurtleName)
else else
print("[WARN] No crafting turtle found") log.warn("INIT", "No crafting turtle found")
end end
-- Load recipe toggles from disk -- Load recipe toggles from disk
loadDisabledRecipes() loadDisabledRecipes()
if smeltingPaused then if smeltingPaused then
print("[INIT] Smelting is PAUSED (toggle on smelter monitor)") log.info("INIT", "Smelting is PAUSED (toggle on smelter monitor)")
end end
local enabledCount = 0 local enabledCount = 0
local totalRecipeCount = 0 local totalRecipeCount = 0
@@ -2549,21 +2549,21 @@ local function main()
for k in pairs(SMELTABLE) do for k in pairs(SMELTABLE) do
if not disabledRecipes[k] then enabledCount = enabledCount + 1 end if not disabledRecipes[k] then enabledCount = enabledCount + 1 end
end end
print(string.format("[INIT] %d/%d recipes enabled", enabledCount, totalRecipeCount)) log.info("INIT", "%d/%d recipes enabled", enabledCount, totalRecipeCount)
-- Detect compost peripherals -- Detect compost peripherals
if peripheral.isPresent(COMPOST_DROPPER) then if peripheral.isPresent(COMPOST_DROPPER) then
print("[OK] Compost dropper: " .. COMPOST_DROPPER) log.info("INIT", "Compost dropper: %s", COMPOST_DROPPER)
else else
print("[WARN] Compost dropper not found: " .. COMPOST_DROPPER) log.warn("INIT", "Compost dropper not found: %s", COMPOST_DROPPER)
end end
if peripheral.isPresent(COMPOST_HOPPER) then if peripheral.isPresent(COMPOST_HOPPER) then
print("[OK] Compost hopper: " .. COMPOST_HOPPER) log.info("INIT", "Compost hopper: %s", COMPOST_HOPPER)
else else
print("[WARN] Compost hopper not found: " .. COMPOST_HOPPER) log.warn("INIT", "Compost hopper not found: %s", COMPOST_HOPPER)
end end
print(string.format("[INIT] Tracking %d low-stock alerts", #LOW_STOCK_ALERTS)) log.info("INIT", "Tracking %d low-stock alerts", #LOW_STOCK_ALERTS)
print("") print("")
print("Console shows log. Use the monitors to interact.") print("Console shows log. Use the monitors to interact.")
@@ -2572,11 +2572,11 @@ local function main()
-- Try loading cached inventory from disk for instant startup -- Try loading cached inventory from disk for instant startup
local cacheLoaded = loadCacheFromDisk() local cacheLoaded = loadCacheFromDisk()
if cacheLoaded then if cacheLoaded then
print("[INIT] Loaded cached inventory (" .. #cache.itemList .. " types)") log.info("INIT", "Loaded cached inventory (%d types)", #cache.itemList)
print("[INIT] Background refresh starting...") log.info("INIT", "Background refresh starting...")
else else
-- No cache: do full scan with progress bar -- No cache: do full scan with progress bar
print("[INIT] No cache found. Scanning inventories...") log.info("INIT", "No cache found. Scanning inventories...")
if mon then if mon then
local w, h = mon.getSize() local w, h = mon.getSize()
local buf = window.create(mon, 1, 1, w, h, false) local buf = window.create(mon, 1, 1, w, h, false)
@@ -2641,7 +2641,7 @@ local function main()
else else
refreshCache() refreshCache()
end end
print("[INIT] Done. Found " .. #cache.itemList .. " item types.") log.info("INIT", "Done. Found %d item types.", #cache.itemList)
end end
print("") print("")
@@ -2654,7 +2654,7 @@ local function main()
pcall(checkAlerts) pcall(checkAlerts)
needsRedraw = true needsRedraw = true
smelterNeedsRedraw = true smelterNeedsRedraw = true
print("[INIT] Background refresh complete. " .. #cache.itemList .. " types.") log.info("INIT", "Background refresh complete. %d types.", #cache.itemList)
end end
while true do while true do
sleep(SCAN_INTERVAL) sleep(SCAN_INTERVAL)
@@ -2768,10 +2768,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
@@ -2794,7 +2794,7 @@ local function main()
if name then if name then
invalidateWrapCache(name) invalidateWrapCache(name)
invalidatePeripheralCaches() invalidatePeripheralCaches()
print("[DETACH] " .. name) log.info("DETACH", "%s", name)
end end
end end
end, end,
@@ -2807,10 +2807,10 @@ local function main()
if channel == ORDER_CHANNEL and type(message) == "table" then if channel == ORDER_CHANNEL and type(message) == "table" then
local handlerOk, handlerErr = pcall(function() local handlerOk, handlerErr = pcall(function()
if message.type == "order" and message.itemName and message.amount then if message.type == "order" and message.itemName and message.amount then
print(string.format("[NET] Order: %s x%d", message.itemName, message.amount)) log.info("NET", "Order: %s x%d", message.itemName, message.amount)
local pok, success = pcall(orderItem, message.itemName, message.amount, message.dropperName) local pok, success = pcall(orderItem, message.itemName, message.amount, message.dropperName)
if not pok then if not pok then
print("[NET] orderItem crashed: " .. tostring(success)) log.error("NET", "orderItem crashed: %s", tostring(success))
success = false success = false
statusMessage = "Order error" statusMessage = "Order error"
statusColor = colors.red statusColor = colors.red
@@ -2826,7 +2826,7 @@ local function main()
end) end)
pcall(broadcastState) pcall(broadcastState)
elseif message.type == "scan" then elseif message.type == "scan" then
print("[NET] Scan request from client") log.info("NET", "Scan request from client")
configDirty = true -- resend full config after scan configDirty = true -- resend full config after scan
pcall(refreshCache) pcall(refreshCache)
pcall(checkAlerts) pcall(checkAlerts)
@@ -2835,7 +2835,7 @@ local function main()
pcall(broadcastState) pcall(broadcastState)
elseif message.type == "toggle_pause" then elseif message.type == "toggle_pause" then
smeltingPaused = not smeltingPaused smeltingPaused = not smeltingPaused
print("[NET] Smelting " .. (smeltingPaused and "PAUSED" or "RESUMED")) log.info("NET", "Smelting %s", smeltingPaused and "PAUSED" or "RESUMED")
saveDisabledRecipes() saveDisabledRecipes()
bumpStateVersion() bumpStateVersion()
smelterNeedsRedraw = true smelterNeedsRedraw = true