Made some formatting changes to lib/log.lua and disabled buffering when trimming the log.

The reason for this is that log files can get really large, and
buffering loads the whole file into the buffer. We devised an algorithm
to not use too much memory when trimming log files by NOT loading
everything into RAM, but buffering nullified it. It has now been
disabled.
This commit is contained in:
2025-10-04 20:14:36 +03:00
parent d63941814f
commit e0b6feb98c
+21 -10
View File
@@ -16,7 +16,7 @@ local log = {}
if not _G.logSettings then if not _G.logSettings then
_G.logSettings = { -- We have to preload the library just for this :P _G.logSettings = { -- We have to preload the library just for this :P
["printLogs"] = true, ["printLogs"] = true,
["printerY"] = 1 ["printerY"] = 1,
} }
end end
@@ -36,7 +36,7 @@ local function writeToLog(path, text)
-- Log trimming if it gets too long -- Log trimming if it gets too long
if fs.size(path) > logFileSizeLimit then if fs.size(path) > logFileSizeLimit then
local sizeCounter = 0 local sizeCounter = 0
local readHandle = fs.open(path, "r") local readHandle = fs.open(path, "r", false) -- Making sure buffering is disabled, otherwise this whole thing is pointless
local currentChunk = "" local currentChunk = ""
readHandle:seek("end", -chunkSize) readHandle:seek("end", -chunkSize)
repeat repeat
@@ -51,8 +51,13 @@ local function writeToLog(path, text)
if not infoEntry and not warnEntry and not errorEntry then if not infoEntry and not warnEntry and not errorEntry then
readHandle:seek(-chunkSize) readHandle:seek(-chunkSize)
else else
readHandle:seek(math.min(infoEntry or math.huge or math.maxinteger, warnEntry or math.huge or math.maxinteger, readHandle:seek(
errorEntry or math.huge or math.maxinteger) - 1) math.min(
infoEntry or math.huge or math.maxinteger,
warnEntry or math.huge or math.maxinteger,
errorEntry or math.huge or math.maxinteger
) - 1
)
break break
end end
if readHandle:seek("cur") == 0 then -- Failsafe to prevent infinite loops if readHandle:seek("cur") == 0 then -- Failsafe to prevent infinite loops
@@ -97,16 +102,22 @@ setmetatable(log, {
return { return {
["logpath"] = fs.concat("/halyde/logs/", index .. ".log"), ["logpath"] = fs.concat("/halyde/logs/", index .. ".log"),
["info"] = function(text) ["info"] = function(text)
writeToLog(fs.concat("/halyde/logs/", index .. ".log"), writeToLog(
"INFO [" .. string.format("%.2f", computer.uptime()) .. "] " .. text) fs.concat("/halyde/logs/", index .. ".log"),
"INFO [" .. string.format("%.2f", computer.uptime()) .. "] " .. text
)
end, end,
["warn"] = function(text) ["warn"] = function(text)
writeToLog(fs.concat("/halyde/logs/", index .. ".log"), writeToLog(
"WARN [" .. string.format("%.2f", computer.uptime()) .. "] " .. text) fs.concat("/halyde/logs/", index .. ".log"),
"WARN [" .. string.format("%.2f", computer.uptime()) .. "] " .. text
)
end, end,
["error"] = function(text) ["error"] = function(text)
writeToLog(fs.concat("/halyde/logs/", index .. ".log"), writeToLog(
"ERROR [" .. string.format("%.2f", computer.uptime()) .. "] " .. text) fs.concat("/halyde/logs/", index .. ".log"),
"ERROR [" .. string.format("%.2f", computer.uptime()) .. "] " .. text
)
end, end,
} }
end, end,