private scada-common data refactor

This commit is contained in:
Mikayla Fischler
2025-10-26 13:41:59 -04:00
parent 7ddd6f32c5
commit 390cf98b0a
3 changed files with 109 additions and 109 deletions

View File

@@ -20,7 +20,7 @@ local MODE = { APPEND = 0, NEW = 1 }
log.MODE = MODE
local logger = {
local _log = {
not_ready = true,
path = "/log.txt",
mode = MODE.APPEND,
@@ -42,36 +42,36 @@ local free_space = fs.getFreeSpace
---@param err_msg string|nil error message
---@return boolean out_of_space
local function check_out_of_space(err_msg)
return (free_space(logger.path) < MIN_SPACE) or ((err_msg ~= nil) and (string.find(err_msg, OUT_OF_SPACE) ~= nil))
return (free_space(_log.path) < MIN_SPACE) or ((err_msg ~= nil) and (string.find(err_msg, OUT_OF_SPACE) ~= nil))
end
-- private log write function
---@param msg_bits any[]
local function _log(msg_bits)
if logger.not_ready then return end
local function write_log(msg_bits)
if _log.not_ready then return end
local time_stamp = os.date(TIME_FMT)
local stamped = util.c(time_stamp, table.unpack(msg_bits))
-- attempt to write log
local status, result = pcall(function ()
logger.file.writeLine(stamped)
logger.file.flush()
_log.file.writeLine(stamped)
_log.file.flush()
end)
-- if we don't have space, we need to create a new log file
if check_out_of_space() then
-- delete the old log file before opening a new one
logger.file.close()
fs.delete(logger.path)
_log.file.close()
fs.delete(_log.path)
-- re-init logger and pass dmesg_out so that it doesn't change
log.init(logger.path, logger.mode, logger.debug, logger.dmesg_out)
log.init(_log.path, _log.mode, _log.debug, _log.dmesg_out)
-- log the message and recycle warning
logger.file.writeLine(time_stamp .. WRN_TAG .. "recycled log file")
logger.file.writeLine(stamped)
logger.file.flush()
_log.file.writeLine(time_stamp .. WRN_TAG .. "recycled log file")
_log.file.writeLine(stamped)
_log.file.flush()
elseif (not status) and (result ~= nil) then
util.println("unexpected error writing to the log file: " .. result)
end
@@ -89,45 +89,45 @@ end
function log.init(path, write_mode, include_debug, dmesg_redirect)
local err_msg
logger.path = path
logger.mode = write_mode
logger.debug = include_debug
logger.file, err_msg = fs.open(path, util.trinary(logger.mode == MODE.APPEND, "a", "w"))
_log.path = path
_log.mode = write_mode
_log.debug = include_debug
_log.file, err_msg = fs.open(path, util.trinary(_log.mode == MODE.APPEND, "a", "w"))
if dmesg_redirect then
logger.dmesg_out = dmesg_redirect
_log.dmesg_out = dmesg_redirect
else
logger.dmesg_out = term.current()
_log.dmesg_out = term.current()
end
-- check for space issues
local out_of_space = check_out_of_space(err_msg)
-- try to handle problems
if logger.file == nil or out_of_space then
if _log.file == nil or out_of_space then
if out_of_space then
if fs.exists(logger.path) then
fs.delete(logger.path)
if fs.exists(_log.path) then
fs.delete(_log.path)
logger.file, err_msg = fs.open(path, util.trinary(logger.mode == MODE.APPEND, "a", "w"))
_log.file, err_msg = fs.open(path, util.trinary(_log.mode == MODE.APPEND, "a", "w"))
if logger.file then
logger.file.writeLine(os.date(TIME_FMT) .. WRN_TAG .. "init recycled log file")
logger.file.flush()
if _log.file then
_log.file.writeLine(os.date(TIME_FMT) .. WRN_TAG .. "init recycled log file")
_log.file.flush()
else error("failed to setup the log file: " .. err_msg) end
else error("failed to make space for the log file, please delete unused files") end
else error("unexpected error setting up the log file: " .. err_msg) end
end
logger.not_ready = false
_log.not_ready = false
end
-- close the log file handle
function log.close() logger.file.close() end
function log.close() _log.file.close() end
-- direct dmesg output to a monitor/window
---@param window Window window or terminal reference
function log.direct_dmesg(window) logger.dmesg_out = window end
function log.direct_dmesg(window) _log.dmesg_out = window end
-- dmesg style logging for boot because I like linux-y things
---@param msg any message
@@ -142,7 +142,7 @@ function log.dmesg(msg, tag, tag_color)
tag = util.strval(tag or "")
local t_stamp = string.format("%12.2f", os.clock())
local out = logger.dmesg_out
local out = _log.dmesg_out
if out ~= nil then
local out_w, out_h = out.getSize()
@@ -180,7 +180,7 @@ function log.dmesg(msg, tag, tag_color)
if cur_y == out_h then
out.scroll(1)
out.setCursorPos(1, cur_y)
logger.dmesg_scroll_count = logger.dmesg_scroll_count + 1
_log.dmesg_scroll_count = _log.dmesg_scroll_count + 1
else
out.setCursorPos(1, cur_y + 1)
end
@@ -216,7 +216,7 @@ function log.dmesg(msg, tag, tag_color)
if cur_y == out_h then
out.scroll(1)
out.setCursorPos(1, cur_y)
logger.dmesg_scroll_count = logger.dmesg_scroll_count + 1
_log.dmesg_scroll_count = _log.dmesg_scroll_count + 1
else
out.setCursorPos(1, cur_y + 1)
end
@@ -225,9 +225,9 @@ function log.dmesg(msg, tag, tag_color)
out.write(lines[i])
end
logger.dmesg_restore_coord = { out.getCursorPos() }
_log.dmesg_restore_coord = { out.getCursorPos() }
_log{"[", t_stamp, "] [", tag, "] ", msg}
write_log{"[", t_stamp, "] [", tag, "] ", msg}
end
return ts_coord
@@ -241,9 +241,9 @@ end
---@return function update, function done
function log.dmesg_working(msg, tag, tag_color)
local ts_coord = log.dmesg(msg, tag, tag_color)
local initial_scroll = logger.dmesg_scroll_count
local initial_scroll = _log.dmesg_scroll_count
local out = logger.dmesg_out
local out = _log.dmesg_out
local width = (ts_coord.x2 - ts_coord.x1) + 1
if out ~= nil then
@@ -252,7 +252,7 @@ function log.dmesg_working(msg, tag, tag_color)
local counter = 0
local function update(sec_remaining)
local new_y = ts_coord.y - (logger.dmesg_scroll_count - initial_scroll)
local new_y = ts_coord.y - (_log.dmesg_scroll_count - initial_scroll)
if new_y < 1 then return end
local time = util.sprintf("%ds", sec_remaining)
@@ -280,11 +280,11 @@ function log.dmesg_working(msg, tag, tag_color)
counter = counter + 1
out.setCursorPos(table.unpack(logger.dmesg_restore_coord))
out.setCursorPos(table.unpack(_log.dmesg_restore_coord))
end
local function done(ok)
local new_y = ts_coord.y - (logger.dmesg_scroll_count - initial_scroll)
local new_y = ts_coord.y - (_log.dmesg_scroll_count - initial_scroll)
if new_y < 1 then return end
out.setCursorPos(ts_coord.x1, new_y)
@@ -299,7 +299,7 @@ function log.dmesg_working(msg, tag, tag_color)
out.setTextColor(initial_color)
out.setCursorPos(table.unpack(logger.dmesg_restore_coord))
out.setCursorPos(table.unpack(_log.dmesg_restore_coord))
end
return update, done
@@ -312,28 +312,28 @@ end
---@param msg any message
---@param trace? boolean include file trace
function log.debug(msg, trace)
if logger.debug then
if _log.debug then
if trace then
local info = debug.getinfo(2)
if info.name ~= nil then
_log{DBG_TAG, info.short_src, COLON, info.name, FUNC, info.currentline, ARROW, msg}
write_log{DBG_TAG, info.short_src, COLON, info.name, FUNC, info.currentline, ARROW, msg}
else
_log{DBG_TAG, info.short_src, COLON, info.currentline, ARROW, msg}
write_log{DBG_TAG, info.short_src, COLON, info.currentline, ARROW, msg}
end
else
_log{DBG_TAG, msg}
write_log{DBG_TAG, msg}
end
end
end
-- log info messages
---@param msg any message
function log.info(msg) _log{INF_TAG, msg} end
function log.info(msg) write_log{INF_TAG, msg} end
-- log warning messages
---@param msg any message
function log.warning(msg) _log{WRN_TAG, msg} end
function log.warning(msg) write_log{WRN_TAG, msg} end
-- log error messages
---@param msg any message
@@ -343,17 +343,17 @@ function log.error(msg, trace)
local info = debug.getinfo(2)
if info.name ~= nil then
_log{ERR_TAG, info.short_src, COLON, info.name, FUNC, info.currentline, ARROW, msg}
write_log{ERR_TAG, info.short_src, COLON, info.name, FUNC, info.currentline, ARROW, msg}
else
_log{ERR_TAG, info.short_src, COLON, info.currentline, ARROW, msg}
write_log{ERR_TAG, info.short_src, COLON, info.currentline, ARROW, msg}
end
else
_log{ERR_TAG, msg}
write_log{ERR_TAG, msg}
end
end
-- log fatal errors
---@param msg any message
function log.fatal(msg) _log{FTL_TAG, msg} end
function log.fatal(msg) write_log{FTL_TAG, msg} end
return log