1 -----------------------------------------------------------------------------
3 -- Module : Debug.Trace
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- The 'trace' function.
13 -----------------------------------------------------------------------------
15 #ifdef __GLASGOW_HASKELL__
23 -- | The tracer is a function that monitors the trace messages.
24 fileTracer, -- :: Handle -> String -> IO ()
25 #ifdef mingw32_TARGET_OS
26 winDebugTracer, -- :: String -> IO ()
28 addTracer, -- :: String -> (String -> IO ()) -> IO ()
29 removeTracer, -- :: String -> IO ()
32 putTraceMsg, -- :: String -> IO ()
33 trace -- :: String -> a -> a
38 import System.IO.Unsafe
41 #ifdef mingw32_TARGET_OS
42 import Foreign.C.String
45 {-# NOINLINE tracers #-}
46 tracers :: IORef [(String, String -> IO ())]
47 tracers = unsafePerformIO (newIORef [("defaultTracer", fileTracer stderr)])
49 -- | A tracer function that outputs the message to a file
50 fileTracer :: Handle -- ^ file handle
51 -> String -- ^ trace message
53 fileTracer handle msg = do
57 #ifdef mingw32_TARGET_OS
58 -- | A tracer function that outputs the message to the debuger (Windows only)
59 winDebugTracer :: String -- ^ trace message
61 winDebugTracer msg = do
62 withCString (msg++"\n") outputDebugString
64 foreign import ccall unsafe "OutputDebugStringA"
65 outputDebugString :: CString -> IO ()
68 -- | Registering a new tracer
69 addTracer :: String -- ^ the tracer name
70 -> (String -> IO ()) -- ^ tracer
72 addTracer name tracer = do
73 ts <- readIORef tracers
74 writeIORef tracers ((name,tracer):filter (\(n,l) -> n /= name) ts)
76 -- | Removing the tracer with the given name
77 removeTracer :: String -> IO ()
78 removeTracer name = do
79 ts <- readIORef tracers
80 writeIORef tracers (filter (\(n,l) -> n /= name) ts)
82 -- | 'putTraceMsg' function outputs the trace message from IO monad.
83 putTraceMsg :: String -> IO ()
85 ts <- readIORef tracers
86 mapM_ (\(n,l) -> l msg) ts
88 {-# NOINLINE trace #-}
90 When called, 'trace' outputs the string in its first argument using the
91 installed tracers, before returning the second argument as its result.
92 The 'trace' function is not referentially transparent, and should only
93 be used for debugging, or for monitoring execution. Some
94 implementations of 'trace' may decorate the string that\'s output to
95 indicate that you\'re tracing.
97 trace :: String -> a -> a
98 trace string expr = unsafePerformIO $ do