on master help sign in
Allow user to specify log verbosity in CLI

``` LOGLEVEL=... ob ... ``` - Make the handoff log message of Debug sev - Display Obelisk config in Debug sev

commite4609ae06ee5cab8da4dd295a860b9e13f693b77 authorSridhar Ratnakumar <srid@srid.ca> dateSat 12 May 2018, 14:47 (8 years ago) parent12f1f98
3 files changed+27−18
lib/command/src/Obelisk/App.hs+1−2 lib/command/src/Obelisk/CLI/Logging.hs+11−9 lib/command/src/Obelisk/Command.hs+15−7
lib/command/src/Obelisk/App.hs+1−2
@@ -12,8 +12,7 @@ import Control.Monad.Reader (MonadIO, MonadReader, ReaderT, runReaderT)
1212import Obelisk.CLI.Logging (LoggingConfig, Output, handleLog)
1313
1414data Obelisk = Obelisk
15− { _obelisk_verbose :: Bool
16− , _obelisk_noSpinner :: Bool
15+ { _obelisk_noSpinner :: Bool
1716 , _obelisk_logging :: LoggingConfig
1817 }
1918
lib/command/src/Obelisk/CLI/Logging.hs+11−9
@@ -4,7 +4,7 @@
44{-# LANGUAGE MultiWayIf #-}
55{-# LANGUAGE OverloadedStrings #-}
66module Obelisk.CLI.Logging
7− ( Severity (Error, Warning, Notice)
7+ ( Severity (Error, Warning, Notice, Debug)
88 , LoggingConfig (..)
99 , Output (..)
1010 , failWith
@@ -13,7 +13,7 @@ module Obelisk.CLI.Logging
1313 ) where
1414
1515import Control.Concurrent.MVar (MVar, modifyMVar_)
16−import Control.Monad ((>=>))
16+import Control.Monad (unless, when, (>=>))
1717import Control.Monad.Catch (MonadMask, bracket_)
1818import Control.Monad.IO.Class (liftIO)
1919import Control.Monad.Reader (MonadIO)
@@ -47,13 +47,15 @@ handleLog :: MonadIO m => LoggingConfig -> Output -> m ()
4747handleLog c current = do
4848 let lock = _loggingConfig_lock c
4949 liftIO $ modifyMVar_ lock $ \raw -> do
50− case (raw, current) of
51− (True, Output_Log _) -> do
52− -- If the last output was raw, clear the line prior to logging a non-raw
53− -- message.
54− handleLog' Output_ClearLine
55− handleLog' current
56− _ -> do
50+ case current of
51+ Output_Log (WithSeverity sev _) -> do
52+ -- Discard unless severity is above configured log level
53+ unless (sev > _loggingConfig_level c) $ do
54+ -- If the last output was raw first clear it,
55+ when raw $ handleLog' Output_ClearLine
56+ -- ... then actually write the log.
57+ handleLog' current
58+ _ ->
5759 handleLog' current
5860 return $ isRaw current
5961
lib/command/src/Obelisk/Command.hs+15−7
@@ -10,7 +10,7 @@ module Obelisk.Command where
1010import Control.Concurrent (newMVar, threadDelay)
1111import Control.Monad
1212import Control.Monad.IO.Class (liftIO)
13−import Control.Monad.Log
13+import Control.Monad.Reader (ask)
1414import qualified Data.Binary as Binary
1515import qualified Data.ByteString.Base16 as Base16
1616import qualified Data.ByteString.Lazy as LBS
@@ -26,7 +26,8 @@ import System.IO (hIsTerminalDevice, stdout)
2626import System.Posix.Process (executeFile)
2727
2828import 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)
3031import Obelisk.Command.Deploy
3132import Obelisk.Command.Project
3233import Obelisk.Command.Repl
@@ -185,16 +186,23 @@ isInteractiveTerm = do
185186
186187main :: IO ()
187188main = 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
189190 -- pass along the cli arguments to recursive obelisks.
190− verbose <- maybe False read <$> lookupEnv "VERBOSE"
191+ logLevel <- maybe Notice read <$> lookupEnv "LOGLEVEL"
191192 noSpinner <- not <$> isInteractiveTerm
192− loggingConfig <- LoggingConfig Debug <$> newMVar False
193− let obeliskConfig = Obelisk verbose noSpinner loggingConfig
193+ loggingConfig <- LoggingConfig logLevel <$> newMVar False
194+ let obeliskConfig = Obelisk noSpinner loggingConfig
194195 runObelisk obeliskConfig $ ObeliskT main'
195196
196197main' :: MonadObelisk m => m ()
197198main' = 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+
198206 myArgs <- liftIO getArgs
199207 --TODO: We'd like to actually use the parser to determine whether to hand off,
200208 --but in the case where this implementation of 'ob' doesn't support all
@@ -210,7 +218,7 @@ main' = do
210218 Nothing -> go as -- If not in a project, just run ourselves
211219 Just impl -> do
212220 -- 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
214222 liftIO $ executeFile impl False ("--no-handoff" : myArgs) Nothing
215223 case myArgs of
216224 "--no-handoff" : as -> go as -- If we've been told not to hand off, don't hand off