module Text.XML.HXT.Arrow.XmlState.TraceHandling
where
import Control.Arrow
import Control.Arrow.ArrowList
import Control.Arrow.ArrowIf
import Control.Arrow.ArrowTree
import Control.Arrow.ArrowIO
import System.IO ( hPutStrLn
, hFlush
, stderr
)
import Text.XML.HXT.DOM.Interface
import Text.XML.HXT.Arrow.XmlArrow
import Text.XML.HXT.Arrow.XmlState.TypeDefs
import Text.XML.HXT.Arrow.XmlState.SystemConfig
import Text.XML.HXT.Arrow.Edit ( addHeadlineToXmlDoc
, treeRepOfXmlDoc
, indentDoc
)
setTraceLevel :: Int -> IOStateArrow s b b
setTraceLevel l = configSysVar $ withTrace l
getTraceLevel :: IOStateArrow s b Int
getTraceLevel = getSysVar theTraceLevel
setTraceCmd :: (Int -> String -> IO ()) -> IOStateArrow s b b
setTraceCmd c = configSysVar $ setS theTraceCmd c
getTraceCmd :: IOStateArrow a b (Int -> String -> IO ())
getTraceCmd = getSysVar theTraceCmd
withTraceLevel :: Int -> IOStateArrow s b c -> IOStateArrow s b c
withTraceLevel level f = localSysEnv $ setTraceLevel level >>> f
trace :: Int -> IOStateArrow s b String -> IOStateArrow s b b
trace level trc = perform ( trc
>>>
( getTraceCmd &&& this )
>>>
arrIO (\ (cmd, msg) -> cmd level msg)
)
`when` ( getTraceLevel
>>>
isA (>= level)
)
traceValue :: Int -> (b -> String) -> IOStateArrow s b b
traceValue level trc = trace level (arr $ (('-' : "- (" ++ show level ++ ") ") ++) . trc)
traceString :: Int -> (b -> String) -> IOStateArrow s b b
traceString = traceValue
traceMsg :: Int -> String -> IOStateArrow s b b
traceMsg level msg = traceValue level (const msg)
traceSource :: IOStateArrow s XmlTree XmlTree
traceSource = trace 3 $
xshow $
choiceA [ isRoot :-> ( indentDoc
>>>
getChildren
)
, isElem :-> ( root [] [this]
>>> indentDoc
>>> getChildren
>>> isElem
)
, this :-> this
]
traceTree :: IOStateArrow s XmlTree XmlTree
traceTree = trace 4 $
xshow $
treeRepOfXmlDoc
>>>
addHeadlineToXmlDoc
>>>
getChildren
traceDoc :: String -> IOStateArrow s XmlTree XmlTree
traceDoc msg = traceMsg 1 msg
>>>
traceSource
>>>
traceTree
traceOutputToStderr :: Int -> String -> IO ()
traceOutputToStderr _level msg
= do
hPutStrLn stderr msg
hFlush stderr