hslogger-template (empty) → 0.1
raw patch · 3 files changed
+116/−0 lines, 3 filesdep +basedep +haskell98dep +hsloggersetup-changed
Dependencies added: base, haskell98, hslogger, template-haskell
Files
- Setup.lhs +4/−0
- hslogger-template.cabal +23/−0
- src/System/Log/Logger/TH.hs +89/−0
+ Setup.lhs view
@@ -0,0 +1,4 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> main = defaultMain
+ hslogger-template.cabal view
@@ -0,0 +1,23 @@+name: hslogger-template+version: 0.1+synopsis: Automatic generation of hslogger functions+description:+ A function for generating hslogger functions automatically using Template+ Haskell.++license: PublicDomain+author: Brian Lewis <brian@lorf.org>, Ian Taylor <ian@lorf.org>+maintainer: Brian Lewis <brian@lorf.org>, Ian Taylor <ian@lorf.org>+category: Interfaces++cabal-version: >= 1.6+build-type: Simple++library+ exposed-modules: System.Log.Logger.TH+ build-depends: base, haskell98, hslogger, template-haskell+ extensions: TemplateHaskell+ hs-source-dirs: src+ ghc-options: -Wall+ if impl(ghc >= 6.8)+ ghc-options: -fwarn-tabs
+ src/System/Log/Logger/TH.hs view
@@ -0,0 +1,89 @@+-- | This module provides a function that generates hslogger functions+-- automatically using Template Haskell.+module System.Log.Logger.TH (deriveLoggers) where++import qualified Language.Haskell.TH as TH++import qualified System.Log.Logger as HSL++-- | Generate hslogger functions for given priorities.+--+-- Example usage:+--+-- > module Foo.Bar ( ... ) where+-- >+-- > import System.Log.Logger.TH (deriveLoggers)+-- > import qualified System.Log.Logger as HSL+-- >+-- > $(deriveLoggers "HSL" [HSL.DEBUG, HSL.INFO])+--+-- Used this way, @deriveLoggers@ would generate the following functions:+--+-- > info :: String -> IO ()+-- > info s = HSL.infoM "Foo.Bar" ((++) "Foo.Bar: " s)+-- >+-- > debug :: String -> IO ()+-- > debug s = HSL.debugM "Foo.Bar" ((++) "Foo.Bar: " s)+--+-- The other hslogger priorities follow the same pattern.+--+-- So+--+-- > info "hi there"+--+-- would generate the INFO-level log event+--+-- > Foo.Bar: hi there+--+-- Note: "System.Log.Logger" must be imported qualified, and the qualifier must+-- match the qualifier given to @deriveLoggers@.+--+deriveLoggers+ :: String -- ^ Must match qualifier on import of "System.Log.Logger".+ -> [HSL.Priority] -- ^ List of priorities for which to generate logging functions.+ -> TH.Q [TH.Dec]+deriveLoggers qualifier priorities =+ fmap TH.loc_module TH.location >>= \moduleName ->+ fmap concat (mapM (deriveLogger qualifier moduleName) priorities)++-- ----------------------------------------++deriveLogger :: String -> String -> HSL.Priority -> TH.Q [TH.Dec]+deriveLogger qualifier moduleName priority = code+ where+ code = do+ sig <- TH.sigD th_f [t| String -> IO () |]+ body <- TH.funD th_f+ [ TH.clause [TH.varP th_s]+ (TH.normalB+ (TH.appE+ (TH.appE+ (TH.varE th_h)+ (TH.stringE moduleName)+ )+ (TH.appE+ (TH.appE+ (TH.varE '(++))+ (TH.litE (TH.stringL (moduleName ++ ": ")))+ )+ (TH.varE th_s)+ )+ )+ )+ []+ ]+ return [sig, body]+ where+ th_f = TH.mkName functionName+ th_h = TH.mkName (concat [qualifier, ".", functionName, "M"])+ th_s = TH.mkName "s"+ functionName =+ case priority of+ HSL.DEBUG -> "debug"+ HSL.INFO -> "info"+ HSL.NOTICE -> "notice"+ HSL.WARNING -> "warning"+ HSL.ERROR -> "error"+ HSL.CRITICAL -> "critical"+ HSL.ALERT -> "alert"+ HSL.EMERGENCY -> "emergency"