{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Logging.Filter ( Filter(..), Filterer ) where
import Data.List (stripPrefix)
import Data.String
import Prelude hiding (filter)
import Logging.Class.Filterable
import Logging.Logger
import Logging.Record
data Filter = Filter Logger (Maybe (LogRecord -> Bool))
instance IsString Filter where
fromString = flip Filter Nothing
instance Read Filter where
readsPrec _ s = [(fromString s, "")]
instance Show Filter where
show (Filter logger _) = "Filter " ++ (show logger)
instance Eq Filter where
(Filter logger _) == (Filter logger' _) = logger == logger'
instance Filterable Filter where
filter (Filter logger' Nothing) rcd@LogRecord{..}
| logger' == "" = True
| otherwise = case stripPrefix logger' logger of
Just "" -> True
Just ('.':_) -> True
_ -> False
filter (Filter _ (Just f)) rcd = f rcd
type Filterer = [Filter]