logging optimizations
This commit is contained in:
parent
b7e5ced2e8
commit
d0b2820160
@ -1,11 +1,11 @@
|
|||||||
|
local util = require("scada-common.util")
|
||||||
|
|
||||||
--
|
--
|
||||||
-- File System Logger
|
-- File System Logger
|
||||||
--
|
--
|
||||||
|
|
||||||
local log = {}
|
local log = {}
|
||||||
|
|
||||||
-- we use extra short abbreviations since computer craft screens are very small
|
|
||||||
|
|
||||||
local MODE = {
|
local MODE = {
|
||||||
APPEND = 0,
|
APPEND = 0,
|
||||||
NEW = 1
|
NEW = 1
|
||||||
@ -13,54 +13,63 @@ local MODE = {
|
|||||||
|
|
||||||
log.MODE = MODE
|
log.MODE = MODE
|
||||||
|
|
||||||
|
----------------------------
|
||||||
|
-- PRIVATE DATA/FUNCTIONS --
|
||||||
|
----------------------------
|
||||||
|
|
||||||
local LOG_DEBUG = true
|
local LOG_DEBUG = true
|
||||||
|
|
||||||
local log_path = "/log.txt"
|
local _log_sys = {
|
||||||
local mode = MODE.APPEND
|
path = "/log.txt",
|
||||||
local file_handle = nil
|
mode = MODE.APPEND,
|
||||||
|
file = nil
|
||||||
|
}
|
||||||
|
|
||||||
local _log = function (msg)
|
local _log = function (msg)
|
||||||
local stamped = os.date("[%c] ") .. msg
|
local time_stamp = os.date("[%c] ")
|
||||||
|
local stamped = time_stamp .. msg
|
||||||
|
|
||||||
-- attempt to write log
|
-- attempt to write log
|
||||||
local status, result = pcall(function ()
|
local status, result = pcall(function ()
|
||||||
file_handle.writeLine(stamped)
|
_log_sys.file.writeLine(stamped)
|
||||||
file_handle.flush()
|
_log_sys.file.flush()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
-- if we don't have much space, we need to create a new log file
|
-- if we don't have space, we need to create a new log file
|
||||||
local delete_log = fs.getFreeSpace(log_path) < 100
|
|
||||||
|
|
||||||
if not status then
|
if not status then
|
||||||
if result == "Out of space" then
|
if result == "Out of space" then
|
||||||
delete_log = true
|
-- will delete log file
|
||||||
elseif result ~= nil then
|
elseif result ~= nil then
|
||||||
print("unknown error writing to logfile: " .. result)
|
util.println("unknown error writing to logfile: " .. result)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if delete_log then
|
if (result == "Out of space") or (fs.getFreeSpace(_log_sys.path) < 100) then
|
||||||
-- delete the old log file and open a new one
|
-- delete the old log file and open a new one
|
||||||
file_handle.close()
|
_log_sys.file.close()
|
||||||
fs.delete(log_path)
|
fs.delete(_log_sys.path)
|
||||||
init(log_path, mode)
|
init(_log_sys.path, _log_sys.mode)
|
||||||
|
|
||||||
-- leave a message
|
-- leave a message
|
||||||
local notif = os.date("[%c] ") .. "recycled log file"
|
_log_sys.file.writeLine(time_stamp .. "recycled log file")
|
||||||
file_handle.writeLine(notif)
|
_log_sys.file.writeLine(stamped)
|
||||||
file_handle.writeLine(stamped)
|
_log_sys.file.flush()
|
||||||
file_handle.flush()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
----------------------
|
||||||
|
-- PUBLIC FUNCTIONS --
|
||||||
|
----------------------
|
||||||
|
|
||||||
log.init = function (path, write_mode)
|
log.init = function (path, write_mode)
|
||||||
log_path = path
|
_log_sys.path = path
|
||||||
mode = write_mode
|
_log_sys.mode = write_mode
|
||||||
|
|
||||||
if mode == MODE.APPEND then
|
if _log_sys.mode == MODE.APPEND then
|
||||||
file_handle = fs.open(path, "a")
|
_log_sys.file = fs.open(path, "a")
|
||||||
else
|
else
|
||||||
file_handle = fs.open(path, "w+")
|
_log_sys.file = fs.open(path, "w+")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -69,14 +78,14 @@ log.debug = function (msg, trace)
|
|||||||
local dbg_info = ""
|
local dbg_info = ""
|
||||||
|
|
||||||
if trace then
|
if trace then
|
||||||
|
local info = debug.getinfo(2)
|
||||||
local name = ""
|
local name = ""
|
||||||
|
|
||||||
if debug.getinfo(2).name ~= nil then
|
if info.name ~= nil then
|
||||||
name = ":" .. debug.getinfo(2).name .. "():"
|
name = ":" .. info.name .. "():"
|
||||||
end
|
end
|
||||||
|
|
||||||
dbg_info = debug.getinfo(2).short_src .. ":" .. name ..
|
dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > "
|
||||||
debug.getinfo(2).currentline .. " > "
|
|
||||||
end
|
end
|
||||||
|
|
||||||
_log("[DBG] " .. dbg_info .. msg)
|
_log("[DBG] " .. dbg_info .. msg)
|
||||||
@ -95,14 +104,14 @@ log.error = function (msg, trace)
|
|||||||
local dbg_info = ""
|
local dbg_info = ""
|
||||||
|
|
||||||
if trace then
|
if trace then
|
||||||
|
local info = debug.getinfo(2)
|
||||||
local name = ""
|
local name = ""
|
||||||
|
|
||||||
if debug.getinfo(2).name ~= nil then
|
if info.name ~= nil then
|
||||||
name = ":" .. debug.getinfo(2).name .. "():"
|
name = ":" .. info.name .. "():"
|
||||||
end
|
end
|
||||||
|
|
||||||
dbg_info = debug.getinfo(2).short_src .. ":" .. name ..
|
dbg_info = info.short_src .. ":" .. name .. info.currentline .. " > "
|
||||||
debug.getinfo(2).currentline .. " > "
|
|
||||||
end
|
end
|
||||||
|
|
||||||
_log("[ERR] " .. dbg_info .. msg)
|
_log("[ERR] " .. dbg_info .. msg)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user