diff options
author | jim@bo <jim@bo> | 2018-06-22 02:42:16 -0400 |
---|---|---|
committer | jim@bo <jim@bo> | 2018-06-22 02:45:15 -0400 |
commit | a4af87c425950ff6697a67ae5acb7abd2108580f (patch) | |
tree | 6034d0354558bbce9d81d853103567b704b50de0 /src | |
parent | babb35615121227e0ad2123d211fd138b6fe55c0 (diff) |
Avoid MVar access in dput & dtrace
Diffstat (limited to 'src')
-rw-r--r-- | src/DPut.hs | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/src/DPut.hs b/src/DPut.hs index 94a237f8..73d142b7 100644 --- a/src/DPut.hs +++ b/src/DPut.hs | |||
@@ -3,13 +3,15 @@ module DPut where | |||
3 | import Control.Monad.IO.Class | 3 | import Control.Monad.IO.Class |
4 | import Control.Concurrent.STM | 4 | import Control.Concurrent.STM |
5 | import qualified Data.Map.Strict as Map | 5 | import qualified Data.Map.Strict as Map |
6 | import System.IO (stderr,hPutStrLn) | ||
7 | import Data.Maybe | 6 | import Data.Maybe |
7 | import Data.IORef | ||
8 | import System.IO (stderr,hPutStrLn) | ||
8 | import System.IO.Unsafe (unsafePerformIO) | 9 | import System.IO.Unsafe (unsafePerformIO) |
9 | import System.Log.Logger | 10 | import System.Log.Logger |
10 | import qualified Data.ByteString.Char8 as B | 11 | import qualified Data.ByteString.Char8 as B |
11 | import qualified Data.Text as T | 12 | import qualified Data.Text as T |
12 | import qualified Data.Text.Encoding as T | 13 | import qualified Data.Text.Encoding as T |
14 | import Debug.Trace | ||
13 | 15 | ||
14 | -- | Debug Tags, add more as needed, but ensure XAnnounce is always first, XMisc last | 16 | -- | Debug Tags, add more as needed, but ensure XAnnounce is always first, XMisc last |
15 | data DebugTag | 17 | data DebugTag |
@@ -41,16 +43,29 @@ dput tag msg = liftIO $ debugM (appName <.> show tag) msg | |||
41 | dputB :: MonadIO m => DebugTag -> B.ByteString -> m () | 43 | dputB :: MonadIO m => DebugTag -> B.ByteString -> m () |
42 | dputB tag msg = liftIO $ debugM (appName <.> show tag) (T.unpack . T.decodeUtf8 $ msg) | 44 | dputB tag msg = liftIO $ debugM (appName <.> show tag) (T.unpack . T.decodeUtf8 $ msg) |
43 | 45 | ||
46 | {-# NOINLINE verbosityMap #-} | ||
47 | verbosityMap :: IORef (Map.Map DebugTag Bool) | ||
48 | verbosityMap = unsafePerformIO $ newIORef (Map.empty) | ||
49 | |||
44 | -- | Trace version of 'dput' works in arbitrary monad, using unsafePerformIO. | 50 | -- | Trace version of 'dput' works in arbitrary monad, using unsafePerformIO. |
45 | tput :: Applicative m => DebugTag -> String -> m () | 51 | tput :: Applicative m => DebugTag -> String -> m () |
46 | tput tag msg = (unsafePerformIO $ dput tag msg) `seq` pure () | 52 | tput tag msg = |
53 | let mp = unsafePerformIO $ readIORef verbosityMap | ||
54 | in if fromMaybe True (Map.lookup tag mp) | ||
55 | then trace msg (pure ()) | ||
56 | else pure () | ||
47 | 57 | ||
48 | -- | like 'trace' but parameterized with 'DebugTag' | 58 | -- | like 'trace' but parameterized with 'DebugTag' |
49 | dtrace :: DebugTag -> String -> a -> a | 59 | dtrace :: DebugTag -> String -> a -> a |
50 | dtrace tag msg result = (unsafePerformIO $ dput tag msg) `seq` result | 60 | dtrace tag msg result = let mp = unsafePerformIO $ readIORef verbosityMap |
61 | in if fromMaybe True (Map.lookup tag mp) | ||
62 | then trace msg result | ||
63 | else result | ||
51 | 64 | ||
52 | setTagLevel :: Priority -> DebugTag -> IO () | 65 | setTagLevel :: Priority -> DebugTag -> IO () |
53 | setTagLevel level tag = updateGlobalLogger (appName <.> show tag) (setLevel level) | 66 | setTagLevel level tag = do |
67 | updateGlobalLogger (appName <.> show tag) (setLevel level) | ||
68 | modifyIORef verbosityMap (Map.insert tag (level <= DEBUG)) | ||
54 | 69 | ||
55 | setQuiet :: DebugTag -> IO () | 70 | setQuiet :: DebugTag -> IO () |
56 | setQuiet = setTagLevel WARNING | 71 | setQuiet = setTagLevel WARNING |