cc-mek-scada/scada-common/log.lua

125 lines
2.6 KiB
Lua
Raw Normal View History

2022-05-06 14:48:46 +00:00
local util = require("scada-common.util")
--
-- File System Logger
--
local log = {}
local MODE = {
APPEND = 0,
NEW = 1
}
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
}
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
end
2022-05-06 14:48:46 +00:00
----------------------
-- PUBLIC FUNCTIONS --
----------------------
log.init = function (path, write_mode)
2022-05-06 14:48:46 +00:00
_log_sys.path = path
_log_sys.mode = write_mode
2022-05-06 14:48:46 +00:00
if _log_sys.mode == MODE.APPEND then
_log_sys.file = fs.open(path, "a")
else
2022-05-06 14:48:46 +00:00
_log_sys.file = fs.open(path, "w+")
end
end
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
log.info = function (msg)
2022-04-05 20:08:55 +00:00
_log("[INF] " .. msg)
end
log.warning = function (msg)
2022-04-05 20:08:55 +00:00
_log("[WRN] " .. msg)
2022-03-23 19:41:08 +00:00
end
log.error = function (msg, trace)
local dbg_info = ""
if trace then
2022-05-06 14:48:46 +00:00
local info = debug.getinfo(2)
local name = ""
2022-05-06 14:48:46 +00:00
if info.name ~= nil then
name = ":" .. info.name .. "():"
end
2022-05-06 14:48:46 +00:00
dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > "
end
2022-04-05 20:08:55 +00:00
_log("[ERR] " .. dbg_info .. msg)
end
log.fatal = function (msg)
2022-04-05 20:08:55 +00:00
_log("[FTL] " .. msg)
end
return log