[BSD logger. Lemmih **20060110151918] { hunk ./Conjure.hs 33 +import Conjure.Logger hunk ./Conjure.hs 44 +-- FIXME: Logger options and log mask should be configurable. hunk ./Conjure.hs 46 + openlog [PError] "conjure" hunk ./Conjure.hs 49 + closelog hunk ./Conjure/Debug.hs 15 +import Conjure.Logger + hunk ./Conjure/Debug.hs 18 -debug = putStrLn +debug = syslog Debug addfile ./Conjure/Logger.hsc hunk ./Conjure/Logger.hsc 1 +----------------------------------------------------------------------------- +-- | +-- Module : Conjure.Logger +-- Copyright : (c) Lemmih 2006 +-- License : BSD-like +-- +-- Maintainer : lemmih@gmail.com +-- Stability : experimental +-- Portability : non-portable (POSIX) +-- +----------------------------------------------------------------------------- +-- FIXME: What do we do on Windows? +#include +module Conjure.Logger + ( Mask + , Option (..) + , Priority (..) + , setlogmask + , openlog + , syslog + , closelog + ) where + +import Foreign +import Foreign.C +import Data.Bits + +foreign import ccall unsafe "openlog" c_openlog :: CString -> Int -> Int -> IO () +foreign import ccall unsafe "syslog" c_syslog :: Int -> CString -> CString -> IO () +foreign import ccall unsafe "closelog" closelog :: IO () +foreign import ccall unsafe "setlogmask" c_setlogmask :: Int -> IO Int + +data Mask + = Bitmask [Priority] + | Upto Priority + +logMask p = 1 `shiftL` fromPriority p +logUpto p = 1 `shiftL` fromPriority p + +fromMask (Bitmask ps) + = foldr (.|.) 0 (map logMask ps) +fromMask (Upto p) + = logUpto p + +setlogmask :: Mask -> IO () +setlogmask mask + = do c_setlogmask (fromMask mask) + return () + +data Option + = Console + | NoDelay + | NoWait + | Delay + | PError + | PID + +fromOption Console = #{const LOG_CONS} +fromOption NoDelay = #{const LOG_NDELAY} +fromOption NoWait = #{const LOG_NOWAIT} +fromOption Delay = #{const LOG_ODELAY} +fromOption PError = #{const LOG_PERROR} +fromOption PID = #{const LOG_PID} + +openlog :: [Option] -> String -> IO () +openlog options ident + = withCString ident $ \cstr -> + c_openlog cstr (foldr (.|.) 0 (map fromOption options)) + #{const LOG_USER} + +data Priority + = Emergency + | Alert + | Critical + | Error + | Warning + | Notice + | Info + | Debug + +fromPriority :: Priority -> Int +fromPriority Emergency = #{const LOG_EMERG } +fromPriority Alert = #{const LOG_ALERT } +fromPriority Critical = #{const LOG_CRIT } +fromPriority Error = #{const LOG_ERR } +fromPriority Warning = #{const LOG_WARNING } +fromPriority Notice = #{const LOG_NOTICE } +fromPriority Info = #{const LOG_INFO } +fromPriority Debug= #{const LOG_DEBUG } + +syslog :: Priority -> String -> IO () +syslog priority msg + = withCString "%s" $ \format -> + withCString msg $ \cstr -> + c_syslog (fromPriority priority) format cstr + hunk ./conjure.cabal 8 +Other-modules: Conjure.Logger }