on master help sign in
Revert changes to TH-API; add non-TH API using implicit callstacks
commit8aa357abc4574cedfc87bf452209b7b35cf0ca5c authorAlfredo Di Napoli <alfredo.dinapoli@gmail.com> dateMon 11 Apr 2016, 18:44 (11 years ago) parent824cd54
5 files changed+78−27
katip-elasticsearch/test/Main.hs+5−5 katip/examples/example.hs+3−3 katip/examples/example_lens.hs+3−3 katip/src/Katip/Core.hs+41−9 katip/src/Katip/Monadic.hs+26−7
katip-elasticsearch/test/Main.hs+5−5
@@ -82,7 +82,7 @@ esTests :: TestTree
8282esTests = testGroup "elasticsearch scribe"
8383 [
8484 withSearch' (\c -> c { essIndexSharding = NoIndexSharding}) $ \setup -> testCase "it flushes to elasticsearch" $ withTestLogging setup $ \done -> do
85− logT (ExampleCtx True) mempty InfoS "A test message"
85+ $(logT) (ExampleCtx True) mempty InfoS "A test message"
8686 liftIO $ do
8787 void done
8888 logs <- getLogs
@@ -94,10 +94,10 @@ esTests = testGroup "elasticsearch scribe"
9494 let t1 = mkTime 2016 1 2 3 4 5
9595 fakeClock <- newTVarIO t1
9696 withTestLogging' (set logEnvTimer (readTVarIO fakeClock)) setup $ \done -> do
97− logT (ExampleCtx True) mempty InfoS "today"
97+ $(logT) (ExampleCtx True) mempty InfoS "today"
9898 let t2 = mkTime 2016 1 3 3 4 5
9999 liftIO (atomically (writeTVar fakeClock t2))
100− logT (ExampleCtx True) mempty InfoS "tomorrow"
100+ $(logT) (ExampleCtx True) mempty InfoS "tomorrow"
101101 liftIO $ do
102102 void done
103103 todayLogs <- getLogsByIndex (IndexName "katip-elasticsearch-tests-2016-1-2")
@@ -112,10 +112,10 @@ esTests = testGroup "elasticsearch scribe"
112112 let t1 = mkTime 2016 3 5 0 0 0 -- saturday, march 5th
113113 fakeClock <- newTVarIO t1
114114 withTestLogging' (set logEnvTimer (readTVarIO fakeClock)) setup $ \done -> do
115− logT (ExampleCtx True) mempty InfoS "today"
115+ $(logT) (ExampleCtx True) mempty InfoS "today"
116116 let t2 = mkTime 2016 3 6 0 0 0 -- sunday march 6th
117117 liftIO (atomically (writeTVar fakeClock t2))
118− logT (ExampleCtx True) mempty InfoS "tomorrow"
118+ $(logT) (ExampleCtx True) mempty InfoS "tomorrow"
119119 liftIO $ do
120120 void done
121121 todayLogs <- getLogsByIndex (IndexName "katip-elasticsearch-tests-2016-2-28") -- rounds back to previous sunday
katip/examples/example.hs+3−3
@@ -32,7 +32,7 @@ main = do
3232 let le' = registerScribe "stdout" handleScribe le
3333 let s = MyState M.mempty mempty le'
3434 runStack s $ do
35− logTM InfoS "Started"
35+ $(logTM) InfoS "Started"
3636 -- this will add "confrabulation" to the current namespace, making
3737 -- logs made under this block have the namespace of
3838 -- "main.confrabulation". Further, ConfrabLogCTX's key/value
@@ -41,9 +41,9 @@ main = do
4141 -- code and they will be flattened out and combined in the log
4242 -- output.
4343 addNamespace "confrabulation" $ addContext (ConfrabLogCTX 42) $ do
44− logTM DebugS "Confrabulating widgets, with extra namespace and context"
44+ $(logTM) DebugS "Confrabulating widgets, with extra namespace and context"
4545 confrabulateWidgets
46− logTM InfoS "Namespace and context are back to normal"
46+ $(logTM) InfoS "Namespace and context are back to normal"
4747
4848
4949-------------------------------------------------------------------------------
katip/examples/example_lens.hs+3−3
@@ -48,7 +48,7 @@ main = do
4848 let le' = registerScribe "stdout" handleScribe le
4949 let s = MyState M.mempty mempty le'
5050 runStack s $ do
51− logTM InfoS "Started"
51+ $(logTM) InfoS "Started"
5252 -- this will add "confrabulation" to the current namespace, making
5353 -- logs made under this block have the namespace of
5454 -- "main.confrabulation". Further, ConfrabLogCTX's key/value
@@ -57,9 +57,9 @@ main = do
5757 -- code and they will be flattened out and combined in the log
5858 -- output.
5959 addNamespace "confrabulation" $ addContext (ConfrabLogCTX 42) $ do
60− logTM DebugS "Confrabulating widgets, with extra namespace and context"
60+ $(logTM) DebugS "Confrabulating widgets, with extra namespace and context"
6161 confrabulateWidgets
62− logTM InfoS "Namespace and context are back to normal"
62+ $(logTM) InfoS "Namespace and context are back to normal"
6363
6464
6565-------------------------------------------------------------------------------
katip/src/Katip/Core.hs+41−9
@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE DeriveFunctor #-}
23{-# LANGUAGE DeriveGeneric #-}
34{-# LANGUAGE ExistentialQuantification #-}
@@ -47,8 +48,10 @@ import qualified Data.Text as T
4748import qualified Data.Text.Lazy.Builder as B
4849import Data.Time
4950import GHC.Generics hiding (to)
51+#if MIN_VERSION_base(4, 8, 0)
5052import GHC.SrcLoc
5153import GHC.Stack
54+#endif
5255import Language.Haskell.TH
5356import qualified Language.Haskell.TH.Syntax as TH
5457import Lens.Micro
@@ -723,7 +726,9 @@ liftLoc (Loc a b c (d1, d2) (e1, e2)) = [|Loc
723726
724727-------------------------------------------------------------------------------
725728-- | For use when you want to include location in your logs. This will
726−-- fill the 'Maybe Loc' gap in 'logF' of this module.
729+-- fill the 'Maybe Loc' gap in 'logF' of this module, and relies on implicit
730+-- callstacks when available (GHC > 7.8).
731+#if MIN_VERSION_base(4, 8, 0)
727732getLoc :: (?loc :: CallStack) => Maybe Loc
728733getLoc = case getCallStack ?loc of
729734 [] -> Nothing
@@ -737,18 +742,45 @@ getLoc = case getCallStack ?loc of
737742 , loc_start = (srcLocStartLine l, srcLocStartCol l)
738743 , loc_end = (srcLocEndLine l, srcLocEndCol l)
739744 }
745+#else
746+getLoc :: Maybe Loc
747+getLoc = Nothing
748+#endif
749+
750+
751+-------------------------------------------------------------------------------
752+-- Like `getLoc`, but uses template-haskell and works with older versions of
753+-- the compiler (GHC 7.8 or older).
754+getLocTH :: ExpQ
755+getLocTH = [| $(location >>= liftLoc) |]
756+
740757
741758-------------------------------------------------------------------------------
742759-- | 'Loc'-tagged logging when using template-haskell.
743760--
744−-- @logT obj mempty InfoS "Hello world"@
745−logT :: (A.Applicative m, LogItem a, Katip m)
746− => a
747− -> Namespace
748− -> Severity
749− -> LogStr
750− -> m ()
751−logT a ns = logItem a ns getLoc
761+-- @$(logT) obj mempty InfoS "Hello world"@
762+logT :: ExpQ
763+logT = [| \ a ns sev msg -> logItem a ns (Just $(getLocTH)) sev msg |]
764+
765+
766+-------------------------------------------------------------------------------
767+-- | 'Loc'-tagged logging using implicit-callstacks when available.
768+--
769+-- This function does not require template-haskell as it
770+-- automatically uses <https://hackage.haskell.org/package/base-4.8.2.0/docs/GHC-Stack.html#v:getCallStack implicit-callstacks>
771+-- when the code is compiled using GHC > 7.8. Using an older version of the
772+-- compiler will result in the emission of a log line without any location information,
773+-- so be aware of it. Users using GHC <= 7.8 may want to use the template-haskell function
774+-- `logT` for maximum compatibility.
775+--
776+-- @logLoc obj mempty InfoS "Hello world"@
777+logLoc :: (Applicative m, LogItem a, Katip m)
778+ => a
779+ -> Namespace
780+ -> Severity
781+ -> LogStr
782+ -> m ()
783+logLoc a ns = logItem a ns getLoc
752784
753785
754786-- taken from the file-location package
katip/src/Katip/Monadic.hs+26−7
@@ -16,6 +16,7 @@ module Katip.Monadic
1616 -- * Monadic variants of logging functions from "Katip.Core"
1717 logFM
1818 , logTM
19+ , logLocM
1920 , logItemM
2021 , logExceptionM
2122
@@ -185,12 +186,30 @@ logFM sev msg = do
185186-- | 'Loc'-tagged logging when using template-haskell. Automatically
186187-- supplies payload and namespace.
187188--
188−-- @logTM InfoS "Hello world"@
189−logTM :: (Applicative m, KatipContext m, Katip m)
190− => Severity
191− -> LogStr
192− -> m ()
193−logTM = logItemM getLoc
189+-- @$(logTM) InfoS "Hello world"@
190+logTM :: ExpQ
191+logTM = [| logItemM (Just $(getLocTH)) |]
192+
193+
194+-------------------------------------------------------------------------------
195+-- | 'Loc'-tagged logging when using template-haskell. Automatically
196+-- supplies payload and namespace.
197+--
198+-- Same consideration as `logLoc` applies.
199+--
200+-- This function does not require template-haskell as it
201+-- automatically uses <https://hackage.haskell.org/package/base-4.8.2.0/docs/GHC-Stack.html#v:getCallStack implicit-callstacks>
202+-- when the code is compiled using GHC > 7.8. Using an older version of the
203+-- compiler will result in the emission of a log line without any location information,
204+-- so be aware of it. Users using GHC <= 7.8 may want to use the template-haskell function
205+-- `logTM` for maximum compatibility.
206+--
207+-- @logLocM InfoS "Hello world"@
208+logLocM :: (Applicative m, KatipContext m, Katip m)
209+ => Severity
210+ -> LogStr
211+ -> m ()
212+logLocM = logItemM getLoc
194213
195214
196215-------------------------------------------------------------------------------
@@ -222,7 +241,7 @@ logExceptionM action sev = action `catchAll` \e -> f e >> throwM e
222241-- ctx <- getKatipContext
223242-- ns <- getKatipNamespace
224243-- forkIO $ runKatipContextT le ctx ns $ do
225−-- logTM InfoS "Look, I can log in IO and retain context!"
244+-- $(logTM) InfoS "Look, I can log in IO and retain context!"
226245-- doOtherStuff
227246-- @
228247newtype KatipContextT m a = KatipContextT {