on master help sign in
Allow user to specify log verbosity in CLI
e4609aeSridhar Ratnakumar
lib/command/src/Obelisk/App.hs+1−2
@@ -12,8 +12,7 @@ import Control.Monad.Reader (MonadIO, MonadReader, ReaderT, runReaderT)
12import Obelisk.CLI.Logging (LoggingConfig, Output, handleLog)
12import Obelisk.CLI.Logging (LoggingConfig, Output, handleLog)
13
13
14data Obelisk = Obelisk
14data Obelisk = Obelisk
15− { _obelisk_verbose :: Bool
16− , _obelisk_noSpinner :: Bool
15+ { _obelisk_noSpinner :: Bool
17 , _obelisk_logging :: LoggingConfig
16 , _obelisk_logging :: LoggingConfig
18 }
17 }
19
18
lib/command/src/Obelisk/CLI/Logging.hs+11−9
@@ -4,7 +4,7 @@
4{-# LANGUAGE MultiWayIf #-}
4{-# LANGUAGE MultiWayIf #-}
5{-# LANGUAGE OverloadedStrings #-}
5{-# LANGUAGE OverloadedStrings #-}
6module Obelisk.CLI.Logging
6module Obelisk.CLI.Logging
7− ( Severity (Error, Warning, Notice)
7+ ( Severity (Error, Warning, Notice, Debug)
8 , LoggingConfig (..)
8 , LoggingConfig (..)
9 , Output (..)
9 , Output (..)
10 , failWith
10 , failWith
@@ -13,7 +13,7 @@ module Obelisk.CLI.Logging
13 ) where
13 ) where
14
14
15import Control.Concurrent.MVar (MVar, modifyMVar_)
15import Control.Concurrent.MVar (MVar, modifyMVar_)
16−import Control.Monad ((>=>))
16+import Control.Monad (unless, when, (>=>))
17import Control.Monad.Catch (MonadMask, bracket_)
17import Control.Monad.Catch (MonadMask, bracket_)
18import Control.Monad.IO.Class (liftIO)
18import Control.Monad.IO.Class (liftIO)
19import Control.Monad.Reader (MonadIO)
19import Control.Monad.Reader (MonadIO)
@@ -47,13 +47,15 @@ handleLog :: MonadIO m => LoggingConfig -> Output -> m ()
47handleLog c current = do
47handleLog c current = do
48 let lock = _loggingConfig_lock c
48 let lock = _loggingConfig_lock c
49 liftIO $ modifyMVar_ lock $ \raw -> do
49 liftIO $ modifyMVar_ lock $ \raw -> do
50− case (raw, current) of
50+ case current of
51− (True, Output_Log _) -> do
51+ Output_Log (WithSeverity sev _) -> do
52+ -- Discard unless severity is above configured log level
53+ unless (sev > _loggingConfig_level c) $ do
52− -- If the last output was raw, clear the line prior to logging a non-raw
54+ -- If the last output was raw first clear it,
53− -- message.
54− handleLog' Output_ClearLine
55+ when raw $ handleLog' Output_ClearLine
56+ -- ... then actually write the log.
55− handleLog' current
57+ handleLog' current
56− _ -> do
58+ _ ->
57 handleLog' current
59 handleLog' current
58 return $ isRaw current
60 return $ isRaw current
59
61
lib/command/src/Obelisk/Command.hs+15−7
@@ -10,7 +10,7 @@ module Obelisk.Command where
10import Control.Concurrent (newMVar, threadDelay)
10import Control.Concurrent (newMVar, threadDelay)
11import Control.Monad
11import Control.Monad
12import Control.Monad.IO.Class (liftIO)
12import Control.Monad.IO.Class (liftIO)
13−import Control.Monad.Log
13+import Control.Monad.Reader (ask)
14import qualified Data.Binary as Binary
14import qualified Data.Binary as Binary
15import qualified Data.ByteString.Base16 as Base16
15import qualified Data.ByteString.Base16 as Base16
16import qualified Data.ByteString.Lazy as LBS
16import qualified Data.ByteString.Lazy as LBS
@@ -26,7 +26,8 @@ import System.IO (hIsTerminalDevice, stdout)
26import System.Posix.Process (executeFile)
26import System.Posix.Process (executeFile)
27
27
28import Obelisk.App
28import Obelisk.App
29−import Obelisk.CLI.Logging (LoggingConfig (LoggingConfig), Severity (..), failWith, putLog)
29+import Obelisk.CLI.Logging (LoggingConfig (LoggingConfig, _loggingConfig_level), Severity (..), failWith,
30+ putLog)
30import Obelisk.Command.Deploy
31import Obelisk.Command.Deploy
31import Obelisk.Command.Project
32import Obelisk.Command.Project
32import Obelisk.Command.Repl
33import Obelisk.Command.Repl
@@ -185,16 +186,23 @@ isInteractiveTerm = do
185
186
186main :: IO ()
187main :: IO ()
187main = do
188main = do
188− -- We are not passing this as cli argument (--verbose) because run-static-io does not
189+ -- We are not passing `logLevel` as cli argument (--verbose) because run-static-io does not
189 -- pass along the cli arguments to recursive obelisks.
190 -- pass along the cli arguments to recursive obelisks.
190− verbose <- maybe False read <$> lookupEnv "VERBOSE"
191+ logLevel <- maybe Notice read <$> lookupEnv "LOGLEVEL"
191 noSpinner <- not <$> isInteractiveTerm
192 noSpinner <- not <$> isInteractiveTerm
192− loggingConfig <- LoggingConfig Debug <$> newMVar False
193+ loggingConfig <- LoggingConfig logLevel <$> newMVar False
193− let obeliskConfig = Obelisk verbose noSpinner loggingConfig
194+ let obeliskConfig = Obelisk noSpinner loggingConfig
194 runObelisk obeliskConfig $ ObeliskT main'
195 runObelisk obeliskConfig $ ObeliskT main'
195
196
196main' :: MonadObelisk m => m ()
197main' :: MonadObelisk m => m ()
197main' = do
198main' = do
199+ c <- ask
200+ putLog Debug $ T.unwords
201+ [ "Obelisk"
202+ , "noSpinner=" <> T.pack (show $ _obelisk_noSpinner c)
203+ , "logging-level=" <> T.pack (show $ _loggingConfig_level $ _obelisk_logging c)
204+ ]
205+
198 myArgs <- liftIO getArgs
206 myArgs <- liftIO getArgs
199 --TODO: We'd like to actually use the parser to determine whether to hand off,
207 --TODO: We'd like to actually use the parser to determine whether to hand off,
200 --but in the case where this implementation of 'ob' doesn't support all
208 --but in the case where this implementation of 'ob' doesn't support all
@@ -210,7 +218,7 @@ main' = do
210 Nothing -> go as -- If not in a project, just run ourselves
218 Nothing -> go as -- If not in a project, just run ourselves
211 Just impl -> do
219 Just impl -> do
212 -- Invoke the real implementation, using --no-handoff to prevent infinite recursion
220 -- Invoke the real implementation, using --no-handoff to prevent infinite recursion
213− putLog Notice $ "Handing off to " <> T.pack impl
221+ putLog Debug $ "Handing off to " <> T.pack impl
214 liftIO $ executeFile impl False ("--no-handoff" : myArgs) Nothing
222 liftIO $ executeFile impl False ("--no-handoff" : myArgs) Nothing
215 case myArgs of
223 case myArgs of
216 "--no-handoff" : as -> go as -- If we've been told not to hand off, don't hand off
224 "--no-handoff" : as -> go as -- If we've been told not to hand off, don't hand off