co-log-0.5.0.0: tutorials/Concurrent.hs
{- | The purpose of this module is to check concurrent abilities of @co-log@.
-}
module Main (main) where
import Control.Concurrent (forkIO, threadDelay)
import Control.Monad (forM_, replicateM_, void)
import Data.ByteString (ByteString)
import Data.ByteString.Char8 (pack)
import Prelude hiding (log)
import Colog (LogAction, logByteStringStderr, logByteStringStdout, (<&))
{- | Action that prints predefined message 5 times.
-}
action :: LogAction IO ByteString -> ByteString -> IO ()
action log msg = replicateM_ 5 $ log <& msg
{- | Checks @colog@ for interleaved output. Forks 10 threads, each of them runs
'action'.
-}
main :: IO ()
main = do
forM_ [1..10 :: Int] $ \i -> do
let log = if even i then logByteStringStdout else logByteStringStderr
void $ forkIO $ action log $ "Logging from thread: " <> pack (show i)
threadDelay $ 3 * (10 ^ (6 :: Int)) -- wait 3 seconds