How to best add logging/debugging code?

Simon Marlow simonmar at microsoft.com
Thu Nov 27 12:38:09 EST 2003


 
> Manuel M T Chakravarty wrote:
> 
> > There is `Debug.Trace.trace' for this:
> > 
> >   http://haskell.org/ghc/docs/latest/html/base/Debug.Trace.html
> > 
> > However, if you want to log as opposed to debug, you may
> > want to have the output go to somewhere else but stdout.
> > Hence, it might be useful to have a variant of the `trace'
> > that takes an explicit file handle in the library.
> 
> Is there a list of problems anywhere with using trace?  For 
> example does it affect evaluation order?

You can think of the meaning of trace like this:

    trace e1 e2   <=>  foldr seq () e1 `seq` e2

in other words, it deepSeqs the first argument and then behaves as the
second argument.

If you insert a trace with a literal string into your program, eg.

    trace "calling f"  (f x)

then it has no effect on the meaning of the program.  Trace will only
have an effect on the semantics of the program when you print out a
value in the program:

   trace ("calling f with " ++ show x) (f x)

in which case it will cause the value (x in this case) to be evaluated
earlier than it would otherwise.  If x turned out to be _|_, then the
program might fail when it otherwise might not have.

Cheers,
	Simon


More information about the Haskell mailing list