#217 close log files on exit (including crash)

This commit is contained in:
Mikayla Fischler 2023-04-20 21:00:10 -04:00
parent e9788abde7
commit 5db517cedc
7 changed files with 49 additions and 33 deletions

View File

@ -20,7 +20,7 @@ local sounder = require("coordinator.sounder")
local apisessions = require("coordinator.session.apisessions")
local COORDINATOR_VERSION = "v0.13.4"
local COORDINATOR_VERSION = "v0.13.5"
local println = util.println
local println_ts = util.println_ts
@ -389,4 +389,6 @@ if not xpcall(main, crash.handler) then
pcall(renderer.close_ui)
pcall(sounder.stop)
crash.exit()
else
log.close()
end

File diff suppressed because one or more lines are too long

View File

@ -18,7 +18,7 @@ local plc = require("reactor-plc.plc")
local renderer = require("reactor-plc.renderer")
local threads = require("reactor-plc.threads")
local R_PLC_VERSION = "v1.1.12"
local R_PLC_VERSION = "v1.1.13"
local println = util.println
local println_ts = util.println_ts
@ -264,4 +264,4 @@ local function main()
log.info("exited")
end
if not xpcall(main, crash.handler) then crash.exit() end
if not xpcall(main, crash.handler) then crash.exit() else log.close() end

View File

@ -28,7 +28,7 @@ local sna_rtu = require("rtu.dev.sna_rtu")
local sps_rtu = require("rtu.dev.sps_rtu")
local turbinev_rtu = require("rtu.dev.turbinev_rtu")
local RTU_VERSION = "v1.0.0"
local RTU_VERSION = "v1.0.1"
local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
local RTU_UNIT_HW_STATE = databus.RTU_UNIT_HW_STATE
@ -497,4 +497,4 @@ local function main()
log.info("exited")
end
if not xpcall(main, crash.handler) then crash.exit() end
if not xpcall(main, crash.handler) then crash.exit() else log.close() end

View File

@ -39,6 +39,7 @@ end
-- final error print on failed xpcall, app exits here
function crash.exit()
log.close()
util.println("fatal error occured in main application:")
error(err, 0)
end

View File

@ -16,7 +16,7 @@ local MODE = {
log.MODE = MODE
-- whether to log debug messages or not
local LOG_DEBUG = false
local LOG_DEBUG = true
local log_sys = {
path = "/log.txt",
@ -28,30 +28,9 @@ local log_sys = {
---@type function
local free_space = fs.getFreeSpace
-- initialize logger
---@param path string file path
---@param write_mode MODE
---@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
if log_sys.mode == MODE.APPEND then
log_sys.file = fs.open(path, "a")
else
log_sys.file = fs.open(path, "w")
end
if dmesg_redirect then
log_sys.dmesg_out = dmesg_redirect
else
log_sys.dmesg_out = term.current()
end
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
-----------------------
-- PRIVATE FUNCTIONS --
-----------------------
-- private log write function
---@param msg string
@ -93,6 +72,40 @@ local function _log(msg)
end
end
----------------------
-- PUBLIC FUNCTIONS --
----------------------
-- initialize logger
---@param path string file path
---@param write_mode MODE
---@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
if log_sys.mode == MODE.APPEND then
log_sys.file = fs.open(path, "a")
else
log_sys.file = fs.open(path, "w")
end
if dmesg_redirect then
log_sys.dmesg_out = dmesg_redirect
else
log_sys.dmesg_out = term.current()
end
end
-- close the log file handle
function log.close()
log_sys.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
-- dmesg style logging for boot because I like linux-y things
---@param msg string message
---@param tag? string log tag

View File

@ -14,7 +14,7 @@ local supervisor = require("supervisor.supervisor")
local svsessions = require("supervisor.session.svsessions")
local SUPERVISOR_VERSION = "v0.15.3"
local SUPERVISOR_VERSION = "v0.15.4"
local println = util.println
local println_ts = util.println_ts
@ -169,4 +169,4 @@ local function main()
log.info("exited")
end
if not xpcall(main, crash.handler) then crash.exit() end
if not xpcall(main, crash.handler) then crash.exit() else log.close() end