@@ -5,6 +5,7 @@ module Katip.Scribes.Handle where
5-------------------------------------------------------------------------------
5-------------------------------------------------------------------------------
6import Control.Applicative as A
6import Control.Applicative as A
7import Control.Monad
7import Control.Monad
8+import Control.Exception (onException)
8import Data.Aeson
9import Data.Aeson
9import qualified Data.HashMap.Strict as HM
10import qualified Data.HashMap.Strict as HM
10import Data.Monoid
11import Data.Monoid
@@ -61,19 +62,27 @@ data WorkerCmd =
61-- > [2016-05-11 21:01:15][MyApp][Info][myhost.example.com][1724][ThreadId 1154][main:Helpers.Logging Helpers/Logging.hs:32:7] Started
62-- > [2016-05-11 21:01:15][MyApp][Info][myhost.example.com][1724][ThreadId 1154][main:Helpers.Logging Helpers/Logging.hs:32:7] Started
62-- > [2016-05-11 21:01:15][MyApp.confrabulation][Debug][myhost.example.com][1724][ThreadId 1154][confrab_factor:42.0][main:Helpers.Logging Helpers/Logging.hs:41:9] Confrabulating widgets, with extra namespace and context
63-- > [2016-05-11 21:01:15][MyApp.confrabulation][Debug][myhost.example.com][1724][ThreadId 1154][confrab_factor:42.0][main:Helpers.Logging Helpers/Logging.hs:41:9] Confrabulating widgets, with extra namespace and context
63-- > [2016-05-11 21:01:15][MyApp][Info][myhost.example.com][1724][ThreadId 1154][main:Helpers.Logging Helpers/Logging.hs:43:7] Namespace and context are back to normal
64-- > [2016-05-11 21:01:15][MyApp][Info][myhost.example.com][1724][ThreadId 1154][main:Helpers.Logging Helpers/Logging.hs:43:7] Namespace and context are back to normal
66+-- Returns the newly-created `Scribe` together with a finaliser the user needs to run to perform resource cleanup.
64−mkHandleScribe :: ColorStrategy -> Handle -> Severity -> Verbosity -> IO Scribe
67+mkHandleScribe :: ColorStrategy -> Handle -> Severity -> Verbosity -> IO (Scribe, IO ())
65mkHandleScribe cs h sev verb = do
68mkHandleScribe cs h sev verb = do
66 (inChan, outChan) <- U.newChan 4096
69 (inChan, outChan) <- U.newChan 4096
67− -- TODO: Come up with a good story for resource cleanup.
68− _ <- async $ workerLoop outChan
70+ worker <- async $ workerLoop outChan
71+ flip onException (stopWorker worker inChan) $ do
69− hSetBuffering h LineBuffering
72+ hSetBuffering h LineBuffering
70− colorize <- case cs of
73+ colorize <- case cs of
71− ColorIfTerminal -> hIsTerminalDevice h
74+ ColorIfTerminal -> hIsTerminalDevice h
72− ColorLog b -> return b
75+ ColorLog b -> return b
73− return $ Scribe $ \i -> when (permitItem sev i) $ do
76+ let scribe = Scribe $ \i ->
74− void (U.tryWriteChan inChan (NewItem (formatItem colorize verb i)))
77+ when (permitItem sev i) $ void (U.tryWriteChan inChan (NewItem (formatItem colorize verb i)))
78+ return (scribe, stopWorker worker inChan)
81+ stopWorker :: Async () -> U.InChan WorkerCmd -> IO ()
82+ stopWorker worker inChan = do
83+ U.writeChan inChan PoisonPill
77 workerLoop :: U.OutChan WorkerCmd -> IO ()
86 workerLoop :: U.OutChan WorkerCmd -> IO ()
78 workerLoop outChan = do
87 workerLoop outChan = do
79 newCmd <- U.readChan outChan
88 newCmd <- U.readChan outChan
@@ -114,10 +123,11 @@ formatItem withColor verb Item{..} =
115-------------------------------------------------------------------------------
124-------------------------------------------------------------------------------
116-- | An implicit environment to enable logging directly ouf of the IO monad.
125-- | An implicit environment to enable logging directly ouf of the IO monad.
126+-- Be careful as this LogEnv won't perform any resource cleanup for you.
117_ioLogEnv :: LogEnv
127_ioLogEnv :: LogEnv
118_ioLogEnv = unsafePerformIO $ do
128_ioLogEnv = unsafePerformIO $ do
119 le <- initLogEnv "io" "io"
129 le <- initLogEnv "io" "io"
120− lh <- mkHandleScribe ColorIfTerminal stdout DebugS V3
130+ (lh, _) <- mkHandleScribe ColorIfTerminal stdout DebugS V3
121 return $ registerScribe "stdout" lh le
131 return $ registerScribe "stdout" lh le
122{-# NOINLINE _ioLogEnv #-}
132{-# NOINLINE _ioLogEnv #-}