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-04 17:37:01 +00:00
|
|
|
local log = {}
|
|
|
|
|
|
|
|
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-06 14:48:46 +00:00
|
|
|
----------------------------
|
|
|
|
-- PRIVATE DATA/FUNCTIONS --
|
|
|
|
----------------------------
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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-06 14:48:46 +00:00
|
|
|
if (result == "Out of space") or (fs.getFreeSpace(_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)
|
|
|
|
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-06 14:48:46 +00:00
|
|
|
----------------------
|
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
----------------------
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
log.init = function (path, write_mode)
|
2022-05-06 14:48:46 +00:00
|
|
|
_log_sys.path = path
|
|
|
|
_log_sys.mode = write_mode
|
2022-04-29 17:32:37 +00:00
|
|
|
|
2022-05-06 14:48:46 +00:00
|
|
|
if _log_sys.mode == MODE.APPEND then
|
|
|
|
_log_sys.file = fs.open(path, "a")
|
2022-04-29 17:32:37 +00:00
|
|
|
else
|
2022-05-06 14:48:46 +00:00
|
|
|
_log_sys.file = fs.open(path, "w+")
|
2022-04-29 17:32:37 +00:00
|
|
|
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
|
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-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
|
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-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-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
|