/
TraceHandling.hs
150 lines (111 loc) · 5.26 KB
/
TraceHandling.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
-- ------------------------------------------------------------
{- |
Module : Text.XML.HXT.Arrow.XmlState.TraceHandling
Copyright : Copyright (C) 2010 Uwe Schmidt
License : MIT
Maintainer : Uwe Schmidt (uwe@fh-wedel.de)
Stability : stable
Portability: portable
the trace arrows
-}
-- ------------------------------------------------------------
module Text.XML.HXT.Arrow.XmlState.TraceHandling
where
import Control.Arrow -- arrow classes
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
)
-- ------------------------------------------------------------
-- | set the global trace level
setTraceLevel :: Int -> IOStateArrow s b b
setTraceLevel l = configSysVar $ withTrace l
-- | read the global trace level
getTraceLevel :: IOStateArrow s b Int
getTraceLevel = getSysVar theTraceLevel
-- | set the global trace command. This command does the trace output
setTraceCmd :: (Int -> String -> IO ()) -> IOStateArrow s b b
setTraceCmd c = configSysVar $ setS theTraceCmd c
-- | acces the command for trace output
getTraceCmd :: IOStateArrow a b (Int -> String -> IO ())
getTraceCmd = getSysVar theTraceCmd
-- | run an arrow with a given trace level, the old trace level is restored after the arrow execution
withTraceLevel :: Int -> IOStateArrow s b c -> IOStateArrow s b c
withTraceLevel level f = localSysEnv $ setTraceLevel level >>> f
-- | apply a trace arrow and issue message to stderr
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)
)
-- | trace the current value transfered in a sequence of arrows.
--
-- The value is formated by a string conversion function. This is a substitute for
-- the old and less general traceString function
traceValue :: Int -> (b -> String) -> IOStateArrow s b b
traceValue level trc = trace level (arr $ (('-' : "- (" ++ show level ++ ") ") ++) . trc)
-- | an old alias for 'traceValue'
traceString :: Int -> (b -> String) -> IOStateArrow s b b
traceString = traceValue
-- | issue a string message as trace
traceMsg :: Int -> String -> IOStateArrow s b b
traceMsg level msg = traceValue level (const msg)
-- | issue the source representation of a document if trace level >= 3
--
-- for better readability the source is formated with indentDoc
traceSource :: IOStateArrow s XmlTree XmlTree
traceSource = trace 3 $
xshow $
choiceA [ isRoot :-> ( indentDoc
>>>
getChildren
)
, isElem :-> ( root [] [this]
>>> indentDoc
>>> getChildren
>>> isElem
)
, this :-> this
]
-- | issue the tree representation of a document if trace level >= 4
traceTree :: IOStateArrow s XmlTree XmlTree
traceTree = trace 4 $
xshow $
treeRepOfXmlDoc
>>>
addHeadlineToXmlDoc
>>>
getChildren
-- | trace a main computation step
-- issue a message when trace level >= 1, issue document source if level >= 3, issue tree when level is >= 4
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
-- ----------------------------------------------------------