module Colog.Syslog.Priority
(
Priority (..)
, priorityValue
, Severity (..)
, severityCode
, Facility (..)
, facilityCode
) where
import Universum
import Data.Aeson (FromJSON(..), ToJSON(..), Value (..), withText, withObject,
(.:), (.=), object)
import Data.Bits ((.|.), shiftL)
import Fmt (Buildable (build), fmt, (+|), (|+))
import Text.Show (Show (show))
data Priority = Priority Facility Severity
deriving (Eq, Read)
instance Buildable Priority where
build p = "<"+|(priorityValue p)|+">"
instance Show Priority where
show = fmt . build
instance FromJSON Priority where
parseJSON = withObject "Priority" $ \v -> Priority
<$> v .: "facility"
<*> v .: "severity"
instance ToJSON Priority where
toJSON (Priority facility severity) = object
[ "facility" .= facility
, "severity" .= severity
]
priorityValue :: Priority -> Int
priorityValue (Priority fac sev) = facilityCode fac .|. severityCode sev
data Severity
= Emergency
| Alert
| Critical
| Error
| Warning
| Notice
| Info
| Debug
deriving (Eq, Show, Read, Bounded, Enum)
instance Ord Severity where
compare l r = compare (fromEnum r) (fromEnum l)
instance Buildable Severity where
build = \case
Emergency -> "[Emergency] "
Alert -> "[Alert] "
Critical -> "[Critical] "
Error -> "[Error] "
Warning -> "[Warning] "
Notice -> "[Notice] "
Info -> "[Info] "
Debug -> "[Debug] "
instance FromJSON Severity where
parseJSON = withText "Severity" $ \t ->
maybe (fail $ "Unknown Severity: \""+|t|+"\"") pure . readMaybe $ toString t
instance ToJSON Severity where
toJSON = String . Universum.show
severityCode :: Severity -> Int
severityCode = fromEnum
data Facility
= Kernel
| User
| Mail
| Daemon
| Auth
| Syslog
| Lpr
| News
| Uucp
| Cron
| AuthPriv
| Ftp
| Ntp
| LogAudit
| LogAlert
| Clock
| Local0
| Local1
| Local2
| Local3
| Local4
| Local5
| Local6
| Local7
deriving (Eq, Show, Read, Bounded, Enum)
instance FromJSON Facility where
parseJSON = withText "Facility" $ \t ->
maybe (fail $ "Unknown Facility: \""+|t|+"\"") pure . readMaybe $ toString t
instance ToJSON Facility where
toJSON = String . Universum.show
facilityCode :: Facility -> Int
facilityCode fac = shiftL (fromEnum fac) 3