2022-03-10 19:09:21 +00:00
|
|
|
--
|
|
|
|
-- File System Logger
|
|
|
|
--
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local log = {}
|
|
|
|
|
2022-03-10 19:09:21 +00:00
|
|
|
-- we use extra short abbreviations since computer craft screens are very small
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local MODE = {
|
2022-04-29 17:32:37 +00:00
|
|
|
APPEND = 0,
|
|
|
|
NEW = 1
|
|
|
|
}
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.MODE = MODE
|
|
|
|
|
2022-03-23 19:41:08 +00:00
|
|
|
local LOG_DEBUG = true
|
|
|
|
|
2022-04-29 17:32:37 +00:00
|
|
|
local log_path = "/log.txt"
|
|
|
|
local mode = MODE.APPEND
|
|
|
|
local file_handle = nil
|
2022-03-10 19:09:21 +00:00
|
|
|
|
|
|
|
local _log = function (msg)
|
|
|
|
local stamped = os.date("[%c] ") .. msg
|
2022-04-27 22:43:07 +00:00
|
|
|
|
|
|
|
-- 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
|
2022-04-29 17:32:37 +00:00
|
|
|
local delete_log = fs.getFreeSpace(log_path) < 100
|
2022-04-27 22:43:07 +00:00
|
|
|
|
|
|
|
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()
|
2022-04-29 17:32:37 +00:00
|
|
|
fs.delete(log_path)
|
|
|
|
init(log_path, mode)
|
2022-04-27 22:43:07 +00:00
|
|
|
|
|
|
|
-- leave a message
|
|
|
|
local notif = os.date("[%c] ") .. "recycled log file"
|
|
|
|
file_handle.writeLine(notif)
|
|
|
|
file_handle.writeLine(stamped)
|
|
|
|
file_handle.flush()
|
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.init = function (path, write_mode)
|
2022-04-29 17:32:37 +00:00
|
|
|
log_path = path
|
|
|
|
mode = write_mode
|
|
|
|
|
|
|
|
if mode == MODE.APPEND then
|
|
|
|
file_handle = fs.open(path, "a")
|
|
|
|
else
|
|
|
|
file_handle = fs.open(path, "w+")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug = function (msg, trace)
|
2022-03-23 19:41:08 +00:00
|
|
|
if LOG_DEBUG then
|
|
|
|
local dbg_info = ""
|
|
|
|
|
|
|
|
if trace then
|
|
|
|
local name = ""
|
|
|
|
|
|
|
|
if debug.getinfo(2).name ~= nil then
|
|
|
|
name = ":" .. debug.getinfo(2).name .. "():"
|
|
|
|
end
|
|
|
|
|
|
|
|
dbg_info = debug.getinfo(2).short_src .. ":" .. name ..
|
|
|
|
debug.getinfo(2).currentline .. " > "
|
|
|
|
end
|
|
|
|
|
2022-04-05 20:08:55 +00:00
|
|
|
_log("[DBG] " .. dbg_info .. msg)
|
2022-03-23 19:41:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.info = function (msg)
|
2022-04-05 20:08:55 +00:00
|
|
|
_log("[INF] " .. msg)
|
2022-04-03 16:08:22 +00:00
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning = function (msg)
|
2022-04-05 20:08:55 +00:00
|
|
|
_log("[WRN] " .. msg)
|
2022-03-23 19:41:08 +00:00
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error = function (msg, trace)
|
2022-03-10 19:09:21 +00:00
|
|
|
local dbg_info = ""
|
|
|
|
|
|
|
|
if trace then
|
|
|
|
local name = ""
|
|
|
|
|
|
|
|
if debug.getinfo(2).name ~= nil then
|
|
|
|
name = ":" .. debug.getinfo(2).name .. "():"
|
|
|
|
end
|
|
|
|
|
|
|
|
dbg_info = debug.getinfo(2).short_src .. ":" .. name ..
|
|
|
|
debug.getinfo(2).currentline .. " > "
|
|
|
|
end
|
|
|
|
|
2022-04-05 20:08:55 +00:00
|
|
|
_log("[ERR] " .. dbg_info .. msg)
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.fatal = function (msg)
|
2022-04-05 20:08:55 +00:00
|
|
|
_log("[FTL] " .. msg)
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return log
|