Skip to content

Commit 8375615

Browse files
committed
Simple handler: evaluate message before taking lock
Because of laziness, the `msg` string in `streamHandler` can contain arbitrary computations. In the previous state of affairs, the handler would first take the lock then `putStrLn` the message. So all the (pure) computation would happen in the critical section, and it couldn't guarantee timely release of the lock. Calling `deepseq` before the lock is taken makes sure that the locked section does only what it must do: print the string.
1 parent ca93778 commit 8375615

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

hslogger.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ library
6767
base >= 4.3 && < 4.13
6868
, bytestring >= 0.9 && < 0.11
6969
, containers >= 0.4 && < 0.7
70+
, deepseq
7071
, time >= 1.2 && < 1.10
7172
, old-locale >= 1.0 && < 1.1
7273

src/System/Log/Handler/Simple.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module System.Log.Handler.Simple(streamHandler, fileHandler,
1616
where
1717

1818
import Control.Exception (tryJust)
19+
import Control.DeepSeq
1920
import Data.Char (ord)
2021

2122
import System.Log
@@ -51,6 +52,7 @@ streamHandler :: Handle -> Priority -> IO (GenericHandler Handle)
5152
streamHandler h pri =
5253
do lock <- newMVar ()
5354
let mywritefunc hdl msg =
55+
msg `deepseq`
5456
withMVar lock (\_ -> do writeToHandle hdl msg
5557
hFlush hdl
5658
)

0 commit comments

Comments
 (0)