#222 added debug log enable to configs

This commit is contained in:
Mikayla 2023-05-10 20:57:23 +00:00
parent 032284e90d
commit 50c0a4a3eb
11 changed files with 47 additions and 32 deletions

View File

@ -28,5 +28,7 @@ config.LOG_PATH = "/log.txt"
-- 0 = APPEND (adds to existing file on start)
-- 1 = NEW (replaces existing file on start)
config.LOG_MODE = 0
-- true to log verbose debug messages
config.LOG_DEBUG = false
return config

View File

@ -50,6 +50,7 @@ cfv.assert_type_num(config.SOUNDER_VOLUME)
cfv.assert_type_bool(config.TIME_24_HOUR)
cfv.assert_type_str(config.LOG_PATH)
cfv.assert_type_int(config.LOG_MODE)
cfv.assert_type_bool(config.LOG_DEBUG)
assert(cfv.valid(), "bad config file: missing/invalid fields")
@ -57,7 +58,7 @@ assert(cfv.valid(), "bad config file: missing/invalid fields")
-- log init
----------------------------------------
log.init(config.LOG_PATH, config.LOG_MODE)
log.init(config.LOG_PATH, config.LOG_MODE, config.LOG_DEBUG)
log.info("========================================")
log.info("BOOTING coordinator.startup " .. COORDINATOR_VERSION)

View File

@ -17,5 +17,7 @@ config.LOG_PATH = "/log.txt"
-- 0 = APPEND (adds to existing file on start)
-- 1 = NEW (replaces existing file on start)
config.LOG_MODE = 0
-- true to log verbose debug messages
config.LOG_DEBUG = false
return config

View File

@ -36,6 +36,7 @@ cfv.assert_type_num(config.COMMS_TIMEOUT)
cfv.assert_min(config.COMMS_TIMEOUT, 2)
cfv.assert_type_str(config.LOG_PATH)
cfv.assert_type_int(config.LOG_MODE)
cfv.assert_type_bool(config.LOG_DEBUG)
assert(cfv.valid(), "bad config file: missing/invalid fields")
@ -43,7 +44,7 @@ assert(cfv.valid(), "bad config file: missing/invalid fields")
-- log init
----------------------------------------
log.init(config.LOG_PATH, config.LOG_MODE)
log.init(config.LOG_PATH, config.LOG_MODE, config.LOG_DEBUG)
log.info("========================================")
log.info("BOOTING pocket.startup " .. POCKET_VERSION)

View File

@ -24,5 +24,7 @@ config.LOG_PATH = "/log.txt"
-- 0 = APPEND (adds to existing file on start)
-- 1 = NEW (replaces existing file on start)
config.LOG_MODE = 0
-- true to log verbose debug messages
config.LOG_DEBUG = false
return config

View File

@ -38,6 +38,7 @@ cfv.assert_type_num(config.COMMS_TIMEOUT)
cfv.assert_min(config.COMMS_TIMEOUT, 2)
cfv.assert_type_str(config.LOG_PATH)
cfv.assert_type_int(config.LOG_MODE)
cfv.assert_type_bool(config.LOG_DEBUG)
assert(cfv.valid(), "bad config file: missing/invalid fields")
@ -54,7 +55,7 @@ end
-- log init
----------------------------------------
log.init(config.LOG_PATH, config.LOG_MODE)
log.init(config.LOG_PATH, config.LOG_MODE, config.LOG_DEBUG)
log.info("========================================")
log.info("BOOTING reactor-plc.startup " .. R_PLC_VERSION)

View File

@ -17,6 +17,8 @@ config.LOG_PATH = "/log.txt"
-- 0 = APPEND (adds to existing file on start)
-- 1 = NEW (replaces existing file on start)
config.LOG_MODE = 0
-- true to log verbose debug messages
config.LOG_DEBUG = false
-- RTU peripheral devices (named: side/network device name)
config.RTU_DEVICES = {

View File

@ -49,6 +49,7 @@ cfv.assert_type_num(config.COMMS_TIMEOUT)
cfv.assert_min(config.COMMS_TIMEOUT, 2)
cfv.assert_type_str(config.LOG_PATH)
cfv.assert_type_int(config.LOG_MODE)
cfv.assert_type_bool(config.LOG_DEBUG)
cfv.assert_type_table(config.RTU_DEVICES)
cfv.assert_type_table(config.RTU_REDSTONE)
assert(cfv.valid(), "bad config file: missing/invalid fields")
@ -57,7 +58,7 @@ assert(cfv.valid(), "bad config file: missing/invalid fields")
-- log init
----------------------------------------
log.init(config.LOG_PATH, config.LOG_MODE)
log.init(config.LOG_PATH, config.LOG_MODE, config.LOG_DEBUG)
log.info("========================================")
log.info("BOOTING rtu.startup " .. RTU_VERSION)

View File

@ -15,12 +15,10 @@ local MODE = {
log.MODE = MODE
-- whether to log debug messages or not
local LOG_DEBUG = true
local log_sys = {
local logger = {
path = "/log.txt",
mode = MODE.APPEND,
debug = false,
file = nil,
dmesg_out = nil
}
@ -41,8 +39,8 @@ local function _log(msg)
-- attempt to write log
local status, result = pcall(function ()
log_sys.file.writeLine(stamped)
log_sys.file.flush()
logger.file.writeLine(stamped)
logger.file.flush()
end)
-- if we don't have space, we need to create a new log file
@ -57,18 +55,18 @@ local function _log(msg)
end
end
if out_of_space or (free_space(log_sys.path) < 100) then
if out_of_space or (free_space(logger.path) < 100) then
-- delete the old log file before opening a new one
log_sys.file.close()
fs.delete(log_sys.path)
logger.file.close()
fs.delete(logger.path)
-- re-init logger and pass dmesg_out so that it doesn't change
log.init(log_sys.path, log_sys.mode, log_sys.dmesg_out)
log.init(logger.path, logger.mode, logger.debug, logger.dmesg_out)
-- leave a message
log_sys.file.writeLine(time_stamp .. "recycled log file")
log_sys.file.writeLine(stamped)
log_sys.file.flush()
logger.file.writeLine(time_stamp .. "recycled log file")
logger.file.writeLine(stamped)
logger.file.flush()
end
end
@ -78,33 +76,35 @@ end
-- initialize logger
---@param path string file path
---@param write_mode MODE
---@param write_mode MODE file write mode
---@param include_debug boolean whether or not to include debug logs
---@param dmesg_redirect? table terminal/window to direct dmesg to
function log.init(path, write_mode, dmesg_redirect)
log_sys.path = path
log_sys.mode = write_mode
function log.init(path, write_mode, include_debug, dmesg_redirect)
logger.path = path
logger.mode = write_mode
logger.debug = include_debug
if log_sys.mode == MODE.APPEND then
log_sys.file = fs.open(path, "a")
if logger.mode == MODE.APPEND then
logger.file = fs.open(path, "a")
else
log_sys.file = fs.open(path, "w")
logger.file = fs.open(path, "w")
end
if dmesg_redirect then
log_sys.dmesg_out = dmesg_redirect
logger.dmesg_out = dmesg_redirect
else
log_sys.dmesg_out = term.current()
logger.dmesg_out = term.current()
end
end
-- close the log file handle
function log.close()
log_sys.file.close()
logger.file.close()
end
-- direct dmesg output to a monitor/window
---@param window table window or terminal reference
function log.direct_dmesg(window) log_sys.dmesg_out = window end
function log.direct_dmesg(window) logger.dmesg_out = window end
-- dmesg style logging for boot because I like linux-y things
---@param msg string message
@ -120,7 +120,7 @@ function log.dmesg(msg, tag, tag_color)
tag = util.strval(tag)
local t_stamp = string.format("%12.2f", os.clock())
local out = log_sys.dmesg_out
local out = logger.dmesg_out
if out ~= nil then
local out_w, out_h = out.getSize()
@ -216,7 +216,7 @@ end
function log.dmesg_working(msg, tag, tag_color)
local ts_coord = log.dmesg(msg, tag, tag_color)
local out = log_sys.dmesg_out
local out = logger.dmesg_out
local width = (ts_coord.x2 - ts_coord.x1) + 1
if out ~= nil then
@ -275,7 +275,7 @@ end
---@param msg string message
---@param trace? boolean include file trace
function log.debug(msg, trace)
if LOG_DEBUG then
if logger.debug then
local dbg_info = ""
if trace then

View File

@ -28,5 +28,7 @@ config.LOG_PATH = "/log.txt"
-- 0 = APPEND (adds to existing file on start)
-- 1 = NEW (replaces existing file on start)
config.LOG_MODE = 0
-- true to log verbose debug messages
config.LOG_DEBUG = false
return config

View File

@ -40,6 +40,7 @@ cfv.assert_type_int(config.NUM_REACTORS)
cfv.assert_type_table(config.REACTOR_COOLING)
cfv.assert_type_str(config.LOG_PATH)
cfv.assert_type_int(config.LOG_MODE)
cfv.assert_type_bool(config.LOG_DEBUG)
assert(cfv.valid(), "bad config file: missing/invalid fields")
@ -61,7 +62,7 @@ end
-- log init
----------------------------------------
log.init(config.LOG_PATH, config.LOG_MODE)
log.init(config.LOG_PATH, config.LOG_MODE, config.LOG_DEBUG)
log.info("========================================")
log.info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION)