{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE UnicodeSyntax #-}
module System.LogLevel
( LogLevel(..)
, logLevelToText
, logLevelFromText
) where
import Control.DeepSeq
import Data.Char
import Data.String
import qualified Data.Text as T
import GHC.Generics
data LogLevel
= Quiet
| Error
| Warn
| Info
| Debug
| Other T.Text
deriving (Show, Read, Eq, Ord, Generic)
instance NFData LogLevel
instance IsString LogLevel where
fromString s = case toLower <$> s of
"quiet" → Quiet
"error" → Error
"warn" → Warn
"info" → Info
"debug" → Debug
_ → Other (T.pack s)
logLevelToText ∷ IsString a ⇒ LogLevel → a
logLevelToText (Other t) = fromString $ T.unpack t
logLevelToText l = fromString $ toLower <$> show l
logLevelFromText ∷ T.Text → LogLevel
logLevelFromText = fromString . T.unpack