8 import Control.Concurrent
10 -- The below logger implementation has been taken from
11 -- Parallel and Concurrent Programming in Haskell, Chapter 7
12 -- The logger is implemented in a concurrent thread.
14 -- Here the (MVar LogCommand) is used for actual thread communication
15 -- So if multiple threads try to log, then the logger will be thread-safe
16 -- Also the 'loop' in logger will wait for the message to come.
18 -- The MVar in stop is just to ensure the logger thread executes completely
19 -- Before exiting the main application.
21 data Logger = Logger (MVar LogCommand)
22 data LogCommand = Message String | Stop (MVar ())
24 initLogger :: IO Logger
28 _ <- forkIO (logger l)
31 logger :: Logger -> IO ()
32 logger (Logger m) = loop
38 -- We can alternatively put the message to a file
43 putStrLn "FuncTorrent: Exit succesfully"
46 -- Send log message to logger
47 logMessage :: Logger -> String -> IO ()
48 logMessage (Logger m) s = putMVar m (Message s)
50 logStop :: Logger -> IO ()
51 logStop (Logger m) = do
54 -- Wait for logger to complete the logging