Skip to content

Commit b49566a

Browse files
author
Tim Watson
committed
deal with handles explicitly when tracing to a file
1 parent 927d2bc commit b49566a

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

distributed-process/src/Control/Distributed/Process/Internal/Trace.hs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,19 @@ import Control.Concurrent.STM
1616
, writeTQueue
1717
, atomically
1818
)
19-
import Control.Distributed.Process.Internal.Types (forever', Tracer(..))
20-
import Control.Exception (catch, throwTo, AsyncException(ThreadKilled))
19+
import Control.Distributed.Process.Internal.Types
20+
( forever'
21+
, Tracer(..)
22+
)
23+
import Control.Exception
24+
( catch
25+
, throwTo
26+
, SomeException
27+
, AsyncException(ThreadKilled)
28+
)
2129
import Data.List (intersperse)
30+
import Data.Time.Clock (getCurrentTime)
31+
import Data.Time.Format (formatTime)
2232
import Debug.Trace (traceEventIO)
2333

2434
import Prelude hiding (catch)
@@ -27,9 +37,13 @@ import System.Environment (getEnv)
2737
import System.IO
2838
( Handle
2939
, IOMode(AppendMode)
30-
, withFile
31-
, hPutStr
40+
, BufferMode(..)
41+
, openFile
42+
, hClose
43+
, hPutStrLn
44+
, hSetBuffering
3245
)
46+
import System.Locale (defaultTimeLocale)
3347

3448
defaultTracer :: IO Tracer
3549
defaultTracer = do
@@ -39,23 +53,26 @@ defaultTracer = do
3953
logfileTracer :: FilePath -> IO Tracer
4054
logfileTracer p = do
4155
q <- newTQueueIO
42-
tid <- forkIO $ withFile p AppendMode (\h -> logger h q)
43-
return $ LogFileTracer tid q
56+
h <- openFile p AppendMode
57+
hSetBuffering h LineBuffering
58+
tid <- forkIO $ logger h q `catch` (\(_ :: SomeException) ->
59+
hClose h >> return ())
60+
return $ LogFileTracer tid q h
4461
where logger :: Handle -> TQueue String -> IO ()
4562
logger h q' = forever' $ do
4663
msg <- atomically $ readTQueue q'
47-
hPutStr h msg
48-
logger h q'
64+
now <- getCurrentTime
65+
hPutStrLn h $ msg ++ (formatTime defaultTimeLocale " - %c" now)
4966

50-
-- TODO: compatibility layer (conditional compilation?) for GHC/base versions
67+
-- TODO: compatibility layer for GHC/base versions (e.g., where's killThread?)
5168

52-
stopTracer :: Tracer -> IO ()
53-
stopTracer (LogFileTracer tid _) = throwTo tid ThreadKilled -- cf killThread
54-
stopTracer _ = return ()
69+
stopTracer :: Tracer -> IO () -- overzealous but harmless duplication of hClose
70+
stopTracer (LogFileTracer tid _ h) = throwTo tid ThreadKilled >> hClose h
71+
stopTracer _ = return ()
5572

5673
trace :: Tracer -> String -> IO ()
57-
trace (LogFileTracer _ q) msg = atomically $ writeTQueue q msg
58-
trace (EventLogTracer t) msg = t msg
74+
trace (LogFileTracer _ q _) msg = atomically $ writeTQueue q msg
75+
trace (EventLogTracer t) msg = t msg
5976

6077
traceFormat :: Tracer
6178
-> String

distributed-process/src/Control/Distributed/Process/Internal/Types.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ import Control.Distributed.Process.Internal.StrictMVar (StrictMVar)
110110
import Control.Distributed.Process.Internal.WeakTQueue (TQueue)
111111
import Control.Distributed.Static (RemoteTable, Closure)
112112
import qualified Control.Distributed.Process.Internal.StrictContainerAccessors as DAC (mapMaybe)
113+
import System.IO (Handle)
113114

114115
--------------------------------------------------------------------------------
115116
-- Node and process identifiers --
@@ -181,8 +182,8 @@ nullProcessId nid =
181182

182183
-- | Required for system tracing in the node controller
183184
data Tracer =
184-
LogFileTracer ThreadId (STM.TQueue String)
185-
| EventLogTracer (String -> IO ())
185+
LogFileTracer !ThreadId !(STM.TQueue String) !Handle
186+
| EventLogTracer !(String -> IO ())
186187

187188
-- | Local nodes
188189
data LocalNode = LocalNode

0 commit comments

Comments
 (0)