{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module System.Log.FastLogger.LoggerSet (
LoggerSet
, newFileLoggerSet
, newFileLoggerSetN
, newStdoutLoggerSet
, newStdoutLoggerSetN
, newStderrLoggerSet
, newStderrLoggerSetN
, newLoggerSet
, newFDLoggerSet
, renewLoggerSet
, rmLoggerSet
, pushLogStr
, pushLogStrLn
, flushLogStr
, replaceLoggerSet
) where
import Control.Concurrent (getNumCapabilities)
import Control.Debounce (mkDebounce, defaultDebounceSettings, debounceAction)
import System.Log.FastLogger.FileIO
import System.Log.FastLogger.IO
import System.Log.FastLogger.Imports
import System.Log.FastLogger.LogStr
import System.Log.FastLogger.MultiLogger (MultiLogger)
import qualified System.Log.FastLogger.MultiLogger as M
import System.Log.FastLogger.SingleLogger (SingleLogger)
import qualified System.Log.FastLogger.SingleLogger as S
import System.Log.FastLogger.Write
data Logger = SL SingleLogger | ML MultiLogger
data LoggerSet = LoggerSet {
LoggerSet -> Maybe FilePath
lgrsetFilePath :: Maybe FilePath
, LoggerSet -> IORef FD
lgrsetFdRef :: IORef FD
, LoggerSet -> Logger
lgrsetLogger :: Logger
, LoggerSet -> IO ()
lgrsetDebounce :: IO ()
}
newFileLoggerSet :: BufSize -> FilePath -> IO LoggerSet
newFileLoggerSet :: Int -> FilePath -> IO LoggerSet
newFileLoggerSet Int
size FilePath
file = FilePath -> IO FD
openFileFD FilePath
file forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Maybe Int -> Maybe FilePath -> FD -> IO LoggerSet
newFDLoggerSet Int
size forall a. Maybe a
Nothing (forall a. a -> Maybe a
Just FilePath
file)
newFileLoggerSetN :: BufSize -> Maybe Int -> FilePath -> IO LoggerSet
newFileLoggerSetN :: Int -> Maybe Int -> FilePath -> IO LoggerSet
newFileLoggerSetN Int
size Maybe Int
mn FilePath
file = FilePath -> IO FD
openFileFD FilePath
file forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Maybe Int -> Maybe FilePath -> FD -> IO LoggerSet
newFDLoggerSet Int
size Maybe Int
mn (forall a. a -> Maybe a
Just FilePath
file)
newStdoutLoggerSet :: BufSize -> IO LoggerSet
newStdoutLoggerSet :: Int -> IO LoggerSet
newStdoutLoggerSet Int
size = IO FD
getStdoutFD forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Maybe Int -> Maybe FilePath -> FD -> IO LoggerSet
newFDLoggerSet Int
size forall a. Maybe a
Nothing forall a. Maybe a
Nothing
newStdoutLoggerSetN :: BufSize -> Maybe Int -> IO LoggerSet
newStdoutLoggerSetN :: Int -> Maybe Int -> IO LoggerSet
newStdoutLoggerSetN Int
size Maybe Int
mn = IO FD
getStdoutFD forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Maybe Int -> Maybe FilePath -> FD -> IO LoggerSet
newFDLoggerSet Int
size Maybe Int
mn forall a. Maybe a
Nothing
newStderrLoggerSet :: BufSize -> IO LoggerSet
newStderrLoggerSet :: Int -> IO LoggerSet
newStderrLoggerSet Int
size = IO FD
getStderrFD forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Maybe Int -> Maybe FilePath -> FD -> IO LoggerSet
newFDLoggerSet Int
size forall a. Maybe a
Nothing forall a. Maybe a
Nothing
newStderrLoggerSetN :: BufSize -> Maybe Int -> IO LoggerSet
newStderrLoggerSetN :: Int -> Maybe Int -> IO LoggerSet
newStderrLoggerSetN Int
size Maybe Int
mn = IO FD
getStderrFD forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Maybe Int -> Maybe FilePath -> FD -> IO LoggerSet
newFDLoggerSet Int
size Maybe Int
mn forall a. Maybe a
Nothing
{-# DEPRECATED newLoggerSet "Use newFileLoggerSet etc instead" #-}
newLoggerSet :: BufSize -> Maybe Int -> Maybe FilePath -> IO LoggerSet
newLoggerSet :: Int -> Maybe Int -> Maybe FilePath -> IO LoggerSet
newLoggerSet Int
size Maybe Int
mn = forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Int -> IO LoggerSet
newStdoutLoggerSet Int
size) (Int -> Maybe Int -> FilePath -> IO LoggerSet
newFileLoggerSetN Int
size Maybe Int
mn)
newFDLoggerSet :: BufSize -> Maybe Int -> Maybe FilePath -> FD -> IO LoggerSet
newFDLoggerSet :: Int -> Maybe Int -> Maybe FilePath -> FD -> IO LoggerSet
newFDLoggerSet Int
size Maybe Int
mn Maybe FilePath
mfile FD
fd = do
Int
n <- case Maybe Int
mn of
Just Int
n' -> forall (m :: * -> *) a. Monad m => a -> m a
return Int
n'
Maybe Int
Nothing -> IO Int
getNumCapabilities
IORef FD
fdref <- forall a. a -> IO (IORef a)
newIORef FD
fd
let bufsiz :: Int
bufsiz = forall a. Ord a => a -> a -> a
max Int
1 Int
size
Logger
logger <- if Int
n forall a. Eq a => a -> a -> Bool
== Int
1 Bool -> Bool -> Bool
&& Maybe Int
mn forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just Int
1 then
SingleLogger -> Logger
SL forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> IORef FD -> IO SingleLogger
S.newSingleLogger Int
bufsiz IORef FD
fdref
else do
MultiLogger -> Logger
ML forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Int -> IORef FD -> IO MultiLogger
M.newMultiLogger Int
n Int
bufsiz IORef FD
fdref
IO ()
flush <- DebounceSettings -> IO (IO ())
mkDebounce DebounceSettings
defaultDebounceSettings
{ debounceAction :: IO ()
debounceAction = Logger -> IO ()
flushLogStrRaw Logger
logger
}
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ LoggerSet {
lgrsetFilePath :: Maybe FilePath
lgrsetFilePath = Maybe FilePath
mfile
, lgrsetFdRef :: IORef FD
lgrsetFdRef = IORef FD
fdref
, lgrsetLogger :: Logger
lgrsetLogger = Logger
logger
, lgrsetDebounce :: IO ()
lgrsetDebounce = IO ()
flush
}
pushLogStr :: LoggerSet -> LogStr -> IO ()
pushLogStr :: LoggerSet -> LogStr -> IO ()
pushLogStr LoggerSet{Maybe FilePath
IO ()
IORef FD
Logger
lgrsetDebounce :: IO ()
lgrsetLogger :: Logger
lgrsetFdRef :: IORef FD
lgrsetFilePath :: Maybe FilePath
lgrsetDebounce :: LoggerSet -> IO ()
lgrsetLogger :: LoggerSet -> Logger
lgrsetFdRef :: LoggerSet -> IORef FD
lgrsetFilePath :: LoggerSet -> Maybe FilePath
..} LogStr
logmsg = case Logger
lgrsetLogger of
SL SingleLogger
sl -> do
forall a. Loggers a => a -> LogStr -> IO ()
pushLog SingleLogger
sl LogStr
logmsg
IO ()
lgrsetDebounce
ML MultiLogger
ml -> do
forall a. Loggers a => a -> LogStr -> IO ()
pushLog MultiLogger
ml LogStr
logmsg
IO ()
lgrsetDebounce
pushLogStrLn :: LoggerSet -> LogStr -> IO ()
pushLogStrLn :: LoggerSet -> LogStr -> IO ()
pushLogStrLn LoggerSet
loggerSet LogStr
logStr = LoggerSet -> LogStr -> IO ()
pushLogStr LoggerSet
loggerSet (LogStr
logStr forall a. Semigroup a => a -> a -> a
<> LogStr
"\n")
flushLogStr :: LoggerSet -> IO ()
flushLogStr :: LoggerSet -> IO ()
flushLogStr LoggerSet{Maybe FilePath
IO ()
IORef FD
Logger
lgrsetDebounce :: IO ()
lgrsetLogger :: Logger
lgrsetFdRef :: IORef FD
lgrsetFilePath :: Maybe FilePath
lgrsetDebounce :: LoggerSet -> IO ()
lgrsetLogger :: LoggerSet -> Logger
lgrsetFdRef :: LoggerSet -> IORef FD
lgrsetFilePath :: LoggerSet -> Maybe FilePath
..} = Logger -> IO ()
flushLogStrRaw Logger
lgrsetLogger
flushLogStrRaw :: Logger -> IO ()
flushLogStrRaw :: Logger -> IO ()
flushLogStrRaw (SL SingleLogger
sl) = forall a. Loggers a => a -> IO ()
flushAllLog SingleLogger
sl
flushLogStrRaw (ML MultiLogger
ml) = forall a. Loggers a => a -> IO ()
flushAllLog MultiLogger
ml
renewLoggerSet :: LoggerSet -> IO ()
renewLoggerSet :: LoggerSet -> IO ()
renewLoggerSet LoggerSet{Maybe FilePath
IO ()
IORef FD
Logger
lgrsetDebounce :: IO ()
lgrsetLogger :: Logger
lgrsetFdRef :: IORef FD
lgrsetFilePath :: Maybe FilePath
lgrsetDebounce :: LoggerSet -> IO ()
lgrsetLogger :: LoggerSet -> Logger
lgrsetFdRef :: LoggerSet -> IORef FD
lgrsetFilePath :: LoggerSet -> Maybe FilePath
..} = case Maybe FilePath
lgrsetFilePath of
Maybe FilePath
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just FilePath
file -> do
FD
newfd <- FilePath -> IO FD
openFileFD FilePath
file
FD
oldfd <- forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef FD
lgrsetFdRef (\FD
fd -> (FD
newfd, FD
fd))
FD -> IO ()
closeFD FD
oldfd
rmLoggerSet :: LoggerSet -> IO ()
rmLoggerSet :: LoggerSet -> IO ()
rmLoggerSet LoggerSet{Maybe FilePath
IO ()
IORef FD
Logger
lgrsetDebounce :: IO ()
lgrsetLogger :: Logger
lgrsetFdRef :: IORef FD
lgrsetFilePath :: Maybe FilePath
lgrsetDebounce :: LoggerSet -> IO ()
lgrsetLogger :: LoggerSet -> Logger
lgrsetFdRef :: LoggerSet -> IORef FD
lgrsetFilePath :: LoggerSet -> Maybe FilePath
..} = do
FD
fd <- forall a. IORef a -> IO a
readIORef IORef FD
lgrsetFdRef
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (FD -> Bool
isFDValid FD
fd) forall a b. (a -> b) -> a -> b
$ do
case Logger
lgrsetLogger of
SL SingleLogger
sl -> forall a. Loggers a => a -> IO ()
stopLoggers SingleLogger
sl
ML MultiLogger
ml -> forall a. Loggers a => a -> IO ()
stopLoggers MultiLogger
ml
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (forall a. Maybe a -> Bool
isJust Maybe FilePath
lgrsetFilePath) forall a b. (a -> b) -> a -> b
$ FD -> IO ()
closeFD FD
fd
forall a. IORef a -> a -> IO ()
writeIORef IORef FD
lgrsetFdRef FD
invalidFD
replaceLoggerSet :: LoggerSet -> FilePath -> (LoggerSet, Maybe FilePath)
replaceLoggerSet :: LoggerSet -> FilePath -> (LoggerSet, Maybe FilePath)
replaceLoggerSet lgrset :: LoggerSet
lgrset@LoggerSet{Maybe FilePath
IO ()
IORef FD
Logger
lgrsetDebounce :: IO ()
lgrsetLogger :: Logger
lgrsetFdRef :: IORef FD
lgrsetFilePath :: Maybe FilePath
lgrsetDebounce :: LoggerSet -> IO ()
lgrsetLogger :: LoggerSet -> Logger
lgrsetFdRef :: LoggerSet -> IORef FD
lgrsetFilePath :: LoggerSet -> Maybe FilePath
..} FilePath
new_file_path =
(LoggerSet
lgrset { lgrsetFilePath :: Maybe FilePath
lgrsetFilePath = forall a. a -> Maybe a
Just FilePath
new_file_path }, Maybe FilePath
lgrsetFilePath)