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