2022-05-06 14:48:46 +00:00
|
|
|
local util = require("scada-common.util")
|
|
|
|
|
2022-03-10 19:09:21 +00:00
|
|
|
--
|
|
|
|
-- File System Logger
|
|
|
|
--
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@class log
|
2022-05-04 17:37:01 +00:00
|
|
|
local log = {}
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@alias MODE integer
|
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-05-10 21:06:27 +00:00
|
|
|
-- whether to log debug messages or not
|
2022-03-23 19:41:08 +00:00
|
|
|
local LOG_DEBUG = true
|
|
|
|
|
2022-05-06 14:48:46 +00:00
|
|
|
local _log_sys = {
|
|
|
|
path = "/log.txt",
|
|
|
|
mode = MODE.APPEND,
|
|
|
|
file = nil
|
|
|
|
}
|
2022-03-10 19:09:21 +00:00
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@type function
|
2022-05-10 15:35:52 +00:00
|
|
|
local free_space = fs.getFreeSpace
|
|
|
|
|
|
|
|
-- initialize logger
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param path string file path
|
|
|
|
---@param write_mode MODE
|
2022-05-10 15:35:52 +00:00
|
|
|
log.init = function (path, write_mode)
|
|
|
|
_log_sys.path = path
|
|
|
|
_log_sys.mode = write_mode
|
|
|
|
|
|
|
|
if _log_sys.mode == MODE.APPEND then
|
|
|
|
_log_sys.file = fs.open(path, "a")
|
|
|
|
else
|
|
|
|
_log_sys.file = fs.open(path, "w+")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- private log write function
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param msg string
|
2022-03-10 19:09:21 +00:00
|
|
|
local _log = function (msg)
|
2022-05-06 14:48:46 +00:00
|
|
|
local time_stamp = os.date("[%c] ")
|
|
|
|
local stamped = time_stamp .. msg
|
2022-04-27 22:43:07 +00:00
|
|
|
|
|
|
|
-- attempt to write log
|
2022-05-10 15:35:52 +00:00
|
|
|
local status, result = pcall(function ()
|
2022-05-06 14:48:46 +00:00
|
|
|
_log_sys.file.writeLine(stamped)
|
|
|
|
_log_sys.file.flush()
|
2022-04-27 22:43:07 +00:00
|
|
|
end)
|
|
|
|
|
2022-05-06 14:48:46 +00:00
|
|
|
-- if we don't have space, we need to create a new log file
|
2022-04-27 22:43:07 +00:00
|
|
|
|
|
|
|
if not status then
|
|
|
|
if result == "Out of space" then
|
2022-05-06 14:48:46 +00:00
|
|
|
-- will delete log file
|
2022-04-27 22:43:07 +00:00
|
|
|
elseif result ~= nil then
|
2022-05-06 14:48:46 +00:00
|
|
|
util.println("unknown error writing to logfile: " .. result)
|
2022-04-27 22:43:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-10 15:35:52 +00:00
|
|
|
if (result == "Out of space") or (free_space(_log_sys.path) < 100) then
|
2022-04-27 22:43:07 +00:00
|
|
|
-- delete the old log file and open a new one
|
2022-05-06 14:48:46 +00:00
|
|
|
_log_sys.file.close()
|
|
|
|
fs.delete(_log_sys.path)
|
2022-05-10 15:35:52 +00:00
|
|
|
log.init(_log_sys.path, _log_sys.mode)
|
2022-04-27 22:43:07 +00:00
|
|
|
|
|
|
|
-- leave a message
|
2022-05-06 14:48:46 +00:00
|
|
|
_log_sys.file.writeLine(time_stamp .. "recycled log file")
|
|
|
|
_log_sys.file.writeLine(stamped)
|
|
|
|
_log_sys.file.flush()
|
2022-04-27 22:43:07 +00:00
|
|
|
end
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
|
|
|
|
2022-05-10 15:35:52 +00:00
|
|
|
-- log debug messages
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param msg string message
|
|
|
|
---@param trace? boolean include file trace
|
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
|
2022-05-06 14:48:46 +00:00
|
|
|
local info = debug.getinfo(2)
|
2022-03-23 19:41:08 +00:00
|
|
|
local name = ""
|
|
|
|
|
2022-05-06 14:48:46 +00:00
|
|
|
if info.name ~= nil then
|
|
|
|
name = ":" .. info.name .. "():"
|
2022-03-23 19:41:08 +00:00
|
|
|
end
|
|
|
|
|
2022-05-06 14:48:46 +00:00
|
|
|
dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > "
|
2022-03-23 19:41:08 +00:00
|
|
|
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-10 15:35:52 +00:00
|
|
|
-- log info messages
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param msg string message
|
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-10 15:35:52 +00:00
|
|
|
-- log warning messages
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param msg string message
|
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-10 15:35:52 +00:00
|
|
|
-- log error messages
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param msg string message
|
|
|
|
---@param trace? boolean include file trace
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error = function (msg, trace)
|
2022-03-10 19:09:21 +00:00
|
|
|
local dbg_info = ""
|
2022-05-10 15:35:52 +00:00
|
|
|
|
2022-03-10 19:09:21 +00:00
|
|
|
if trace then
|
2022-05-06 14:48:46 +00:00
|
|
|
local info = debug.getinfo(2)
|
2022-03-10 19:09:21 +00:00
|
|
|
local name = ""
|
|
|
|
|
2022-05-06 14:48:46 +00:00
|
|
|
if info.name ~= nil then
|
|
|
|
name = ":" .. info.name .. "():"
|
2022-03-10 19:09:21 +00:00
|
|
|
end
|
2022-05-10 15:35:52 +00:00
|
|
|
|
2022-05-06 14:48:46 +00:00
|
|
|
dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > "
|
2022-03-10 19:09:21 +00:00
|
|
|
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-10 15:35:52 +00:00
|
|
|
-- log fatal errors
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param msg string message
|
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
|