From e0b6feb98ce75158408057aa8660ae07f8fe6f6c Mon Sep 17 00:00:00 2001 From: WahPlus Date: Sat, 4 Oct 2025 20:14:36 +0300 Subject: [PATCH] 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. --- lib/log.lua | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/log.lua b/lib/log.lua index fcdea40..0a2168c 100644 --- a/lib/log.lua +++ b/lib/log.lua @@ -16,7 +16,7 @@ local log = {} if not _G.logSettings then _G.logSettings = { -- We have to preload the library just for this :P ["printLogs"] = true, - ["printerY"] = 1 + ["printerY"] = 1, } end @@ -36,7 +36,7 @@ local function writeToLog(path, text) -- Log trimming if it gets too long if fs.size(path) > logFileSizeLimit then 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 = "" readHandle:seek("end", -chunkSize) repeat @@ -51,8 +51,13 @@ local function writeToLog(path, text) if not infoEntry and not warnEntry and not errorEntry then readHandle:seek(-chunkSize) else - readHandle:seek(math.min(infoEntry or math.huge or math.maxinteger, warnEntry or math.huge or math.maxinteger, - errorEntry or math.huge or math.maxinteger) - 1) + readHandle:seek( + 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 end if readHandle:seek("cur") == 0 then -- Failsafe to prevent infinite loops @@ -97,16 +102,22 @@ setmetatable(log, { return { ["logpath"] = fs.concat("/halyde/logs/", index .. ".log"), ["info"] = function(text) - writeToLog(fs.concat("/halyde/logs/", index .. ".log"), - "INFO [" .. string.format("%.2f", computer.uptime()) .. "] " .. text) + writeToLog( + fs.concat("/halyde/logs/", index .. ".log"), + "INFO [" .. string.format("%.2f", computer.uptime()) .. "] " .. text + ) end, ["warn"] = function(text) - writeToLog(fs.concat("/halyde/logs/", index .. ".log"), - "WARN [" .. string.format("%.2f", computer.uptime()) .. "] " .. text) + writeToLog( + fs.concat("/halyde/logs/", index .. ".log"), + "WARN [" .. string.format("%.2f", computer.uptime()) .. "] " .. text + ) end, ["error"] = function(text) - writeToLog(fs.concat("/halyde/logs/", index .. ".log"), - "ERROR [" .. string.format("%.2f", computer.uptime()) .. "] " .. text) + writeToLog( + fs.concat("/halyde/logs/", index .. ".log"), + "ERROR [" .. string.format("%.2f", computer.uptime()) .. "] " .. text + ) end, } end,