keter/Keter/Logger.hs

117 lines
3.2 KiB
Haskell
Raw Normal View History

2012-05-17 10:32:11 +04:00
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
module Keter.Logger
( Logger
, start
, attach
, detach
2012-10-04 21:16:45 +04:00
, LogPipes (..)
, LogPipe
, mkLogPipe
2012-05-17 10:32:11 +04:00
, dummy
) where
import Keter.Prelude
import qualified Prelude as P
import qualified Keter.LogFile as LogFile
import Control.Concurrent (killThread)
import qualified Data.ByteString as S
2012-10-04 21:16:45 +04:00
import Data.Conduit (Sink, await)
import qualified Control.Concurrent.MVar as M
import Control.Monad.Trans.Class (lift)
2012-05-17 10:32:11 +04:00
2012-10-04 21:16:45 +04:00
data LogPipes = LogPipes
{ stdOut :: LogPipe
, stdErr :: LogPipe
2012-05-17 10:32:11 +04:00
}
2012-10-04 21:16:45 +04:00
data LogPipe = LogPipe
{ readLogPipe :: KIO (Maybe S.ByteString)
, closeLogPipe :: KIO ()
}
mkLogPipe :: KIO (LogPipe, Sink S.ByteString P.IO ())
mkLogPipe = do
toSink <- newEmptyMVar
fromSink <- newEmptyMVar
let pipe = LogPipe
{ readLogPipe = do
putMVar toSink True
takeMVar fromSink
, closeLogPipe = do
_ <- tryTakeMVar toSink
putMVar toSink False
}
sink = do
toCont <- lift $ M.takeMVar toSink
if toCont
then do
mbs <- await
lift $ M.putMVar fromSink mbs
maybe (return ()) (P.const sink) mbs
else return ()
return (pipe, sink)
2012-05-17 10:32:11 +04:00
newtype Logger = Logger (Command -> KIO ())
2012-10-04 21:16:45 +04:00
data Command = Attach LogPipes | Detach
2012-05-17 10:32:11 +04:00
start :: LogFile.LogFile -- ^ stdout
-> LogFile.LogFile -- ^ stderr
-> KIO Logger
start lfout lferr = do
chan <- newChan
forkKIO $ loop chan Nothing Nothing
return $ Logger $ writeChan chan
where
killOld tid = do
res <- liftIO $ killThread tid
case res of
Left e -> $logEx e
Right () -> return ()
loop chan moldout molderr = do
c <- readChan chan
maybe (return ()) killOld moldout
maybe (return ()) killOld molderr
case c of
Detach -> do
LogFile.close lfout
LogFile.close lferr
2012-10-04 21:16:45 +04:00
Attach (LogPipes out err) -> do
2012-05-22 22:49:11 +04:00
LogFile.addChunk lfout "\n\nAttaching new process\n\n"
LogFile.addChunk lferr "\n\nAttaching new process\n\n"
2012-10-04 21:16:45 +04:00
let go logpipe lf = do
etid <- forkKIO' $ listener logpipe lf
case etid of
Left e -> do
$logEx e
closeLogPipe logpipe
return Nothing
Right tid -> return $ Just tid
newout <- go out lfout
newerr <- go err lferr
2012-05-17 10:32:11 +04:00
loop chan newout newerr
2012-10-04 21:16:45 +04:00
listener :: LogPipe -> LogFile.LogFile -> KIO ()
2012-05-17 10:32:11 +04:00
listener out lf =
loop
where
loop = do
2012-10-04 21:16:45 +04:00
mbs <- readLogPipe out
case mbs of
Nothing -> return ()
Just bs -> do
LogFile.addChunk lf bs
loop
2012-05-17 10:32:11 +04:00
2012-10-04 21:16:45 +04:00
attach :: Logger -> LogPipes -> KIO ()
2012-05-17 10:32:11 +04:00
attach (Logger f) h = f (Attach h)
detach :: Logger -> KIO ()
detach (Logger f) = f Detach
dummy :: Logger
2012-10-04 21:16:45 +04:00
dummy = Logger $ P.const $ return ()