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