Skip to content

Commit

Permalink
First attempt at logging to files instead of std out
Browse files Browse the repository at this point in the history
  • Loading branch information
The1Penguin committed Sep 25, 2024
1 parent 7db84d5 commit f7d35eb
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ cabal.sandbox.config
.envrc
dist-newstyle/
result/
logs/
3 changes: 3 additions & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)")
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions mat-chalmers.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
--
Expand Down
15 changes: 7 additions & 8 deletions src/Model.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ( (^.)
, (&)
Expand All @@ -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)`,
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/Model/Linsen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
Expand Down

0 comments on commit f7d35eb

Please sign in to comment.