Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt at logging to files instead of std out #135

Merged
merged 7 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 IT EXISTS FOR REAL! 🎉

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'"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, Option [] ["path"] (ReqArg (set cLogPath) "PATH") "Path to save log files to, default is 'logs'"
, Option [] ["path"] (ReqArg (set cLogPath) "PATH") "Path to save log files to, default is 'logs'"

Columns <3

, 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this path relative?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both absolute and relative that is

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It handles both :D 🗡️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AWESOME! I am now a much calmer human.


-- 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
, getZonedTime, getCurrentTime
, getCurrentTime
, getZonedTime

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newlines and lexicographic ordering <3

)
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor indentation fix

Suggested change
import Model.Linsen
import Text.Printf (printf)
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
Loading