on master help sign in
Fix thread id show bug in handle scribe
commitab81b5f985141a0ec8afd4fefb70d7b90b438dfe authorMichael Xavier <michael@michaelxavier.net> dateTue 19 May 2015, 09:26 (11 years ago) parentae251dd
4 files changed+80−2
katip/katip.cabal+4 katip/src/Katip/Scribes/Handle.hs+1−1 katip/test/Katip/Tests/Scribes/Handle.hs+72 katip/test/Main.hs+3−1
katip/katip.cabal+4
@@ -72,11 +72,15 @@ test-suite test
7272 , katip
7373 , aeson
7474 , tasty
75+ , tasty-hunit
7576 , tasty-quickcheck
7677 , quickcheck-instances
7778 , template-haskell
7879 , text
7980 , time
81+ , temporary
82+ , directory
83+ , regex-tdfa-rc
8084
8185
8286benchmark bench
katip/src/Katip/Scribes/Handle.hs+1−1
@@ -68,7 +68,7 @@ formatItem withColor verb Item{..} =
6868 brackets (fromText (renderSeverity' _itemSeverity)) <>
6969 brackets (fromString _itemHost) <>
7070 brackets (fromString (show _itemProcess)) <>
71− brackets (fromString (show _itemThread)) <>
71+ brackets (fromText (getThreadIdText _itemThread)) <>
7272 mconcat ks <>
7373 maybe mempty (brackets . fromString . locationToString) _itemLoc <>
7474 fromText " " <> (unLogStr _itemMessage)
katip/test/Katip/Tests/Scribes/Handle.hsnew file+72
@@ -0,0 +1,72 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+module Katip.Tests.Scribes.Handle
3+ ( tests
4+ ) where
5+
6+-------------------------------------------------------------------------------
7+import Control.Monad
8+import Data.Aeson
9+import Data.Text (Text)
10+import qualified Data.Text.IO as T
11+import System.Directory
12+import System.IO
13+import System.IO.Temp
14+import Test.Tasty
15+import Test.Tasty.HUnit
16+import Text.Regex.TDFA
17+-------------------------------------------------------------------------------
18+import Katip
19+-------------------------------------------------------------------------------
20+
21+
22+tests :: TestTree
23+tests = testGroup "Katip.Scribes.Handle"
24+ [
25+ withResource setup teardown $ \setupScribe -> testCase "logs the correct data" $ do
26+ (path, h, le) <- setupScribe
27+ runKatipT le $ logItem dummyLogItem "test" Nothing InfoS "test message"
28+ hClose h
29+ res <- readFile path
30+ let pat = "\\[[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{2}:[[:digit:]]{2}\\]\\[katip-test.test\\]\\[Info\\]\\[.+\\]\\[[[:digit:]]+\\]\\[ThreadId [[:digit:]]+\\]\\[note:some note\\] test message" :: String
31+ let matches = res =~ pat
32+ matches @?= True
33+ ]
34+
35+
36+-------------------------------------------------------------------------------
37+data DummyLogItem = DummyLogItem {
38+ dliNote :: Text
39+ }
40+
41+
42+instance ToJSON DummyLogItem where
43+ toJSON (DummyLogItem n) = object ["note" .= n]
44+
45+
46+instance ToObject DummyLogItem
47+
48+
49+instance LogItem DummyLogItem where
50+ payloadKeys _ _ = AllKeys
51+
52+
53+-------------------------------------------------------------------------------
54+dummyLogItem :: DummyLogItem
55+dummyLogItem = DummyLogItem "some note"
56+
57+
58+-------------------------------------------------------------------------------
59+setup :: IO (FilePath, Handle, LogEnv)
60+setup = do
61+ tempDir <- getTemporaryDirectory
62+ (fp, h) <- openTempFile tempDir "katip.log"
63+ s <- mkHandleScribe (ColorLog False) h DebugS V3
64+ le <- initLogEnv "katip-test" "test"
65+ return (fp, h, registerScribe "handle" s le)
66+
67+
68+-------------------------------------------------------------------------------
69+teardown :: (FilePath, Handle, LogEnv) -> IO ()
70+teardown (_, h, _) = do
71+ chk <- hIsOpen h
72+ when chk $ hClose h
katip/test/Main.hs+3−1
@@ -3,7 +3,8 @@ module Main (main) where
33-------------------------------------------------------------------------------
44import Test.Tasty
55-------------------------------------------------------------------------------
6−import Katip.Tests
6+import qualified Katip.Tests
7+import qualified Katip.Tests.Scribes.Handle
78-------------------------------------------------------------------------------
89
910
@@ -14,4 +15,5 @@ testSuite :: TestTree
1415testSuite = testGroup "katip"
1516 [
1617 Katip.Tests.tests
18+ , Katip.Tests.Scribes.Handle.tests
1719 ]