log init function to set path and write mode

This commit is contained in:
Mikayla Fischler 2022-04-29 13:32:37 -04:00
parent 3587352219
commit c805b6e0c5
5 changed files with 46 additions and 14 deletions

View File

@ -10,18 +10,27 @@ os.loadAPI("scada-common/comms.lua")
os.loadAPI("coordinator/config.lua") os.loadAPI("coordinator/config.lua")
os.loadAPI("coordinator/coordinator.lua") os.loadAPI("coordinator/coordinator.lua")
local COORDINATOR_VERSION = "alpha-v0.1.0" local COORDINATOR_VERSION = "alpha-v0.1.1"
local print = util.print
local println = util.println
local print_ts = util.print_ts local print_ts = util.print_ts
local println_ts = util.println_ts
log.init("/log.txt", log.MODE.APPEND)
log._info("========================================")
log._info("BOOTING coordinator.startup " .. COORDINATOR_VERSION)
log._info("========================================")
println(">> RTU " .. COORDINATOR_VERSION .. " <<")
-- mount connected devices
ppm.mount_all() ppm.mount_all()
local modem = ppm.get_device("modem") local modem = ppm.get_wireless_modem()
print("| SCADA Coordinator - " .. COORDINATOR_VERSION .. " |")
-- we need a modem -- we need a modem
if modem == nil then if modem == nil then
print("Please connect a modem.") println("please connect a wireless modem")
return return
end end

View File

@ -12,13 +12,15 @@ os.loadAPI("config.lua")
os.loadAPI("plc.lua") os.loadAPI("plc.lua")
os.loadAPI("threads.lua") os.loadAPI("threads.lua")
local R_PLC_VERSION = "alpha-v0.4.10" local R_PLC_VERSION = "alpha-v0.4.11"
local print = util.print local print = util.print
local println = util.println local println = util.println
local print_ts = util.print_ts local print_ts = util.print_ts
local println_ts = util.println_ts local println_ts = util.println_ts
log.init("/log.txt", log.MODE.APPEND)
log._info("========================================") log._info("========================================")
log._info("BOOTING reactor-plc.startup " .. R_PLC_VERSION) log._info("BOOTING reactor-plc.startup " .. R_PLC_VERSION)
log._info("========================================") log._info("========================================")

View File

@ -19,13 +19,15 @@ os.loadAPI("dev/boiler_rtu.lua")
os.loadAPI("dev/imatrix_rtu.lua") os.loadAPI("dev/imatrix_rtu.lua")
os.loadAPI("dev/turbine_rtu.lua") os.loadAPI("dev/turbine_rtu.lua")
local RTU_VERSION = "alpha-v0.4.8" local RTU_VERSION = "alpha-v0.4.9"
local print = util.print local print = util.print
local println = util.println local println = util.println
local print_ts = util.print_ts local print_ts = util.print_ts
local println_ts = util.println_ts local println_ts = util.println_ts
log.init("/log.txt", log.MODE.APPEND)
log._info("========================================") log._info("========================================")
log._info("BOOTING rtu.startup " .. RTU_VERSION) log._info("BOOTING rtu.startup " .. RTU_VERSION)
log._info("========================================") log._info("========================================")

View File

@ -5,10 +5,16 @@
-- we use extra short abbreviations since computer craft screens are very small -- we use extra short abbreviations since computer craft screens are very small
-- underscores are used since some of these names are used elsewhere (e.g. 'debug' is a lua table) -- underscores are used since some of these names are used elsewhere (e.g. 'debug' is a lua table)
local LOG_DEBUG = true MODE = {
local LOG_PATH = "/log.txt" APPEND = 0,
NEW = 1
}
local file_handle = fs.open(LOG_PATH, "a") local LOG_DEBUG = true
local log_path = "/log.txt"
local mode = MODE.APPEND
local file_handle = nil
local _log = function (msg) local _log = function (msg)
local stamped = os.date("[%c] ") .. msg local stamped = os.date("[%c] ") .. msg
@ -20,7 +26,7 @@ local _log = function (msg)
end) end)
-- if we don't have much space, we need to create a new log file -- if we don't have much space, we need to create a new log file
local delete_log = fs.getFreeSpace(LOG_PATH) < 100 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
@ -33,8 +39,8 @@ local _log = function (msg)
if delete_log then if delete_log then
-- delete the old log file and open a new one -- delete the old log file and open a new one
file_handle.close() file_handle.close()
fs.delete(LOG_PATH) fs.delete(log_path)
file_handle = fs.open(LOG_PATH, "a") init(log_path, mode)
-- leave a message -- leave a message
local notif = os.date("[%c] ") .. "recycled log file" local notif = os.date("[%c] ") .. "recycled log file"
@ -44,6 +50,17 @@ local _log = function (msg)
end end
end end
function init(path, write_mode)
log_path = path
mode = write_mode
if mode == MODE.APPEND then
file_handle = fs.open(path, "a")
else
file_handle = fs.open(path, "w+")
end
end
function _debug(msg, trace) function _debug(msg, trace)
if LOG_DEBUG then if LOG_DEBUG then
local dbg_info = "" local dbg_info = ""

View File

@ -18,13 +18,15 @@ os.loadAPI("session/svsessions.lua")
os.loadAPI("supervisor.lua") os.loadAPI("supervisor.lua")
local SUPERVISOR_VERSION = "alpha-v0.1.8" local SUPERVISOR_VERSION = "alpha-v0.1.9"
local print = util.print local print = util.print
local println = util.println local println = util.println
local print_ts = util.print_ts local print_ts = util.print_ts
local println_ts = util.println_ts local println_ts = util.println_ts
log.init("/log.txt", log.MODE.APPEND)
log._info("========================================") log._info("========================================")
log._info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION) log._info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION)
log._info("========================================") log._info("========================================")