cleanup/fixes of scada common code
This commit is contained in:
@@ -13,10 +13,6 @@ local MODE = {
|
||||
|
||||
log.MODE = MODE
|
||||
|
||||
----------------------------
|
||||
-- PRIVATE DATA/FUNCTIONS --
|
||||
----------------------------
|
||||
|
||||
local LOG_DEBUG = true
|
||||
|
||||
local _log_sys = {
|
||||
@@ -25,12 +21,27 @@ local _log_sys = {
|
||||
file = nil
|
||||
}
|
||||
|
||||
local free_space = fs.getFreeSpace
|
||||
|
||||
-- initialize logger
|
||||
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
|
||||
local _log = function (msg)
|
||||
local time_stamp = os.date("[%c] ")
|
||||
local stamped = time_stamp .. msg
|
||||
|
||||
-- attempt to write log
|
||||
local status, result = pcall(function ()
|
||||
local status, result = pcall(function ()
|
||||
_log_sys.file.writeLine(stamped)
|
||||
_log_sys.file.flush()
|
||||
end)
|
||||
@@ -45,11 +56,11 @@ local _log = function (msg)
|
||||
end
|
||||
end
|
||||
|
||||
if (result == "Out of space") or (fs.getFreeSpace(_log_sys.path) < 100) then
|
||||
if (result == "Out of space") or (free_space(_log_sys.path) < 100) then
|
||||
-- delete the old log file and open a new one
|
||||
_log_sys.file.close()
|
||||
fs.delete(_log_sys.path)
|
||||
init(_log_sys.path, _log_sys.mode)
|
||||
log.init(_log_sys.path, _log_sys.mode)
|
||||
|
||||
-- leave a message
|
||||
_log_sys.file.writeLine(time_stamp .. "recycled log file")
|
||||
@@ -58,21 +69,7 @@ local _log = function (msg)
|
||||
end
|
||||
end
|
||||
|
||||
----------------------
|
||||
-- PUBLIC FUNCTIONS --
|
||||
----------------------
|
||||
|
||||
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
|
||||
|
||||
-- log debug messages
|
||||
log.debug = function (msg, trace)
|
||||
if LOG_DEBUG then
|
||||
local dbg_info = ""
|
||||
@@ -92,17 +89,20 @@ log.debug = function (msg, trace)
|
||||
end
|
||||
end
|
||||
|
||||
-- log info messages
|
||||
log.info = function (msg)
|
||||
_log("[INF] " .. msg)
|
||||
end
|
||||
|
||||
-- log warning messages
|
||||
log.warning = function (msg)
|
||||
_log("[WRN] " .. msg)
|
||||
end
|
||||
|
||||
-- log error messages
|
||||
log.error = function (msg, trace)
|
||||
local dbg_info = ""
|
||||
|
||||
|
||||
if trace then
|
||||
local info = debug.getinfo(2)
|
||||
local name = ""
|
||||
@@ -110,13 +110,14 @@ log.error = function (msg, trace)
|
||||
if info.name ~= nil then
|
||||
name = ":" .. info.name .. "():"
|
||||
end
|
||||
|
||||
|
||||
dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > "
|
||||
end
|
||||
|
||||
_log("[ERR] " .. dbg_info .. msg)
|
||||
end
|
||||
|
||||
-- log fatal errors
|
||||
log.fatal = function (msg)
|
||||
_log("[FTL] " .. msg)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user