on master help sign in
Preview of a faster Handle Scribe
commite821e940a4cf1f4eb9526cd77faa5dab569e447e authorAlfredo Di Napoli <alfredo.dinapoli@gmail.com> dateFri 17 Feb 2017, 12:27 (10 years ago) parentd2cf137
8 files changed+39−17
katip/katip.cabal+2 katip/src/Katip/Scribes/Handle.hs+30−16 stack-lts-2.yaml+1 stack-lts-3.yaml+1 stack-lts-4.yaml+1 stack-lts-5.yaml+1 stack-nightly.yaml+1 stack.yaml+2−1
katip/katip.cabal+2
@@ -55,6 +55,7 @@ library
5555
5656 build-depends: base >=4.5 && <5
5757 , aeson >=0.6 && <1.2
58+ , async < 3.0.0.0
5859 , auto-update >= 0.1 && < 0.2
5960 , bytestring >= 0.9 && < 0.11
6061 , containers >=0.4 && <0.6
@@ -76,6 +77,7 @@ library
7677 , microlens >= 0.2.0.0 && < 0.5
7778 , microlens-th >= 0.1.0.0 && < 0.5
7879 , semigroups
80+ , unagi-chan < 0.5.0.0
7981
8082 hs-source-dirs: src
8183 default-language: Haskell2010
katip/src/Katip/Scribes/Handle.hs+30−16
@@ -3,21 +3,21 @@
33module Katip.Scribes.Handle where
44
55-------------------------------------------------------------------------------
6−import Control.Applicative as A
7−import Control.Concurrent
8−import Control.Exception (bracket_)
6+import Control.Applicative as A
97import Control.Monad
108import Data.Aeson
11−import qualified Data.HashMap.Strict as HM
9+import qualified Data.HashMap.Strict as HM
1210import Data.Monoid
13−import Data.Text (Text)
11+import Data.Text (Text)
1412import Data.Text.Lazy.Builder
15−import Data.Text.Lazy.IO as T
13+import Data.Text.Lazy.IO as T
1614import System.IO
17−import System.IO.Unsafe (unsafePerformIO)
15+import System.IO.Unsafe (unsafePerformIO)
16+import qualified Control.Concurrent.Chan.Unagi.Bounded as U
17+import Control.Concurrent.Async
1818-------------------------------------------------------------------------------
1919import Katip.Core
20−import Katip.Format.Time (formatAsLogTime)
20+import Katip.Format.Time (formatAsLogTime)
2121-------------------------------------------------------------------------------
2222
2323
@@ -48,6 +48,10 @@ data ColorStrategy
4848 | ColorIfTerminal
4949 -- ^ Color if output is a terminal
5050
51+-------------------------------------------------------------------------------
52+data WorkerCmd =
53+ NewItem Builder
54+ | PoisonPill
5155
5256-------------------------------------------------------------------------------
5357-- | Logs to a file handle such as stdout, stderr, or a file. Contexts
@@ -59,15 +63,25 @@ data ColorStrategy
5963-- > [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
6064mkHandleScribe :: ColorStrategy -> Handle -> Severity -> Verbosity -> IO Scribe
6165mkHandleScribe cs h sev verb = do
62− hSetBuffering h LineBuffering
63− colorize <- case cs of
64− ColorIfTerminal -> hIsTerminalDevice h
65− ColorLog b -> return b
66− lock <- newMVar ()
67− return $ Scribe $ \ i@Item{..} -> do
68− when (permitItem sev i) $ bracket_ (takeMVar lock) (putMVar lock ()) $
69− T.hPutStrLn h $ toLazyText $ formatItem colorize verb i
66+ (inChan, outChan) <- U.newChan 4096
67+ -- TODO: Come up with a good story for resource cleanup.
68+ _ <- async $ workerLoop outChan
69+ hSetBuffering h LineBuffering
70+ colorize <- case cs of
71+ ColorIfTerminal -> hIsTerminalDevice h
72+ ColorLog b -> return b
73+ return $ Scribe $ \i -> when (permitItem sev i) $ do
74+ void (U.tryWriteChan inChan (NewItem (formatItem colorize verb i)))
7075
76+ where
77+ workerLoop :: U.OutChan WorkerCmd -> IO ()
78+ workerLoop outChan = do
79+ newCmd <- U.readChan outChan
80+ case newCmd of
81+ NewItem b -> do
82+ T.hPutStrLn h $ toLazyText b
83+ workerLoop outChan
84+ PoisonPill -> return ()
7185
7286-------------------------------------------------------------------------------
7387formatItem :: LogItem a => Bool -> Verbosity -> Item a -> Builder
stack-lts-2.yaml+1
@@ -21,6 +21,7 @@ extra-deps:
2121 - memory-0.13
2222 - rng-utils-0.2.1
2323 - lens-aeson-1.0.0.5
24+ - unagi-chan-0.4.0.0
2425flags:
2526 katip:
2627 lib-Werror: true
stack-lts-3.yaml+1
@@ -14,6 +14,7 @@ extra-deps:
1414 - retry-0.7.4.2
1515 - tasty-0.11.0.4
1616 - rng-utils-0.2.1
17+ - unagi-chan-0.4.0.0
1718flags:
1819 katip:
1920 lib-Werror: true
stack-lts-4.yaml+1
@@ -11,6 +11,7 @@ extra-deps:
1111 - tagged-0.8.3
1212 - uuid-1.3.12
1313 - rng-utils-0.2.1
14+ - unagi-chan-0.4.0.0
1415flags:
1516 katip:
1617 lib-Werror: true
stack-lts-5.yaml+1
@@ -8,6 +8,7 @@ extra-deps:
88 - aeson-0.11.1.0
99 - http-client-0.5.4
1010 - rng-utils-0.2.1
11+ - unagi-chan-0.4.0.0
1112flags:
1213 katip:
1314 lib-Werror: true
stack-nightly.yaml+1
@@ -4,6 +4,7 @@ packages:
44 - katip-elasticsearch
55extra-deps:
66 - bloodhound-0.12.1.0 # this shouldn't be necessary for too much longer
7+ - unagi-chan-0.4.0.0
78flags:
89 katip:
910 lib-Werror: true
stack.yaml+2−1
@@ -2,7 +2,8 @@ resolver: lts-7.9
22packages:
33 - katip
44 - katip-elasticsearch
5−extra-deps: []
5+extra-deps:
6+ - unagi-chan-0.4.0.0
67flags:
78 katip:
89 lib-Werror: true