From d1d4a2c05b02b0aef65f6fc9be7d7d40d75a5331 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Tue, 31 Jan 2017 13:11:52 +1100 Subject: Improved logging/verbosity noting start/finish times and bulk exclusions --- src/Election.hs | 15 +++++++++++---- src/main.hs | 22 ++++++++++++++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Election.hs b/src/Election.hs index a475f0e..8a0e4d0 100644 --- a/src/Election.hs +++ b/src/Election.hs @@ -80,8 +80,8 @@ data Transfer = Transfer -createElection :: FilePath -> Sen.SenateCounter -> Int -> Bool -> IO Election -createElection outDir counter numToElect verbosity = do +createElection :: FilePath -> FilePath -> Sen.SenateCounter -> Int -> Bool -> IO Election +createElection outDir mainLog counter numToElect verbosity = do entries <- mapM (candToEntry counter) (Sen.getBallot counter) return (Election { getEntries = entries @@ -89,7 +89,7 @@ createElection outDir counter numToElect verbosity = do , getLogDir = outDir , getTotalPapers = Sen.getTotal counter , getQuota = droopQuota (Sen.getTotal counter) numToElect - , getMainLog = outDir ++ "/" ++ "log.txt" + , getMainLog = mainLog , getNextLogNum = 1 , getSeats = numToElect , getVacancies = numToElect @@ -360,7 +360,14 @@ excludeCandidates e = do let v1 = v + i n1 = n + 1 if (v1 > appliedBreakpoint) - then n > 0 ? ET.left e $ ET.left r + then if (n > 0) + then do + MIO.liftIO $ Con.when (n > 1) $ do + let logmsg = "Bulk exclusion at logfile #" ++ show (getNextLogNum e) + IO.appendFile (getMainLog e) (logmsg ++ "\n") + Con.when (isVerbose e) (IO.hPutStrLn IO.stderr logmsg) + ET.left e + else ET.left r else excludeLoop n1 v1 r if (length running > 0 && all (< getQuota e) (map getTotalVotes running)) diff --git a/src/main.hs b/src/main.hs index 8ed8bb8..c66dc6e 100644 --- a/src/main.hs +++ b/src/main.hs @@ -18,6 +18,7 @@ import qualified System.Exit as Ex import qualified System.Directory as Dir import qualified System.IO as IO import qualified Control.Monad as Con +import qualified Data.Time.Clock as Time import qualified Data.Maybe as Maybe import qualified Counter as Sen import qualified Candidate as Cand @@ -174,20 +175,37 @@ main = do Ex.die ("Invalid state/territory or state/territory not provided.\n\n" ++ furtherHelp) + -- set up logging + Dir.createDirectory outputDir + startTime <- Time.getCurrentTime + let mainLog = outputDir ++ "/" ++ "log.txt" + startmsg = "Started election count at " ++ show startTime ++ "\n" + IO.appendFile mainLog startmsg + Con.when (isVerbose options) $ IO.hPutStrLn IO.stderr startmsg + + -- set up the election processing Con.when (isVerbose options) $ IO.hPutStrLn IO.stderr "Reading candidate data..." (aboveBallot, belowBallot) <- Cand.readCandidates candidateFile state Con.when (isVerbose options) $ IO.hPutStrLn IO.stderr "Reading preference data..." counter <- Sen.createSenateCounter preferenceFile aboveBallot belowBallot Con.when (isVerbose options) $ IO.hPutStrLn IO.stderr "Done.\n" - Dir.createDirectory outputDir Con.when (isVerbose options) $ IO.hPutStrLn IO.stderr "Setting up election..." - election <- Elt.createElection outputDir counter numToElect (isVerbose options) + election <- Elt.createElection outputDir mainLog counter numToElect (isVerbose options) Con.when (isVerbose options) $ IO.hPutStrLn IO.stderr "Done.\n" -- run the show Con.when (isVerbose options) $ IO.hPutStrLn IO.stderr "Running...\n" Elt.doCount election + Con.when (isVerbose options) $ IO.hPutStr IO.stderr "\n" + + + -- finish up logging + endTime <- Time.getCurrentTime + let endmsg = "Finished election count at " ++ show endTime ++ "\n" + elapsedmsg = show (Time.diffUTCTime endTime startTime) ++ " elapsed\n" + IO.appendFile mainLog (endmsg ++ elapsedmsg) + Con.when (isVerbose options) $ IO.hPutStrLn IO.stderr (endmsg ++ elapsedmsg) -- cgit