1 {-# LANGUAGE CPP, ForeignFunctionInterface #-}
3 -----------------------------------------------------------------------------
5 -- Module : Debug.Trace
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : provisional
11 -- Portability : portable
13 -- The 'trace' function.
15 -----------------------------------------------------------------------------
19 putTraceMsg, -- :: String -> IO ()
20 trace, -- :: String -> a -> a
25 import System.IO.Unsafe
27 #ifdef __GLASGOW_HASKELL__
28 import Foreign.C.String
30 import System.IO (hPutStrLn,stderr)
33 -- | 'putTraceMsg' function outputs the trace message from IO monad.
34 -- Usually the output stream is 'System.IO.stderr' but if the function is called
35 -- from Windows GUI application then the output will be directed to the Windows
37 putTraceMsg :: String -> IO ()
39 #ifndef __GLASGOW_HASKELL__
42 withCString "%s\n" $ \cfmt ->
43 withCString msg $ \cmsg ->
46 -- don't use debugBelch() directly, because we cannot call varargs functions
48 foreign import ccall unsafe "HsBase.h debugBelch2"
49 debugBelch :: CString -> CString -> IO ()
52 {-# NOINLINE trace #-}
54 When called, 'trace' outputs the string in its first argument, before
55 returning the second argument as its result. The 'trace' function is not
56 referentially transparent, and should only be used for debugging, or for
57 monitoring execution. Some implementations of 'trace' may decorate the string
58 that\'s output to indicate that you\'re tracing. The function is implemented on
61 trace :: String -> a -> a
62 trace string expr = unsafePerformIO $ do
67 Like 'trace', but uses 'show' on the argument to convert it to a 'String'.
69 > traceShow = trace . show
71 traceShow :: (Show a) => a -> b -> b
72 traceShow = trace . show