{- |
   Maintainer:  simons@cryp.to
   Stability:   provisional
   Portability: POSIX

   FFI bindings to @syslog(3)@ from
   <http://pubs.opengroup.org/onlinepubs/9699919799/functions/syslog.html POSIX.1-2008>.
   This module is intended for purposes of low-level implementation. Users of
   this library should prefer safer and more convenient API provided by
   "System.Posix.Syslog".
-}

module System.Posix.Syslog.LogMask where

import System.Posix.Syslog.Functions ( _logMask )
import System.Posix.Syslog.Priority ( Priority, fromPriority )

import Data.Bits
import Foreign.C.Types

-- | Convert a set of logging priorities into a system-dependent binary
-- representation suitable for calling '_setlogmask'.

toLogMask :: [Priority] -> CInt
toLogMask :: [Priority] -> CInt
toLogMask = (Priority -> CInt -> CInt) -> CInt -> [Priority] -> CInt
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (CInt -> CInt -> CInt
forall a. Bits a => a -> a -> a
(.|.) (CInt -> CInt -> CInt)
-> (Priority -> CInt) -> Priority -> CInt -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> CInt
_logMask (CInt -> CInt) -> (Priority -> CInt) -> Priority -> CInt
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Priority -> CInt
fromPriority) CInt
0

-- | Decode the the system-dependent binary representation returned by
-- '_setlogmask' back into a set of logging priorities.

fromLogMask :: CInt -> [Priority]
fromLogMask :: CInt -> [Priority]
fromLogMask CInt
old = [ Priority
p | Priority
p <- [Priority
forall a. Bounded a => a
minBound..Priority
forall a. Bounded a => a
maxBound], CInt -> CInt
_logMask (Priority -> CInt
fromPriority Priority
p) CInt -> CInt -> CInt
forall a. Bits a => a -> a -> a
.&. CInt
old CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0 ]