11{-# LANGUAGE NoImplicitPrelude #-}
22{-# OPTIONS_GHC -fno-warn-orphans #-}
33{-# LANGUAGE FlexibleContexts #-}
4- {-# LANGUAGE TemplateHaskell #-}
54{-# LANGUAGE FlexibleInstances #-}
65{-# LANGUAGE OverloadedStrings #-}
76
@@ -12,7 +11,7 @@ module Stack.PrettyPrint
1211 -- * Logging based on pretty-print typeclass
1312 , prettyDebug , prettyInfo , prettyWarn , prettyError
1413 , prettyDebugL , prettyInfoL , prettyWarnL , prettyErrorL
15- , prettyDebugS , prettyInfoS , prettyWarnS , prettyErrorS
14+ , prettyWarnS , prettyErrorS
1615 -- * Semantic styling functions
1716 -- | These are preferred to styling or colors directly, so that we can
1817 -- encourage consistency.
@@ -37,7 +36,6 @@ module Stack.PrettyPrint
3736import Stack.Prelude
3837import Data.List (intersperse )
3938import qualified Data.Text as T
40- import Language.Haskell.TH
4139import Stack.Types.Config
4240import Stack.Types.Package
4341import Stack.Types.PackageIdentifier
@@ -48,48 +46,65 @@ import qualified System.Clock as Clock
4846import Text.PrettyPrint.Leijen.Extended
4947
5048displayWithColor
51- :: (HasRunner env , MonadReader env m , Display a , HasAnsiAnn (Ann a ))
52- => a -> m T. Text
49+ :: (HasRunner env , Display a , HasAnsiAnn (Ann a ))
50+ => a -> RIO env T. Text
5351displayWithColor x = do
5452 useAnsi <- liftM logUseColor $ view logOptionsL
5553 return $ if useAnsi then displayAnsi x else displayPlain x
5654
5755-- TODO: switch to using implicit callstacks once 7.8 support is dropped
5856
59- prettyWith :: LogLevel -> ExpQ -> Q Exp
60- prettyWith level f = do
61- loc <- location
62- [e | monadLoggerLog loc "" level <=< displayWithColor . $f |]
57+ prettyWith :: (HasRunner env , HasCallStack , Display b , HasAnsiAnn (Ann b ))
58+ => LogLevel -> (a -> b ) -> a -> RIO env ()
59+ prettyWith level f = logOther level <=< displayWithColor . f
6360
6461-- Note: I think keeping this section aligned helps spot errors, might be
6562-- worth keeping the alignment in place.
66- prettyDebugWith , prettyInfoWith , prettyWarnWith , prettyErrorWith :: ExpQ -> Q Exp
67- prettyDebugWith = prettyWith LevelDebug
68- prettyInfoWith = prettyWith LevelInfo
69- prettyWarnWith f = prettyWith LevelWarn
70- [| (line <> ) . (styleWarning " Warning:" <+> ) .
71- indentAfterLabel . $ f | ]
63+ prettyDebugWith, prettyInfoWith
64+ :: (HasCallStack , HasRunner env , Display b , HasAnsiAnn (Ann b ))
65+ => (a -> b ) -> a -> RIO env ()
66+ prettyDebugWith = prettyWith LevelDebug
67+ prettyInfoWith = prettyWith LevelInfo
68+
69+ prettyWarnWith, prettyErrorWith
70+ :: (HasCallStack , HasRunner env )
71+ => (a -> Doc AnsiAnn ) -> a -> RIO env ()
72+ prettyWarnWith f = prettyWith LevelWarn
73+ ((line <> ) . (styleWarning " Warning:" <+> ) .
74+ indentAfterLabel . f)
7275prettyErrorWith f = prettyWith LevelError
73- [| (line <> ) . (styleError " Error:" <+> ) .
74- indentAfterLabel . $ f | ]
75-
76- prettyDebug , prettyInfo , prettyWarn , prettyError :: Q Exp
77- prettyDebug = prettyDebugWith [| id | ]
78- prettyInfo = prettyInfoWith [| id | ]
79- prettyWarn = prettyWarnWith [| id | ]
80- prettyError = prettyErrorWith [| id | ]
81-
82- prettyDebugL , prettyInfoL , prettyWarnL , prettyErrorL :: Q Exp
83- prettyDebugL = prettyDebugWith [| fillSep | ]
84- prettyInfoL = prettyInfoWith [| fillSep | ]
85- prettyWarnL = prettyWarnWith [| fillSep | ]
86- prettyErrorL = prettyErrorWith [| fillSep | ]
87-
88- prettyDebugS , prettyInfoS , prettyWarnS , prettyErrorS :: Q Exp
89- prettyDebugS = prettyDebugWith [| flow | ]
90- prettyInfoS = prettyInfoWith [| flow | ]
91- prettyWarnS = prettyWarnWith [| flow | ]
92- prettyErrorS = prettyErrorWith [| flow | ]
76+ ((line <> ) . (styleError " Error:" <+> ) .
77+ indentAfterLabel . f)
78+
79+ prettyDebug, prettyInfo
80+ :: (HasCallStack , HasRunner env , Display b , HasAnsiAnn (Ann b ))
81+ => b -> RIO env ()
82+ prettyDebug = prettyDebugWith id
83+ prettyInfo = prettyInfoWith id
84+
85+ prettyWarn, prettyError
86+ :: (HasCallStack , HasRunner env )
87+ => Doc AnsiAnn -> RIO env ()
88+ prettyWarn = prettyWarnWith id
89+ prettyError = prettyErrorWith id
90+
91+ prettyDebugL, prettyInfoL
92+ :: (HasCallStack , HasRunner env , HasAnsiAnn a )
93+ => [Doc a ] -> RIO env ()
94+ prettyDebugL = prettyDebugWith fillSep
95+ prettyInfoL = prettyInfoWith fillSep
96+
97+ prettyWarnL, prettyErrorL
98+ :: (HasCallStack , HasRunner env )
99+ => [Doc AnsiAnn ] -> RIO env ()
100+ prettyWarnL = prettyWarnWith fillSep
101+ prettyErrorL = prettyErrorWith fillSep
102+
103+ prettyWarnS, prettyErrorS
104+ :: (HasCallStack , HasRunner env )
105+ => String -> RIO env ()
106+ prettyWarnS = prettyWarnWith flow
107+ prettyErrorS = prettyErrorWith flow
93108-- End of aligned section
94109
95110-- | Use after a label and before the rest of what's being labelled for
@@ -107,25 +122,22 @@ wordDocs = map fromString . words
107122flow :: String -> Doc a
108123flow = fillSep . wordDocs
109124
110- debugBracket :: Q Exp
111- debugBracket = do
112- loc <- location
113- [e | \msg f -> do
114- let output = monadLoggerLog loc "" LevelDebug <=< displayWithColor
115- output $ "Start: " <> msg
116- start <- liftIO $ Clock.getTime Clock.Monotonic
117- x <- f `catch` \ex -> do
118- end <- liftIO $ Clock.getTime Clock.Monotonic
119- let diff = Clock.diffTimeSpec start end
120- output $ "Finished with exception in" <+> displayMilliseconds diff <> ":" <+>
121- msg <> line <>
122- "Exception thrown: " <> fromString (show ex)
123- throwIO (ex :: SomeException)
124- end <- liftIO $ Clock.getTime Clock.Monotonic
125- let diff = Clock.diffTimeSpec start end
126- output $ "Finished in" <+> displayMilliseconds diff <> ":" <+> msg
127- return x
128- |]
125+ debugBracket :: (HasCallStack , HasRunner env ) => Doc AnsiAnn -> RIO env a -> RIO env a
126+ debugBracket msg f = do
127+ let output = logDebug <=< displayWithColor
128+ output $ " Start: " <> msg
129+ start <- liftIO $ Clock. getTime Clock. Monotonic
130+ x <- f `catch` \ ex -> do
131+ end <- liftIO $ Clock. getTime Clock. Monotonic
132+ let diff = Clock. diffTimeSpec start end
133+ output $ " Finished with exception in" <+> displayMilliseconds diff <> " :" <+>
134+ msg <> line <>
135+ " Exception thrown: " <> fromString (show ex)
136+ throwIO (ex :: SomeException )
137+ end <- liftIO $ Clock. getTime Clock. Monotonic
138+ let diff = Clock. diffTimeSpec start end
139+ output $ " Finished in" <+> displayMilliseconds diff <> " :" <+> msg
140+ return x
129141
130142-- | Style an 'AnsiDoc' as an error. Should be used sparingly, not to style
131143-- entire long messages. For example, it's used to style the "Error:"
0 commit comments