summaryrefslogtreecommitdiff
path: root/src/DPut.hs
blob: 482db515031d24a5d05c31efb4b7d7778ef6532b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module DPut where

import Control.Concurrent.STM
import qualified Data.Map.Strict as Map
import System.IO (stderr,hPutStrLn)
import Data.Maybe
import System.IO.Unsafe (unsafePerformIO)

data DebugTag = XAnnounce | XDHT | XOnion | XNetCrypto | XPing | XLan | XMisc
    deriving (Eq,Ord,Show,Read)

{-# NOINLINE verbosityMap #-}
verbosityMap :: TVar (Map.Map DebugTag Bool)
verbosityMap = unsafePerformIO $ newTVarIO (Map.empty)

dput :: DebugTag -> String -> IO ()
dput tag msg
    = do
       mp <- atomically $ readTVar verbosityMap
       if fromMaybe True (Map.lookup tag mp)
        then System.IO.hPutStrLn stderr msg
        else return ()

setQuiet :: DebugTag -> IO ()
setQuiet tag = atomically $ modifyTVar' (verbosityMap) (Map.insert tag False)

setVerbose :: DebugTag -> IO ()
setVerbose tag = atomically $ modifyTVar' (verbosityMap) (Map.insert tag True)