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