diff --git a/.gitignore b/.gitignore index 8b7ca09..214f149 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ cabal.sandbox.config .envrc dist-newstyle/ result/ +logs/ diff --git a/app/Main.hs b/app/Main.hs index 388ffad..d586f83 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -37,6 +37,7 @@ import System.Console.GetOpt ( ArgDescr(..) , getOpt , usageInfo ) +import System.Directory ( createDirectoryIfMissing ) import System.Environment ( getArgs ) import System.IO ( stdout ) import Web.Scotty ( get @@ -54,6 +55,7 @@ opts :: [OptDescr (Config -> Config)] opts = [ Option [] ["help"] (NoArg (set cHelp True)) "Show usage info" , Option [] ["port"] (ReqArg (set cPort . read) "PORT") "Port to run on" + , Option [] ["path"] (ReqArg (set cLogPath) "PATH") "Path to save log files to, default is 'logs'" , Option [] ["interval"] (ReqArg (set cInterval . (1000000 *) . read) "INTERVAL (s)") @@ -71,6 +73,7 @@ main = (recreateConfig . getOpt Permute opts <$> getArgs) >>= \case (config , _ , _ ) -> do upd <- newEmptyMVar -- putMVar when to update viewRef <- createViewReference + createDirectoryIfMissing True (_cLogPath config) -- In the list there are three items running concurrently: -- 1. Timer that sends a signal to the updater when it's time to update diff --git a/mat-chalmers.cabal b/mat-chalmers.cabal index 6a578b7..6c3b731 100644 --- a/mat-chalmers.cabal +++ b/mat-chalmers.cabal @@ -74,6 +74,7 @@ executable mat-chalmers , time >= 1.12.2 && < 1.13 , wai-extra >= 3.1.14 && < 4.0 , wai-middleware-static-embedded == 0.1.0.0 + , directory >= 1.3.5.0 && < 1.4 , async >= 2.2.5 && <= 3.0 default-language: GHC2021 diff --git a/src/Config.hs b/src/Config.hs index 3738627..4120e1f 100644 --- a/src/Config.hs +++ b/src/Config.hs @@ -13,13 +13,14 @@ data Config = Config , _cNextDayHour :: !Int , _cInterval :: !Int , _cPort :: !Int + , _cLogPath :: !String } deriving (Eq, Show) makeLenses ''Config defaultConfig :: Config -defaultConfig = Config False 14 (1000000 * 60 * 30) 5007 +defaultConfig = Config False 14 (1000000 * 60 * 30) 5007 "logs" -- | Create a Config we can touch -- diff --git a/src/Model.hs b/src/Model.hs index 36a0049..0486ca9 100644 --- a/src/Model.hs +++ b/src/Model.hs @@ -31,18 +31,14 @@ import Data.IORef ( IORef , writeIORef ) import Data.Foldable ( for_ ) -import Data.Text.Lazy ( fromStrict - , pack - ) -import Prettyprinter ( Doc - , pretty - ) +import Data.Text.Lazy ( fromStrict ) +import Prettyprinter ( Doc ) import Data.AffineSpace ( (.+^) ) import Data.Thyme ( _localDay , _localTimeOfDay , _todHour , _zonedTimeToLocalTime - , getZonedTime + , getZonedTime, getCurrentTime ) import Lens.Micro.Platform ( (^.) , (&) @@ -56,6 +52,7 @@ import Model.Types import Model.Karen import Model.Wijkanders import Model.Linsen +import Text.Printf (printf) -- | Refreshes menus. -- The refresh function evaluates to `Some monad m => m (View model, Update signal)`, @@ -112,7 +109,9 @@ update = do for_ rest $ \r -> case menu r of Left e -> - logMessage =<< timestamp (pretty $ name r <> ": " <> pack (show e)) + asks _cLogPath >>= \path -> + liftIO getCurrentTime >>= + liftIO . flip writeFile (show e) . flip (printf "%s/%s%s.txt" path) (name r) . show Right _ -> pure () return (View rest textday d) diff --git a/src/Model/Linsen.hs b/src/Model/Linsen.hs index 14dd0e0..70beead 100644 --- a/src/Model/Linsen.hs +++ b/src/Model/Linsen.hs @@ -118,11 +118,11 @@ parse day = (v:_) -> pure v) >=> (.: "text") >=> \s -> if - pure day == - parseTime swedishTimeLocale "%A %d-%m-%Y" s && - length v' >= 9 + pure day == parseTime swedishTimeLocale "%A %d-%m-%Y" s + then if length v' >= 9 then pure v' - else pure mempty)) + else pure mempty + else fail "Unable to parse day")) >=> menuParser ) )