{-# LANGUAGE RecordWildCards #-}

module System.Log.FastLogger.SingleLogger (
    SingleLogger,
    newSingleLogger,
) where

import Control.Concurrent (MVar, forkIO, newEmptyMVar, putMVar, takeMVar)
import Control.Concurrent.STM

import System.Log.FastLogger.FileIO
import System.Log.FastLogger.IO
import System.Log.FastLogger.Imports
import System.Log.FastLogger.LogStr
import System.Log.FastLogger.Write

----------------------------------------------------------------

data Ent = F (MVar ()) Bool | L LogStr
type Q = [Ent] -- writer queue

-- | A non-scale but time-ordered logger.
data SingleLogger = SingleLogger
    { SingleLogger -> IORef (LogStr, Q)
slgrRef :: IORef (LogStr, Q)
    , SingleLogger -> Bool -> IO ()
slgrFlush :: Bool -> IO () -- teminate if False
    , SingleLogger -> IO ()
slgrWakeup :: IO ()
    , SingleLogger -> Buffer
slgrBuffer :: Buffer
    , SingleLogger -> Int
slgrBufSize :: BufSize
    , SingleLogger -> IORef FD
slgrFdRef :: IORef FD
    }

instance Loggers SingleLogger where
    stopLoggers :: SingleLogger -> IO ()
stopLoggers = SingleLogger -> IO ()
System.Log.FastLogger.SingleLogger.stopLoggers
    pushLog :: SingleLogger -> LogStr -> IO ()
pushLog = SingleLogger -> LogStr -> IO ()
System.Log.FastLogger.SingleLogger.pushLog
    flushAllLog :: SingleLogger -> IO ()
flushAllLog = SingleLogger -> IO ()
System.Log.FastLogger.SingleLogger.flushAllLog

----------------------------------------------------------------

writer
    :: BufSize
    -> Buffer
    -> IORef FD
    -> TVar Int
    -> IORef (LogStr, Q)
    -> IO ()
writer :: Int -> Buffer -> IORef FD -> TVar Int -> IORef (LogStr, Q) -> IO ()
writer Int
bufsize Buffer
buf IORef FD
fdref TVar Int
tvar IORef (LogStr, Q)
ref = Int -> IO ()
loop (Int
0 :: Int)
  where
    loop :: Int -> IO ()
loop Int
cnt = do
        Int
cnt' <- STM Int -> IO Int
forall a. STM a -> IO a
atomically (STM Int -> IO Int) -> STM Int -> IO Int
forall a b. (a -> b) -> a -> b
$ do
            Int
n <- TVar Int -> STM Int
forall a. TVar a -> STM a
readTVar TVar Int
tvar
            Bool -> STM ()
check (Int
n Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
cnt)
            Int -> STM Int
forall a. a -> STM a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
n
        Q
msgs <- Q -> Q
forall a. [a] -> [a]
reverse (Q -> Q) -> IO Q -> IO Q
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IORef (LogStr, Q) -> ((LogStr, Q) -> ((LogStr, Q), Q)) -> IO Q
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef (LogStr, Q)
ref (\(LogStr
msg, Q
q) -> ((LogStr
msg, []), Q
q))
        Bool
cont <- Q -> IO Bool
go Q
msgs
        Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
cont (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> IO ()
loop Int
cnt'
    go :: Q -> IO Bool
go [] = Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
    go (F MVar ()
mvar Bool
cont : Q
msgs) = do
        MVar () -> () -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar ()
mvar ()
        if Bool
cont then Q -> IO Bool
go Q
msgs else Bool -> IO Bool
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
    go (L msg :: LogStr
msg@(LogStr Int
len Builder
_) : Q
msgs)
        | Int
len Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
bufsize = Buffer -> IORef FD -> LogStr -> IO ()
writeLogStr Buffer
buf IORef FD
fdref LogStr
msg IO () -> IO Bool -> IO Bool
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Q -> IO Bool
go Q
msgs
        | Bool
otherwise = IORef FD -> LogStr -> IO ()
writeBigLogStr IORef FD
fdref LogStr
msg IO () -> IO Bool -> IO Bool
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Q -> IO Bool
go Q
msgs

----------------------------------------------------------------

-- | Creating `SingleLogger`.
newSingleLogger :: BufSize -> IORef FD -> IO SingleLogger
newSingleLogger :: Int -> IORef FD -> IO SingleLogger
newSingleLogger Int
bufsize IORef FD
fdref = do
    TVar Int
tvar <- Int -> IO (TVar Int)
forall a. a -> IO (TVar a)
newTVarIO Int
0
    IORef (LogStr, Q)
ref <- (LogStr, Q) -> IO (IORef (LogStr, Q))
forall a. a -> IO (IORef a)
newIORef (LogStr
forall a. Monoid a => a
mempty, [])
    Buffer
buf <- Int -> IO Buffer
getBuffer Int
bufsize
    ThreadId
_ <- IO () -> IO ThreadId
forkIO (IO () -> IO ThreadId) -> IO () -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ Int -> Buffer -> IORef FD -> TVar Int -> IORef (LogStr, Q) -> IO ()
writer Int
bufsize Buffer
buf IORef FD
fdref TVar Int
tvar IORef (LogStr, Q)
ref
    let wakeup :: IO ()
wakeup = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar Int -> (Int -> Int) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar' TVar Int
tvar (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
        flush :: Bool -> IO ()
flush Bool
cont = do
            MVar ()
mvar <- IO (MVar ())
forall a. IO (MVar a)
newEmptyMVar
            let fin :: Ent
fin = MVar () -> Bool -> Ent
F MVar ()
mvar Bool
cont
            IORef (LogStr, Q) -> ((LogStr, Q) -> ((LogStr, Q), ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef (LogStr, Q)
ref (\(LogStr
old, Q
q) -> ((LogStr
forall a. Monoid a => a
mempty, Ent
fin Ent -> Q -> Q
forall a. a -> [a] -> [a]
: LogStr -> Ent
L LogStr
old Ent -> Q -> Q
forall a. a -> [a] -> [a]
: Q
q), ()))
            IO ()
wakeup
            MVar () -> IO ()
forall a. MVar a -> IO a
takeMVar MVar ()
mvar
    SingleLogger -> IO SingleLogger
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (SingleLogger -> IO SingleLogger)
-> SingleLogger -> IO SingleLogger
forall a b. (a -> b) -> a -> b
$
        SingleLogger
            { slgrRef :: IORef (LogStr, Q)
slgrRef = IORef (LogStr, Q)
ref
            , slgrFlush :: Bool -> IO ()
slgrFlush = Bool -> IO ()
flush
            , slgrWakeup :: IO ()
slgrWakeup = IO ()
wakeup
            , slgrBuffer :: Buffer
slgrBuffer = Buffer
buf
            , slgrBufSize :: Int
slgrBufSize = Int
bufsize
            , slgrFdRef :: IORef FD
slgrFdRef = IORef FD
fdref
            }

----------------------------------------------------------------

pushLog :: SingleLogger -> LogStr -> IO ()
pushLog :: SingleLogger -> LogStr -> IO ()
pushLog SingleLogger{Int
IO ()
Buffer
IORef (LogStr, Q)
IORef FD
Bool -> IO ()
slgrRef :: SingleLogger -> IORef (LogStr, Q)
slgrFlush :: SingleLogger -> Bool -> IO ()
slgrWakeup :: SingleLogger -> IO ()
slgrBuffer :: SingleLogger -> Buffer
slgrBufSize :: SingleLogger -> Int
slgrFdRef :: SingleLogger -> IORef FD
slgrRef :: IORef (LogStr, Q)
slgrFlush :: Bool -> IO ()
slgrWakeup :: IO ()
slgrBuffer :: Buffer
slgrBufSize :: Int
slgrFdRef :: IORef FD
..} nlogmsg :: LogStr
nlogmsg@(LogStr Int
nlen Builder
_)
    | Int
nlen Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
slgrBufSize = do
        IORef (LogStr, Q) -> ((LogStr, Q) -> ((LogStr, Q), ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef (LogStr, Q)
slgrRef (\(LogStr
old, Q
q) -> ((LogStr
forall a. Monoid a => a
mempty, LogStr -> Ent
L LogStr
nlogmsg Ent -> Q -> Q
forall a. a -> [a] -> [a]
: LogStr -> Ent
L LogStr
old Ent -> Q -> Q
forall a. a -> [a] -> [a]
: Q
q), ()))
        IO ()
slgrWakeup
    | Bool
otherwise = do
        Bool
wake <- IORef (LogStr, Q)
-> ((LogStr, Q) -> ((LogStr, Q), Bool)) -> IO Bool
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef (LogStr, Q)
slgrRef (LogStr, Q) -> ((LogStr, Q), Bool)
checkBuf
        Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
wake IO ()
slgrWakeup
  where
    checkBuf :: (LogStr, Q) -> ((LogStr, Q), Bool)
checkBuf (ologmsg :: LogStr
ologmsg@(LogStr Int
olen Builder
_), Q
q)
        | Int
slgrBufSize Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
olen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
nlen = ((LogStr
nlogmsg, LogStr -> Ent
L LogStr
ologmsg Ent -> Q -> Q
forall a. a -> [a] -> [a]
: Q
q), Bool
True)
        | Bool
otherwise = ((LogStr
ologmsg LogStr -> LogStr -> LogStr
forall a. Semigroup a => a -> a -> a
<> LogStr
nlogmsg, Q
q), Bool
False)

flushAllLog :: SingleLogger -> IO ()
flushAllLog :: SingleLogger -> IO ()
flushAllLog SingleLogger{Int
IO ()
Buffer
IORef (LogStr, Q)
IORef FD
Bool -> IO ()
slgrRef :: SingleLogger -> IORef (LogStr, Q)
slgrFlush :: SingleLogger -> Bool -> IO ()
slgrWakeup :: SingleLogger -> IO ()
slgrBuffer :: SingleLogger -> Buffer
slgrBufSize :: SingleLogger -> Int
slgrFdRef :: SingleLogger -> IORef FD
slgrRef :: IORef (LogStr, Q)
slgrFlush :: Bool -> IO ()
slgrWakeup :: IO ()
slgrBuffer :: Buffer
slgrBufSize :: Int
slgrFdRef :: IORef FD
..} = do
    IORef (LogStr, Q) -> ((LogStr, Q) -> ((LogStr, Q), ())) -> IO ()
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef (LogStr, Q)
slgrRef (\(LogStr
old, Q
q) -> ((LogStr
forall a. Monoid a => a
mempty, LogStr -> Ent
L LogStr
old Ent -> Q -> Q
forall a. a -> [a] -> [a]
: Q
q), ()))
    Bool -> IO ()
slgrFlush Bool
True

stopLoggers :: SingleLogger -> IO ()
stopLoggers :: SingleLogger -> IO ()
stopLoggers SingleLogger{Int
IO ()
Buffer
IORef (LogStr, Q)
IORef FD
Bool -> IO ()
slgrRef :: SingleLogger -> IORef (LogStr, Q)
slgrFlush :: SingleLogger -> Bool -> IO ()
slgrWakeup :: SingleLogger -> IO ()
slgrBuffer :: SingleLogger -> Buffer
slgrBufSize :: SingleLogger -> Int
slgrFdRef :: SingleLogger -> IORef FD
slgrRef :: IORef (LogStr, Q)
slgrFlush :: Bool -> IO ()
slgrWakeup :: IO ()
slgrBuffer :: Buffer
slgrBufSize :: Int
slgrFdRef :: IORef FD
..} = do
    Bool -> IO ()
slgrFlush Bool
False
    Buffer -> IO ()
freeBuffer Buffer
slgrBuffer