Refactor logging to use structured logging for improved clarity and consistency

This commit is contained in:
MayaTheShy
2026-03-22 02:02:15 -04:00
parent 71db1d9973
commit e017eb1009

View File

@@ -48,7 +48,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 " .. CONFIG_FILE) log.warn("CONFIG", "Failed to parse %s", CONFIG_FILE)
return return
end end
_loadedConfig = cfg _loadedConfig = cfg
@@ -72,7 +72,7 @@ local function loadConfig()
if cfg.orderChannel then ORDER_CHANNEL = cfg.orderChannel end if cfg.orderChannel then ORDER_CHANNEL = cfg.orderChannel end
if cfg.craftChannel then CRAFT_CHANNEL = cfg.craftChannel end if cfg.craftChannel then CRAFT_CHANNEL = cfg.craftChannel end
if cfg.craftReplyChannel then CRAFT_REPLY_CHANNEL = cfg.craftReplyChannel end if cfg.craftReplyChannel then CRAFT_REPLY_CHANNEL = cfg.craftReplyChannel end
print("[CONFIG] Loaded from " .. CONFIG_FILE) log.info("CONFIG", "Loaded from %s", CONFIG_FILE)
end end
loadConfig() loadConfig()
@@ -573,7 +573,7 @@ local function loadCacheFromDisk()
end end
end) end)
if not ok then if not ok then
print("[WARN] Could not load cache: " .. tostring(err)) log.warn("INIT", "Could not load cache: %s", tostring(err))
return false return false
end end
return true return true
@@ -1052,25 +1052,25 @@ local CRAFT_TIMEOUT = 15 -- seconds to wait for turtle reply
local function craftItem(recipeIdx) local function craftItem(recipeIdx)
local recipe = CRAFTABLE[recipeIdx] local recipe = CRAFTABLE[recipeIdx]
if not recipe then if not recipe then
print("[CRAFT] Invalid recipe index: " .. tostring(recipeIdx)) log.error("CRAFT", "Invalid recipe index: %s", tostring(recipeIdx))
return false, "Invalid recipe" return false, "Invalid recipe"
end end
if not craftTurtleName then if not craftTurtleName then
print("[CRAFT] No turtle detected on network") log.error("CRAFT", "No turtle detected on network")
return false, "No turtle" return false, "No turtle"
end end
if not networkModem then if not networkModem then
print("[CRAFT] No modem available for craft commands") log.error("CRAFT", "No modem available for craft commands")
return false, "No modem" return false, "No modem"
end end
-- Verify the turtle is still on the network -- Verify the turtle is still on the network
if not peripheral.isPresent(craftTurtleName) then if not peripheral.isPresent(craftTurtleName) then
print("[CRAFT] Turtle offline: " .. craftTurtleName) log.error("CRAFT", "Turtle offline: %s", craftTurtleName)
return false, "Turtle offline" return false, "Turtle offline"
end end
print(string.format("[CRAFT] Starting craft: %s (turtle: %s)", recipe.output, craftTurtleName)) log.info("CRAFT", "Starting craft: %s (turtle: %s)", recipe.output, craftTurtleName)
activity.crafting = true activity.crafting = true
needsRedraw = true needsRedraw = true
@@ -1113,7 +1113,7 @@ local function craftItem(recipeIdx)
end end
if not found then if not found then
print(string.format("[CRAFT] Cannot find %s in storage, aborting", itemName)) log.error("CRAFT", "Cannot find %s in storage, aborting", itemName)
activity.crafting = false activity.crafting = false
needsRedraw = true needsRedraw = true
smelterNeedsRedraw = true smelterNeedsRedraw = true
@@ -1131,7 +1131,7 @@ local function craftItem(recipeIdx)
returnChests = chests, returnChests = chests,
} }
print(string.format("[CRAFT] Sending craft request to turtle on channel %d", CRAFT_CHANNEL)) log.info("CRAFT", "Sending craft request to turtle on channel %d", CRAFT_CHANNEL)
networkModem.transmit(CRAFT_CHANNEL, CRAFT_REPLY_CHANNEL, craftMessage) networkModem.transmit(CRAFT_CHANNEL, CRAFT_REPLY_CHANNEL, craftMessage)
-- Adjust cache: mark ingredients as "in transit" (removed from chests) -- Adjust cache: mark ingredients as "in transit" (removed from chests)
@@ -1140,7 +1140,7 @@ local function craftItem(recipeIdx)
end end
-- Wait for reply from turtle with timeout -- Wait for reply from turtle with timeout
print(string.format("[CRAFT] Waiting for turtle reply (timeout: %ds)...", CRAFT_TIMEOUT)) log.info("CRAFT", "Waiting for turtle reply (timeout: %ds)...", CRAFT_TIMEOUT)
local deadline = os.clock() + CRAFT_TIMEOUT local deadline = os.clock() + CRAFT_TIMEOUT
local result = nil local result = nil
@@ -1168,8 +1168,8 @@ local function craftItem(recipeIdx)
-- Timeout — turtle didn't respond. Items may be stuck in turtle. -- Timeout — turtle didn't respond. Items may be stuck in turtle.
-- We've already adjusted the cache as if ingredients were consumed. -- We've already adjusted the cache as if ingredients were consumed.
-- A manual scan will reconcile later. -- A manual scan will reconcile later.
print("[CRAFT] TIMEOUT: No reply from turtle within " .. CRAFT_TIMEOUT .. "s") log.error("CRAFT", "TIMEOUT: No reply from turtle within %ds", CRAFT_TIMEOUT)
print("[CRAFT] Items may be stuck in turtle. Run a manual scan to reconcile.") log.error("CRAFT", "Items may be stuck in turtle. Run a manual scan to reconcile.")
return false, "Turtle timeout" return false, "Turtle timeout"
end end
@@ -1212,7 +1212,7 @@ local function craftItem(recipeIdx)
end end
local short = recipe.output:gsub("^minecraft:", ""):gsub("_", " ") local short = recipe.output:gsub("^minecraft:", ""):gsub("_", " ")
print(string.format("[CRAFT] OK: %s x%d", short, totalOutput)) log.info("CRAFT", "OK: %s x%d", short, totalOutput)
return true return true
else else
-- Craft failed. The turtle returned ingredients to chests. -- Craft failed. The turtle returned ingredients to chests.
@@ -1224,7 +1224,7 @@ local function craftItem(recipeIdx)
end end
local errMsg = result.error or "Craft failed" local errMsg = result.error or "Craft failed"
print("[CRAFT] Failed: " .. errMsg) log.error("CRAFT", "Failed: %s", errMsg)
return false, errMsg return false, errMsg
end end
end end
@@ -1747,7 +1747,7 @@ local function sortBarrel(barrelOverride)
adjustCache(item.name, entry.chest, n) adjustCache(item.name, entry.chest, n)
needsRedraw = true needsRedraw = true
smelterNeedsRedraw = true smelterNeedsRedraw = true
print(string.format("[SORT] %s x%d -> %s", item.name, n, entry.chest)) log.info("SORT", "%s x%d -> %s", item.name, n, entry.chest)
end end
if moved >= item.count then break end if moved >= item.count then break end
end end
@@ -1762,7 +1762,7 @@ local function sortBarrel(barrelOverride)
adjustCache(item.name, chest, n) adjustCache(item.name, chest, n)
needsRedraw = true needsRedraw = true
smelterNeedsRedraw = true smelterNeedsRedraw = true
print(string.format("[SORT] %s x%d -> %s", item.name, n, chest)) log.info("SORT", "%s x%d -> %s", item.name, n, chest)
end end
if moved >= item.count then break end if moved >= item.count then break end
end end
@@ -1770,7 +1770,7 @@ local function sortBarrel(barrelOverride)
end end
if moved < item.count then if moved < item.count then
print(string.format("[WARN] Could not sort %d remaining %s", item.count - moved, item.name)) log.warn("SORT", "Could not sort %d remaining %s", item.count - moved, item.name)
end end
end end
@@ -1807,15 +1807,15 @@ local function autoSmelt()
if n and n > 0 then if n and n > 0 then
adjustCache(outputItem.name, chest, n) adjustCache(outputItem.name, chest, n)
remaining = remaining - n remaining = remaining - n
print(string.format("[SMELT] Output %s x%d -> %s", log.info("SMELT", "Output %s x%d -> %s",
outputItem.name, n, chest)) outputItem.name, n, chest)
didWork = true didWork = true
if remaining <= 0 then break end if remaining <= 0 then break end
end end
end end
if remaining > 0 then if remaining > 0 then
print(string.format("[WARN] Could not move %d %s from %s output (chests full?)", log.warn("SMELT", "Could not move %d %s from %s output (chests full?)",
remaining, outputItem.name, fname)) remaining, outputItem.name, fname)
end end
end end
@@ -1827,8 +1827,8 @@ local function autoSmelt()
local n = furnace.pushItems(chest, slot) local n = furnace.pushItems(chest, slot)
if n and n > 0 then if n and n > 0 then
adjustCache(item.name, chest, n) adjustCache(item.name, chest, n)
print(string.format("[SMELT] Extra slot %d: %s x%d -> %s", log.info("SMELT", "Extra slot %d: %s x%d -> %s",
slot, item.name, n, chest)) slot, item.name, n, chest)
didWork = true didWork = true
break break
end end
@@ -1851,8 +1851,8 @@ local function autoSmelt()
local n = furnace.pushItems(chest, SLOT_INPUT) local n = furnace.pushItems(chest, SLOT_INPUT)
if n and n > 0 then if n and n > 0 then
adjustCache(inputItem.name, chest, n) adjustCache(inputItem.name, chest, n)
print(string.format("[SMELT] Removed incompatible %s x%d from %s -> %s", log.info("SMELT", "Removed incompatible %s x%d from %s -> %s",
inputItem.name, n, fname, chest)) inputItem.name, n, fname, chest)
didWork = true didWork = true
break break
end end
@@ -1878,8 +1878,8 @@ local function autoSmelt()
local n = chest.pushItems(fname, slot, toMove, SLOT_FUEL) local n = chest.pushItems(fname, slot, toMove, SLOT_FUEL)
if n and n > 0 then if n and n > 0 then
adjustCache(fuel.name, source.chest, -n) adjustCache(fuel.name, source.chest, -n)
print(string.format("[SMELT] Fuel %s x%d -> %s", log.info("SMELT", "Fuel %s x%d -> %s",
fuel.name, n, fname)) fuel.name, n, fname)
didWork = true didWork = true
needFuel = false needFuel = false
break break
@@ -1963,8 +1963,8 @@ local function autoSmelt()
local n = chest.pushItems(ef.name, slot, toMove, SLOT_INPUT) local n = chest.pushItems(ef.name, slot, toMove, SLOT_INPUT)
if n and n > 0 then if n and n > 0 then
adjustCache(itemName, source.chest, -n) adjustCache(itemName, source.chest, -n)
print(string.format("[SMELT] Input %s x%d -> %s (balanced %d/furnace)", log.info("SMELT", "Input %s x%d -> %s (balanced %d/furnace)",
itemName, n, ef.name, perFurnace)) itemName, n, ef.name, perFurnace)
didWork = true didWork = true
remaining = remaining - n remaining = remaining - n
available = available - n available = available - n
@@ -2069,7 +2069,7 @@ local function defragInventory()
end end
if totalMerged > 0 then if totalMerged > 0 then
print(string.format("[DEFRAG] Consolidated %d items", totalMerged)) log.info("DEFRAG", "Consolidated %d items", totalMerged)
end end
activity.defragging = false activity.defragging = false
@@ -2095,7 +2095,7 @@ local function autoCompost()
local n = hopper.pushItems(chest, slot) local n = hopper.pushItems(chest, slot)
if n and n > 0 then if n and n > 0 then
adjustCache(item.name, chest, n) adjustCache(item.name, chest, n)
print(string.format("[COMPOST] %s x%d -> %s", item.name, n, chest)) log.info("COMPOST", "%s x%d -> %s", item.name, n, chest)
didWork = true didWork = true
break break
end end
@@ -2149,8 +2149,8 @@ local function autoCompost()
adjustCache(itemName, source.chest, -n) adjustCache(itemName, source.chest, -n)
fed = fed + n fed = fed + n
didWork = true didWork = true
print(string.format("[COMPOST] Fed %s x%d -> dropper", log.info("COMPOST", "Fed %s x%d -> dropper",
itemName, n)) itemName, n)
if fed >= toFeed then break end if fed >= toFeed then break end
end end
end end
@@ -2239,7 +2239,7 @@ local function orderItem(itemName, amount, dropperOverride)
adjustCache(itemName, entry.chest, -moved) adjustCache(itemName, entry.chest, -moved)
needsRedraw = true needsRedraw = true
smelterNeedsRedraw = true smelterNeedsRedraw = true
print(string.format("[ORDER] %s x%d from %s", itemName, moved, entry.chest)) log.info("ORDER", "%s x%d from %s", itemName, moved, entry.chest)
end end
if remaining <= 0 then break end if remaining <= 0 then break end
end end
@@ -2253,7 +2253,7 @@ local function orderItem(itemName, amount, dropperOverride)
if sent > 0 then if sent > 0 then
statusMessage = string.format("Dispensing %s x%d", short, sent) statusMessage = string.format("Dispensing %s x%d", short, sent)
statusColor = colors.lime statusColor = colors.lime
print(string.format("[OK] Ordered %s x%d", short, sent)) log.info("ORDER", "Ordered %s x%d", short, sent)
else else
statusMessage = "Could not order " .. short statusMessage = "Could not order " .. short
statusColor = colors.red statusColor = colors.red