module UHC.Light.Compiler.Base.Trace
( (><)
, TraceOn (..), allTraceOnMp
, TrPP, trppIsEmpty, trppEmpty
, trPPOnIO, trPP, trOnPP, trOn )
where
import UHC.Util.Pretty
import UHC.Util.Utils
import GHC.Generics (Generic)
import Data.Typeable
import Control.Monad
import Control.Monad.IO.Class
import qualified Data.Map as Map
import qualified Data.Sequence as Sq
import qualified Data.Foldable as Fld
import Data.Sequence ((><))
import UHC.Light.Compiler.Base.Common
data TraceOn
= TraceOn_BldFun
| TraceOn_BldFlow
| TraceOn_BldFPaths
| TraceOn_BldSearchPaths
| TraceOn_BldSccImports
| TraceOn_BldTypeables
| TraceOn_BldPipe
| TraceOn_BldPlan
| TraceOn_BldFold
| TraceOn_BldTimes
| TraceOn_BldResult
| TraceOn_BldImport
| TraceOn_BldRef
| TraceOn_BldMod
| TraceOn_HsScc
| TraceOn_HsDpd
| TraceOn_HsOcc
| TraceOn_EhClsGam
| TraceOn_EhDataGam
| TraceOn_EhValGam
| TraceOn_RunMod
| TraceOn_RunHeap
| TraceOn_RunGlobals
| TraceOn_RunFrame
| TraceOn_RunFrames
| TraceOn_RunEval
| TraceOn_RunRef
deriving (Eq,Ord,Enum,Show,Typeable,Bounded,Generic)
instance DataAndConName TraceOn
allTraceOnMp :: Map.Map String TraceOn
allTraceOnMp = str2stMpWithShow (strToLower . showUnprefixed 1)
type TrPP = Sq.Seq PP_Doc
trppIsEmpty :: TrPP -> Bool
trppIsEmpty = Sq.null
trppEmpty :: TrPP
trppEmpty = Sq.empty
instance PP TrPP where
pp = vlist . Fld.toList
trPP :: (TraceOn -> Bool) -> TraceOn -> [PP_Doc] -> TrPP
trPP onTr ton ms = if onTr ton then pr ms else trppEmpty
where pr [] = trppEmpty
pr [m] = Sq.singleton $ show ton >|< ":" >#< m
pr (m:ms) = pr [m] >< (Sq.fromList $ map (indent 2) ms)
trPPOnIO :: (Monad m, MonadIO m) => TrPP -> m ()
trPPOnIO ppl = liftIO $ mapM_ putPPLn $ Fld.toList ppl
trOnPP :: (Monad m, MonadIO m) => (TraceOn -> Bool) -> TraceOn -> [PP_Doc] -> m ()
trOnPP onTr ton ms = when (onTr ton) $ trPPOnIO $ trPP onTr ton ms
trOn :: (Monad m, MonadIO m) => (TraceOn -> Bool) -> TraceOn -> [String] -> m ()
trOn onTr ton ms = trOnPP onTr ton $ map pp ms