Skip to content

Commit 59a5526

Browse files
committed
Move away from monad-logger to RIO.Logger
1 parent 5cc5efa commit 59a5526

26 files changed

Lines changed: 276 additions & 261 deletions

package.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ dependencies:
5555
- echo
5656
- exceptions
5757
- extra
58-
- fast-logger
5958
- file-embed
6059
- filelock
6160
- filepath
@@ -74,7 +73,7 @@ dependencies:
7473
- microlens
7574
- microlens-mtl
7675
- mintty
77-
- monad-logger
76+
- monad-logger # TODO remove dep when persistent drops monad-logger
7877
- mono-traversable
7978
- mtl
8079
- neat-interpolation
@@ -151,6 +150,7 @@ library:
151150
- Path.Extra
152151
- Path.Find
153152
- Paths_stack
153+
- RIO.Logger
154154
- Stack.Build
155155
- Stack.Build.Cache
156156
- Stack.Build.ConstructPlan

src/Data/Aeson/Extended.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ unWarningParser wp = do
100100

101101
-- | Log JSON warnings.
102102
logJSONWarnings
103-
:: MonadLogger m
103+
:: (MonadReader env m, HasLogFunc env, HasCallStack, MonadIO m)
104104
=> FilePath -> [JSONWarning] -> m ()
105105
logJSONWarnings fp =
106106
mapM_ (\w -> logWarn ("Warning: " <> T.pack fp <> ": " <> T.pack (show w)))

src/Data/Store/VersionTagged.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ versionedDecodeFile :: Data a => VersionConfig a -> Q Exp
3737
versionedDecodeFile vc = [e| versionedDecodeFileImpl $(decodeWithVersionQ vc) |]
3838

3939
-- | Write to the given file.
40-
storeEncodeFile :: (Store a, MonadIO m, MonadLogger m, Eq a)
40+
storeEncodeFile :: (Store a, MonadIO m, MonadReader env m, HasCallStack, HasLogFunc env, Eq a)
4141
=> (a -> (Int, Poke ()))
4242
-> Peek a
4343
-> Path Abs File
@@ -55,7 +55,7 @@ storeEncodeFile pokeFunc peekFunc fp x = do
5555
-- | Read from the given file. If the read fails, run the given action and
5656
-- write that back to the file. Always starts the file off with the
5757
-- version tag.
58-
versionedDecodeOrLoadImpl :: (Store a, Eq a, MonadUnliftIO m, MonadLogger m)
58+
versionedDecodeOrLoadImpl :: (Store a, Eq a, MonadUnliftIO m, MonadReader env m, HasCallStack, HasLogFunc env)
5959
=> (a -> (Int, Poke ()))
6060
-> Peek a
6161
-> Path Abs File
@@ -75,7 +75,7 @@ versionedDecodeOrLoadImpl pokeFunc peekFunc fp mx = do
7575
storeEncodeFile pokeFunc peekFunc fp x
7676
return x
7777

78-
versionedDecodeFileImpl :: (Store a, MonadUnliftIO m, MonadLogger m)
78+
versionedDecodeFileImpl :: (Store a, MonadUnliftIO m, MonadReader env m, HasCallStack, HasLogFunc env)
7979
=> Peek a
8080
-> Path loc File
8181
-> m (Maybe a)

src/Network/HTTP/Download.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ import System.FilePath (takeDirectory, (<.>))
4747
-- appropriate destination.
4848
--
4949
-- Throws an exception if things go wrong
50-
download :: (MonadUnliftIO m, MonadLogger m, HasRunner env, MonadReader env m)
50+
download :: HasRunner env
5151
=> Request
5252
-> Path Abs File -- ^ destination
53-
-> m Bool -- ^ Was a downloaded performed (True) or did the file already exist (False)?
53+
-> RIO env Bool -- ^ Was a downloaded performed (True) or did the file already exist (False)?
5454
download req destpath = do
5555
let downloadReq = DownloadRequest
5656
{ drRequest = req
@@ -64,10 +64,10 @@ download req destpath = do
6464
-- | Same as 'download', but will download a file a second time if it is already present.
6565
--
6666
-- Returns 'True' if the file was downloaded, 'False' otherwise
67-
redownload :: (MonadUnliftIO m, MonadLogger m, HasRunner env, MonadReader env m)
67+
redownload :: HasRunner env
6868
=> Request
6969
-> Path Abs File -- ^ destination
70-
-> m Bool
70+
-> RIO env Bool
7171
redownload req0 dest = do
7272
logDebug $ "Downloading " <> decodeUtf8With lenientDecode (path req0)
7373
let destFilePath = toFilePath dest

src/Network/HTTP/Download/Verified.hs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,24 +181,23 @@ hashChecksToZipSink :: MonadThrow m => Request -> [HashCheck] -> ZipSink ByteStr
181181
hashChecksToZipSink req = traverse_ (ZipSink . sinkCheckHash req)
182182

183183
-- 'Control.Retry.recovering' customized for HTTP failures
184-
recoveringHttp :: (MonadUnliftIO m, MonadLogger m, HasRunner env, MonadReader env m)
185-
=> RetryPolicy -> m a -> m a
184+
recoveringHttp :: forall env a. HasRunner env => RetryPolicy -> RIO env a -> RIO env a
186185
recoveringHttp retryPolicy =
187186
#if MIN_VERSION_retry(0,7,0)
188187
helper $ \run -> recovering retryPolicy (handlers run) . const
189188
#else
190189
helper $ \run -> recovering retryPolicy (handlers run)
191190
#endif
192191
where
193-
helper :: (MonadUnliftIO m, HasRunner env, MonadReader env m) => (UnliftIO m -> IO a -> IO a) -> m a -> m a
192+
helper :: (UnliftIO (RIO env) -> IO a -> IO a) -> RIO env a -> RIO env a
194193
helper wrapper action = withUnliftIO $ \run -> wrapper run (unliftIO run action)
195194

196-
handlers :: (MonadLogger m, HasRunner env, MonadReader env m) => UnliftIO m -> [RetryStatus -> Handler IO Bool]
197-
handlers run = [Handler . alwaysRetryHttp (unliftIO run),const $ Handler retrySomeIO]
195+
handlers :: UnliftIO (RIO env) -> [RetryStatus -> Handler IO Bool]
196+
handlers u = [Handler . alwaysRetryHttp u,const $ Handler retrySomeIO]
198197

199-
alwaysRetryHttp :: (MonadLogger m', Monad m, HasRunner env, MonadReader env m') => (m' () -> m ()) -> RetryStatus -> HttpException -> m Bool
200-
alwaysRetryHttp run rs _ = do
201-
run $
198+
alwaysRetryHttp :: UnliftIO (RIO env) -> RetryStatus -> HttpException -> IO Bool
199+
alwaysRetryHttp u rs _ = do
200+
unliftIO u $
202201
prettyWarn $ vcat
203202
[ flow $ unwords
204203
[ "Retry number"
@@ -235,17 +234,18 @@ recoveringHttp retryPolicy =
235234
-- Throws VerifiedDownloadException.
236235
-- Throws IOExceptions related to file system operations.
237236
-- Throws HttpException.
238-
verifiedDownload :: (MonadUnliftIO m, MonadLogger m, HasRunner env, MonadReader env m)
237+
verifiedDownload
238+
:: HasRunner env
239239
=> DownloadRequest
240240
-> Path Abs File -- ^ destination
241-
-> (Maybe Integer -> Sink ByteString IO ()) -- ^ custom hook to observe progress
242-
-> m Bool -- ^ Whether a download was performed
241+
-> (Maybe Integer -> Sink ByteString (RIO env) ()) -- ^ custom hook to observe progress
242+
-> RIO env Bool -- ^ Whether a download was performed
243243
verifiedDownload DownloadRequest{..} destpath progressSink = do
244244
let req = drRequest
245245
whenM' (liftIO getShouldDownload) $ do
246246
logDebug $ "Downloading " <> decodeUtf8With lenientDecode (path req)
247247
liftIO $ createDirectoryIfMissing True dir
248-
recoveringHttp drRetryPolicy $ liftIO $
248+
recoveringHttp drRetryPolicy $
249249
withSinkFile fptmp $ httpSink req . go
250250
liftIO $ renameFile fptmp fp
251251
where

src/RIO/Logger.hs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
{-# LANGUAGE ImplicitParams #-}
3+
module RIO.Logger
4+
( LogLevel (..)
5+
, LogSource
6+
, LogStr
7+
, HasLogFunc (..)
8+
, logGeneric
9+
, logDebug
10+
, logInfo
11+
, logWarn
12+
, logError
13+
, logOther
14+
, logSticky
15+
, logStickyDone
16+
, runNoLogging
17+
, NoLogging (..)
18+
) where
19+
20+
import Data.Text (Text)
21+
import Control.Monad.IO.Class (MonadIO, liftIO)
22+
import Control.Monad.Reader (MonadReader, ReaderT, runReaderT)
23+
import Lens.Micro (Getting, to)
24+
import Lens.Micro.Mtl (view)
25+
import GHC.Stack (HasCallStack, CallStack)
26+
27+
data LogLevel = LevelDebug | LevelInfo | LevelWarn | LevelError | LevelOther !Text
28+
deriving (Eq, Show, Read, Ord)
29+
30+
type LogSource = Text
31+
type LogStr = Text
32+
class HasLogFunc env where
33+
logFuncL :: Getting r env (CallStack -> LogSource -> LogLevel -> LogStr -> IO ())
34+
35+
logGeneric
36+
:: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack)
37+
=> LogSource
38+
-> LogLevel
39+
-> LogStr
40+
-> m ()
41+
logGeneric src level str = do
42+
logFunc <- view logFuncL
43+
liftIO $ logFunc ?callStack src level str
44+
45+
logDebug
46+
:: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack)
47+
=> LogStr
48+
-> m ()
49+
logDebug = logGeneric "" LevelDebug
50+
51+
logInfo
52+
:: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack)
53+
=> LogStr
54+
-> m ()
55+
logInfo = logGeneric "" LevelInfo
56+
57+
logWarn
58+
:: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack)
59+
=> LogStr
60+
-> m ()
61+
logWarn = logGeneric "" LevelWarn
62+
63+
logError
64+
:: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack)
65+
=> LogStr
66+
-> m ()
67+
logError = logGeneric "" LevelError
68+
69+
logOther
70+
:: (MonadIO m, MonadReader env m, HasLogFunc env, HasCallStack)
71+
=> Text -- ^ level
72+
-> LogStr
73+
-> m ()
74+
logOther = logGeneric "" . LevelOther
75+
76+
runNoLogging :: MonadIO m => ReaderT NoLogging m a -> m a
77+
runNoLogging = flip runReaderT NoLogging
78+
79+
data NoLogging = NoLogging
80+
instance HasLogFunc NoLogging where
81+
logFuncL = to (\_ _ _ _ _ -> return ())
82+
83+
-- | Write a "sticky" line to the terminal. Any subsequent lines will
84+
-- overwrite this one, and that same line will be repeated below
85+
-- again. In other words, the line sticks at the bottom of the output
86+
-- forever. Running this function again will replace the sticky line
87+
-- with a new sticky line. When you want to get rid of the sticky
88+
-- line, run 'logStickyDone'.
89+
--
90+
logSticky :: (MonadIO m, HasCallStack, MonadReader env m, HasLogFunc env) => Text -> m ()
91+
logSticky = logOther "sticky"
92+
93+
-- | This will print out the given message with a newline and disable
94+
-- any further stickiness of the line until a new call to 'logSticky'
95+
-- happens.
96+
--
97+
-- It might be better at some point to have a 'runSticky' function
98+
-- that encompasses the logSticky->logStickyDone pairing.
99+
logStickyDone :: (MonadIO m, HasCallStack, MonadReader env m, HasLogFunc env) => Text -> m ()
100+
logStickyDone = logOther "sticky-done"

src/Stack/Build.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ instance Exception CabalVersionException
170170

171171
-- | See https://github.com/commercialhaskell/stack/issues/1198.
172172
warnIfExecutablesWithSameNameCouldBeOverwritten
173-
:: MonadLogger m => [LocalPackage] -> Plan -> m ()
173+
:: HasLogFunc env => [LocalPackage] -> Plan -> RIO env ()
174174
warnIfExecutablesWithSameNameCouldBeOverwritten locals plan = do
175175
logDebug "Checking if we are going to build multiple executables with the same name"
176176
forM_ (Map.toList warnings) $ \(exe,(toBuild,otherLocals)) -> do
@@ -237,7 +237,7 @@ warnIfExecutablesWithSameNameCouldBeOverwritten locals plan = do
237237
collect :: Ord k => [(k,v)] -> Map k (NonEmpty v)
238238
collect = Map.map NE.fromList . Map.fromDistinctAscList . groupSort
239239

240-
warnAboutSplitObjs :: MonadLogger m => BuildOpts -> m ()
240+
warnAboutSplitObjs :: HasLogFunc env => BuildOpts -> RIO env ()
241241
warnAboutSplitObjs bopts | boptsSplitObjs bopts = do
242242
logWarn $ "Building with --split-objs is enabled. " <> T.pack splitObjsWarning
243243
warnAboutSplitObjs _ = return ()

0 commit comments

Comments
 (0)