module Data.MediaBus.Conduit.Trace
( exitAfterC
, traceShowC
, traceShowSink
) where
import Conduit
import Data.Conduit.List
import Control.Monad.State.Strict as State
import Debug.Trace
import System.Random
traceShowC :: (Show a, Monad m) => Double -> String -> Conduit a m a
traceShowC probability msg =
evalStateC (mkStdGen 100, 0 :: Integer) $
awaitForever $
\x -> do
(g, omitted) <- State.get
let (p, g') = randomR (0, 1) g
if p < probability
then do
let omittedmsg = if omitted == 0
then ""
else " *** " ++
show omitted ++
" messages omitted"
traceM ((if null msg then "" else msg ++ ": ") ++ show x ++
omittedmsg)
State.put (g', 0)
else State.put (g', omitted + 1)
yield x
traceShowSink :: (Show a, Monad m) => Double -> String -> Consumer a m [a]
traceShowSink probability msg =
traceShowC probability msg .| consume
exitAfterC :: Monad m => Int -> Conduit a m a
exitAfterC 0 = return ()
exitAfterC n = await >>= maybe (return ()) (yield >=> const (exitAfterC (n 1)))