From c805b6e0c54aa71fcdb59d487f0d442d0e7e13cc Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Fri, 29 Apr 2022 13:32:37 -0400 Subject: [PATCH] log init function to set path and write mode --- coordinator/startup.lua | 19 ++++++++++++++----- reactor-plc/startup.lua | 4 +++- rtu/startup.lua | 4 +++- scada-common/log.lua | 29 +++++++++++++++++++++++------ supervisor/startup.lua | 4 +++- 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/coordinator/startup.lua b/coordinator/startup.lua index 20be7a3..a3f12d4 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -10,18 +10,27 @@ os.loadAPI("scada-common/comms.lua") os.loadAPI("coordinator/config.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 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() -local modem = ppm.get_device("modem") - -print("| SCADA Coordinator - " .. COORDINATOR_VERSION .. " |") +local modem = ppm.get_wireless_modem() -- we need a modem if modem == nil then - print("Please connect a modem.") + println("please connect a wireless modem") return end diff --git a/reactor-plc/startup.lua b/reactor-plc/startup.lua index 37ac6de..2952191 100644 --- a/reactor-plc/startup.lua +++ b/reactor-plc/startup.lua @@ -12,13 +12,15 @@ os.loadAPI("config.lua") os.loadAPI("plc.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 println = util.println local print_ts = util.print_ts local println_ts = util.println_ts +log.init("/log.txt", log.MODE.APPEND) + log._info("========================================") log._info("BOOTING reactor-plc.startup " .. R_PLC_VERSION) log._info("========================================") diff --git a/rtu/startup.lua b/rtu/startup.lua index aad3483..6220ad1 100644 --- a/rtu/startup.lua +++ b/rtu/startup.lua @@ -19,13 +19,15 @@ os.loadAPI("dev/boiler_rtu.lua") os.loadAPI("dev/imatrix_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 println = util.println local print_ts = util.print_ts local println_ts = util.println_ts +log.init("/log.txt", log.MODE.APPEND) + log._info("========================================") log._info("BOOTING rtu.startup " .. RTU_VERSION) log._info("========================================") diff --git a/scada-common/log.lua b/scada-common/log.lua index cf493eb..1aafda3 100644 --- a/scada-common/log.lua +++ b/scada-common/log.lua @@ -5,10 +5,16 @@ -- 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) -local LOG_DEBUG = true -local LOG_PATH = "/log.txt" +MODE = { + 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 stamped = os.date("[%c] ") .. msg @@ -20,7 +26,7 @@ local _log = function (msg) end) -- 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 result == "Out of space" then @@ -33,8 +39,8 @@ local _log = function (msg) if delete_log then -- delete the old log file and open a new one file_handle.close() - fs.delete(LOG_PATH) - file_handle = fs.open(LOG_PATH, "a") + fs.delete(log_path) + init(log_path, mode) -- leave a message local notif = os.date("[%c] ") .. "recycled log file" @@ -44,6 +50,17 @@ local _log = function (msg) 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) if LOG_DEBUG then local dbg_info = "" diff --git a/supervisor/startup.lua b/supervisor/startup.lua index 917280d..9cf704a 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -18,13 +18,15 @@ os.loadAPI("session/svsessions.lua") os.loadAPI("supervisor.lua") -local SUPERVISOR_VERSION = "alpha-v0.1.8" +local SUPERVISOR_VERSION = "alpha-v0.1.9" local print = util.print local println = util.println local print_ts = util.print_ts local println_ts = util.println_ts +log.init("/log.txt", log.MODE.APPEND) + log._info("========================================") log._info("BOOTING supervisor.startup " .. SUPERVISOR_VERSION) log._info("========================================")