[project @ 2002-10-11 11:05:20 by malcolm]
[ghc-base.git] / Debug / Trace.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Debug.Trace
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  provisional
9 -- Portability :  portable
10 --
11 -- The 'trace' function.
12 --
13 -----------------------------------------------------------------------------
14
15 module Debug.Trace (
16         -- * Tracing
17         trace -- :: String -> a -> a
18   ) where
19
20 import Prelude
21 import System.IO.Unsafe
22 import System.IO
23
24 #ifdef __GLASGOW_HASKELL__
25 import GHC.IOBase
26 import GHC.Handle
27 #endif
28
29 #ifdef __HUGS__
30 import Hugs.IOExts
31 #endif
32
33 #ifdef __GLASGOW_HASKELL__
34 {-# NOINLINE trace #-}
35 {-|
36 When called, 'trace' prints the string in its first argument to
37 standard error, before returning the second argument as its result.
38 The 'trace' function is not referentially transparent, and should only
39 be used for debugging, or for monitoring execution. Some
40 implementations of 'trace' may decorate the string that\'s output to
41 indicate that you\'re tracing.
42 -}
43 trace :: String -> a -> a
44 trace string expr = unsafePerformIO $ do
45     hPutStr stderr string
46     hPutChar stderr '\n'
47     fd <- withHandle_ "trace" stderr $ (return.haFD)
48     postTraceHook fd
49     return expr
50
51 foreign import ccall "PostTraceHook" postTraceHook :: Int -> IO ()
52 #endif
53
54 #ifdef __NHC__
55 trace :: String -> a -> a
56 trace str expr = unsafePerformIO $ do hPutStr stderr str; return expr
57 #endif