1 module FuncTorrent.Logger (
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.
19 data Logger = Logger (MVar LogCommand)
20 data LogCommand = Message String | Stop (MVar ())
22 initLogger :: IO Logger
26 _ <- forkIO (logger l)
29 logger :: Logger -> IO ()
30 logger (Logger m) = loop
36 -- We can alternatively put the message to a file
41 putStrLn "FuncTorrent: Exit succesfully"
44 -- Send log message to logger
45 logMessage :: Logger -> String -> IO ()
46 logMessage (Logger m) s = putMVar m (Message s)
48 logStop :: Logger -> IO ()
49 logStop (Logger m) = do
52 -- Wait for logger to complete the logging