module Lambdabot.Logging
    ( L.Priority(..)
    , MonadLogging(..)
    , debugM
    , infoM
    , noticeM
    , warningM
    , errorM
    , criticalM
    , alertM
    , emergencyM
    ) where

import Control.Monad
import Data.List
import qualified System.Log.Logger as L

class Monad m => MonadLogging m where
    getCurrentLogger :: m [String]
    logM :: String -> L.Priority -> String -> m ()

instance MonadLogging IO where
    getCurrentLogger :: IO [String]
getCurrentLogger = [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
    logM :: String -> Priority -> String -> IO ()
logM = String -> Priority -> String -> IO ()
L.logM

getCurrentLoggerName :: MonadLogging m => m String
getCurrentLoggerName :: m String
getCurrentLoggerName = ([String] -> String) -> m [String] -> m String
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
"." ([String] -> String)
-> ([String] -> [String]) -> [String] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (String -> Bool) -> String -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null)) m [String]
forall (m :: * -> *). MonadLogging m => m [String]
getCurrentLogger

debugM :: MonadLogging m => String -> m ()
debugM :: String -> m ()
debugM String
msg = do
    String
logger <- m String
forall (m :: * -> *). MonadLogging m => m String
getCurrentLoggerName
    String -> Priority -> String -> m ()
forall (m :: * -> *).
MonadLogging m =>
String -> Priority -> String -> m ()
logM String
logger Priority
L.DEBUG String
msg

infoM :: MonadLogging m => String -> m ()
infoM :: String -> m ()
infoM String
msg = do
    String
logger <- m String
forall (m :: * -> *). MonadLogging m => m String
getCurrentLoggerName
    String -> Priority -> String -> m ()
forall (m :: * -> *).
MonadLogging m =>
String -> Priority -> String -> m ()
logM String
logger Priority
L.INFO String
msg

noticeM :: MonadLogging m => String -> m ()
noticeM :: String -> m ()
noticeM String
msg = do
    String
logger <- m String
forall (m :: * -> *). MonadLogging m => m String
getCurrentLoggerName
    String -> Priority -> String -> m ()
forall (m :: * -> *).
MonadLogging m =>
String -> Priority -> String -> m ()
logM String
logger Priority
L.NOTICE String
msg

warningM :: MonadLogging m => String -> m ()
warningM :: String -> m ()
warningM String
msg = do
    String
logger <- m String
forall (m :: * -> *). MonadLogging m => m String
getCurrentLoggerName
    String -> Priority -> String -> m ()
forall (m :: * -> *).
MonadLogging m =>
String -> Priority -> String -> m ()
logM String
logger Priority
L.WARNING String
msg

errorM :: MonadLogging m => String -> m ()
errorM :: String -> m ()
errorM String
msg = do
    String
logger <- m String
forall (m :: * -> *). MonadLogging m => m String
getCurrentLoggerName
    String -> Priority -> String -> m ()
forall (m :: * -> *).
MonadLogging m =>
String -> Priority -> String -> m ()
logM String
logger Priority
L.ERROR String
msg

criticalM :: MonadLogging m => String -> m ()
criticalM :: String -> m ()
criticalM String
msg = do
    String
logger <- m String
forall (m :: * -> *). MonadLogging m => m String
getCurrentLoggerName
    String -> Priority -> String -> m ()
forall (m :: * -> *).
MonadLogging m =>
String -> Priority -> String -> m ()
logM String
logger Priority
L.CRITICAL String
msg

alertM :: MonadLogging m => String -> m ()
alertM :: String -> m ()
alertM String
msg = do
    String
logger <- m String
forall (m :: * -> *). MonadLogging m => m String
getCurrentLoggerName
    String -> Priority -> String -> m ()
forall (m :: * -> *).
MonadLogging m =>
String -> Priority -> String -> m ()
logM String
logger Priority
L.ALERT String
msg

emergencyM :: MonadLogging m => String -> m ()
emergencyM :: String -> m ()
emergencyM String
msg = do
    String
logger <- m String
forall (m :: * -> *). MonadLogging m => m String
getCurrentLoggerName
    String -> Priority -> String -> m ()
forall (m :: * -> *).
MonadLogging m =>
String -> Priority -> String -> m ()
logM String
logger Priority
L.EMERGENCY String
msg