on master help sign in
Add some more control to color logging
commit65dc987649198c33270681bce9ad318104809c6a authorOzgun Ataman <ozgun.ataman@soostone.com> dateSun 1 Mar 2015, 21:07 (12 years ago) parentad3a240
2 files changed+16−5
examples/Main.hs+1−1 src/Katip/Scribes/Handle.hs+15−4
examples/Main.hs+1−1
@@ -4,7 +4,7 @@ module Main where
44
55-------------------------------------------------------------------------------
66import Data.Aeson
7−import Data.Monoid
7+
88import Data.Text
99-------------------------------------------------------------------------------
1010import Katip
src/Katip/Scribes/Handle.hs+15−4
@@ -2,6 +2,7 @@
22
33module Katip.Scribes.Handle
44 ( mkHandleScribe
5+ , ColorStrategy (..)
56 ) where
67
78-------------------------------------------------------------------------------
@@ -44,10 +45,20 @@ renderPrim NullPrim = fromByteString "null"
4445
4546
4647-------------------------------------------------------------------------------
47−mkHandleScribe :: Handle -> Severity -> Verbosity -> IO Scribe
48−mkHandleScribe h sev verb = do
48+data ColorStrategy
49+ = ColorLog Bool
50+ -- ^ Whether to use color control chars in log output
51+ | ColorIfTerminal
52+ -- ^ Color if output is a terminal
53+
54+
55+-------------------------------------------------------------------------------
56+mkHandleScribe :: ColorStrategy -> Handle -> Severity -> Verbosity -> IO Scribe
57+mkHandleScribe cs h sev verb = do
4958 hSetBuffering h LineBuffering
50− colorize <- hIsTerminalDevice h
59+ colorize <- case cs of
60+ ColorIfTerminal -> hIsTerminalDevice h
61+ ColorLog b -> return b
5162 return $ Scribe $ \ i@Item{..} -> do
5263 when (itemSeverity >= sev) $
5364 B.hPutStrLn h $ toByteString $ formatItem colorize verb i
@@ -87,7 +98,7 @@ formatItem withColor verb Item{..} =
8798_ioLogEnv :: LogEnv
8899_ioLogEnv = unsafePerformIO $ do
89100 le <- initLogEnv "io" "io"
90− lh <- mkHandleScribe stdout Debug V3
101+ lh <- mkHandleScribe ColorIfTerminal stdout Debug V3
91102 return $ registerScribe "stdout" lh le
92103{-# NOINLINE _ioLogEnv #-}
93104