foldBlocks initialBlockState = Control.Foldl.impurely Pipes.Prelude.foldM (blockFoldM initialBlockState)
but now we have it in a shape where we can snap in a logger function. We might do this
by hand writing something with this shape
loggerFold :: Handle -> Control.FoldM IO Block ()
loggerFold = FoldM step begin done where
step x block = undefined -- new 'IO x' whatever it is, probably IO ()
begin = undefined -- initial IO x, probably 'return ()'
done _ = return () -- given I assumed () in the signature
So it is a question of writing `step` to do whatever you want to be logged from each block.
Then you will just write:
foldBlocksWithLogging :: BlockState -> Producer Block IO () -> IO (Either String BlockState)
foldBlocksWithLogging initialBlockState =
Control.Foldl.impurely Pipes.Prelude.foldM (blockFoldM initialBlockState <* loggerFold)
or
foldBlocksWithLogging :: BlockState -> Producer Block IO () -> IO (Either String BlockState)
foldBlocksWithLogging initialBlockState =
Control.Foldl.impurely Pipes.Prelude.foldM (generalize (blockFold initialBlockState) <* loggerFold)
a sufficiently trivial logger can be written with
L.sink :: (Monad m, Monoid w) => (a -> m w) -> L.FoldM m a w
the ultra-minimal debugging fold would be
blockAlertFoldM :: FoldM IO Block ()
blockAlertFoldM = sink (\block -> putStrLn "Block Processed!")
then we have, e.g.
foldBlocksWithAlert :: BlockState -> Producer Block IO () -> IO (Either String BlockState)
foldBlocksWithAlert initialBlockState =
Control.Foldl.impurely Pipes.Prelude.foldM (generalize (blockFold initialBlockState) <* loggerFold)
and could start throwing in other components
foldBlocksWithAlertAndLength :: BlockState -> Producer Block IO () -> IO (Int, Either String BlockState)
foldBlocksWithAlertAndLengh initialBlockState = Control.Foldl.impurely Pipes.Prelude.foldM myfolds where
myfolds = generalize mypurefolds <* loggerFold
mypurefolds = liftA2 (,) Control.Foldl.length (blockFold initialBlockState)
Here we get the block count at the end, and are printing "Block processed!" to stdout as each block is processed.