mirror of
https://github.com/MikaylaFischler/cc-mek-scada.git
synced 2024-08-30 18:22:34 +00:00
#38 handle out of space when logging
This commit is contained in:
@ -6,13 +6,42 @@
|
|||||||
-- underscores are used since some of these names are used elsewhere (e.g. 'debug' is a lua table)
|
-- underscores are used since some of these names are used elsewhere (e.g. 'debug' is a lua table)
|
||||||
|
|
||||||
local LOG_DEBUG = true
|
local LOG_DEBUG = true
|
||||||
|
local LOG_PATH = "/log.txt"
|
||||||
|
|
||||||
local file_handle = fs.open("/log.txt", "a")
|
local file_handle = fs.open(LOG_PATH, "a")
|
||||||
|
|
||||||
local _log = function (msg)
|
local _log = function (msg)
|
||||||
local stamped = os.date("[%c] ") .. msg
|
local stamped = os.date("[%c] ") .. msg
|
||||||
file_handle.writeLine(stamped)
|
|
||||||
file_handle.flush()
|
-- attempt to write log
|
||||||
|
local status, result = pcall(function ()
|
||||||
|
file_handle.writeLine(stamped)
|
||||||
|
file_handle.flush()
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- if we don't have much space, we need to create a new log file
|
||||||
|
local delete_log = fs.getFreeSpace(LOG_PATH) < 100
|
||||||
|
|
||||||
|
if not status then
|
||||||
|
if result == "Out of space" then
|
||||||
|
delete_log = true
|
||||||
|
elseif result ~= nil then
|
||||||
|
print("unknown error writing to logfile: " .. result)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if delete_log then
|
||||||
|
-- delete the old log file and open a new one
|
||||||
|
file_handle.close()
|
||||||
|
fs.delete(LOG_PATH)
|
||||||
|
file_handle = fs.open(LOG_PATH, "a")
|
||||||
|
|
||||||
|
-- leave a message
|
||||||
|
local notif = os.date("[%c] ") .. "recycled log file"
|
||||||
|
file_handle.writeLine(notif)
|
||||||
|
file_handle.writeLine(stamped)
|
||||||
|
file_handle.flush()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function _debug(msg, trace)
|
function _debug(msg, trace)
|
||||||
|
Reference in New Issue
Block a user