module Control.Eff.LogWriter.File
( withFileLogging
, withFileLogWriter
)
where
import Control.Eff as Eff
import Control.Eff.Log
import Control.Eff.LogWriter.Rich
import GHC.Stack
import Data.Text as T
import qualified System.IO as IO
import System.Directory ( canonicalizePath
, createDirectoryIfMissing
)
import System.FilePath ( takeDirectory )
import qualified Control.Exception.Safe as Safe
import qualified Control.Monad.Catch as Catch
import Control.Monad.Trans.Control ( MonadBaseControl
, liftBaseOp
)
withFileLogging
:: (Lifted IO e, MonadBaseControl IO (Eff e), HasCallStack)
=> FilePath
-> Text
-> Facility
-> LogPredicate
-> LogMessageTextRenderer
-> Eff (Logs : LogWriterReader : e) a
-> Eff e a
withFileLogging fnIn a f p render e = do
liftBaseOp (withOpenedLogFile fnIn render) (\lw -> withRichLogging lw a f p e)
withFileLogWriter
:: (IoLogging e, MonadBaseControl IO (Eff e), HasCallStack)
=> FilePath
-> LogMessageTextRenderer
-> Eff e b
-> Eff e b
withFileLogWriter fnIn render e =
liftBaseOp (withOpenedLogFile fnIn render) (`addLogWriter` e)
withOpenedLogFile
:: HasCallStack
=> FilePath
-> LogMessageTextRenderer
-> (LogWriter -> IO a)
-> IO a
withOpenedLogFile fnIn render ioE = Safe.bracket
(do
fnCanon <- canonicalizePath fnIn
createDirectoryIfMissing True (takeDirectory fnCanon)
h <- IO.openFile fnCanon IO.AppendMode
IO.hSetBuffering h IO.LineBuffering
return h
)
(\h -> Safe.try @IO @Catch.SomeException (IO.hFlush h) >> IO.hClose h)
(\h -> ioE (ioHandleLogWriter h render))