outputs during runtime

kahl@heraklit.informatik.unibw-muenchen.de kahl@heraklit.informatik.unibw-muenchen.de
30 Jul 2001 10:34:32 -0000


Nicole Gabler <gabler@ics-ag.de> wrote:

> During runtime I want the program to output =
> information about the status every time another file is parsed. That =
> doesn't work, the output appears much later, mostly not until end of =
> program. But I need these outputs at that specific time, so what can I =
> do to manage it?

Perhaps the following fragment from my prelude extensions helps.
It requires ``import IO''.

Most probably you would either be calling

> printList "\n" statusInformationList

or factor out the flushing part
for interleaving with your other IO actions.


Wolfram


%{{{ hPutList, printList
It is frequently annoying if a long-running program
writes a list of items to a channel or file,
and while it calculates the next item,
the last item has usually not yet been (completely) written
because at least part of it is still held in some output buffer.

The following functions flush the output buffer after every
item.
The most general variant, \verb|hPutList|,
is parameterised by a separator string for list items,
a \verb|shows| function for items,
and a handle for output.
The variant \verb|putList| writes always to the standard output channel,
and \verb|printList| in addition uses the \verb|shows| function
supplied by the type class system.

\begin{code}
hPutList :: String -> (a -> ShowS) -> Handle -> [a] -> IO ()
hPutList sep shows h = mapM_ f
 where f x = hPutStr h (shows x sep) >> hFlush h

putList :: String -> (a -> ShowS) -> [a] -> IO ()
putList sep shows = hPutList sep shows stdout

printList :: Show a => String -> [a] -> IO ()
printList sep = putList sep shows
\end{code}
%}}}