From 0e034f01c189e1e7bedb7e8897680b8f322ba301 Mon Sep 17 00:00:00 2001 From: onefang Date: Tue, 24 Dec 2019 19:36:45 +1000 Subject: Refactor logging. --- apt-panopticommon.lua | 16 +++++++++++++++- apt-panopticon.lua | 14 ++------------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/apt-panopticommon.lua b/apt-panopticommon.lua index d8ab129..54e2be1 100644 --- a/apt-panopticommon.lua +++ b/apt-panopticommon.lua @@ -315,7 +315,18 @@ APT.results = {} APT.logFile = nil APT.html = false -APT.logPre = function() +APT.logName = function(host, a2, a3) + local name = host + if nil ~= a2 then name = name .. "_" .. a2 end + if nil ~= a3 then name = name .. "_" .. a3 end + return 'results/LOG_' .. name .. '.html' +end + +APT.logOpen = function(host, a2, a3) + local name = APT.logName(host, a2, a3) + if APT.checkFile(name) then return false end + APT.logFile, e = io.open(name, "a+") + if nil == APT.logFile then C('opening log file (' .. name .. ') - ' .. e); return false end if nil ~= APT.logFile then APT.logFile:write("\n") APT.logFile:write("\n") @@ -323,10 +334,13 @@ APT.logPre = function() APT.logFile:write(APT.dumpTable(APT.args, '', 'Arguments')) APT.logFile:write("\n") end + return true end + APT.logPost = function() if nil ~= APT.logFile then APT.logFile:write(" \n") + APT.logFile:close() end end diff --git a/apt-panopticon.lua b/apt-panopticon.lua index 45a591f..649346a 100755 --- a/apt-panopticon.lua +++ b/apt-panopticon.lua @@ -922,13 +922,7 @@ if 0 < #arg then if APT.origin or APT.redir then APT.results["IPs"] = gatherIPs(pu.host) end - local logname = pu.host - if nil ~= arg[2] then logname = logname .. "_" .. arg[2] end - if nil ~= arg[3] then logname = logname .. "_" .. arg[3] end - if APT.checkFile("results/LOG_" .. logname .. ".html") then return end - APT.logFile, e = io.open("results/LOG_" .. logname .. ".html", "a+") - if nil == APT.logFile then C("opening log file - " .. e); return end - APT.logPre() + if not APT.logOpen(pu.host, arg[2], arg[3]) then return end I("Starting tests for " .. arg[1] .. " with these tests - " .. table.concat(APT.options.tests.value, ", ")) if nil ~= arg[2] then I(" Using IP " .. arg[2]); ip = arg[2] end if nil ~= arg[3] then I(" Using file " .. arg[3]); end @@ -1005,7 +999,6 @@ os.execute('sleep 1') -- Wait for things to start up before checking for them. end APT.logPost() - APT.logFile:close() else local fadt = io.popen("ls -dl results_old 2>/dev/null | cut -d '>' -f 2 | cut -d ' ' -f 2") local adt = fadt:read('*l') @@ -1024,9 +1017,7 @@ else os.execute("rm -f results/*.txt 2>/dev/null") end - APT.logFile, e = io.open("results/LOG_apt-panopticon.html", "a+") - if nil == APT.logFile then C("opening log file - " .. e); return end - APT.logPre() + if not APT.logOpen('apt-panopticon') then return end I("Starting tests " .. table.concat(APT.options.tests.value, ", ")) os.execute("mkdir -p results") APT.mirrors = getMirrors() @@ -1069,5 +1060,4 @@ os.execute('sleep 1') -- Wait for things to start up before checking for them. I('Total run time was ' .. (os.time() - now) .. ' seconds.') APT.logPost() - APT.logFile:close() end -- cgit v1.1