{-# LANGUAGE RecordWildCards #-}

module Logging.Types.Manager
  ( Manager(..)
  , initialize
  , terminate
  ) where

import           Data.List                   (nub)
import           Data.Map.Lazy               (Map, elems)

import           Logging.Types.Class.Handler
import           Logging.Types.Sink


-- |There is __under normal circumstances__ just one Manager,
-- which holds the hierarchy of sinks.
data Manager = Manager { root                   :: Sink
                       , sinks                  :: Map String Sink
                       , disabled               :: Bool
                       , catchUncaughtException :: Bool
                       }


-- | Initialize a 'Manager', open all its handlers.
initialize :: Manager -> IO ()
initialize Manager{..} = mapM_ open $
  nub $ concat [ handlers | Sink{..} <- (root : (elems sinks)) ]


-- | Terminate a 'Manager', close all its handlers.
terminate :: Manager -> IO ()
terminate Manager{..} = mapM_ close $
  nub $ concat [ handlers | Sink{..} <- (root : (elems sinks)) ]