1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ <= 708
{-# LANGUAGE Trustworthy #-}
#else
{-# LANGUAGE Safe #-}
#endif
module System.Log.FastLogger.IO where
import Data.ByteString.Builder.Extra (Next(..))
import qualified Data.ByteString.Builder.Extra as BBE
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Alloc (mallocBytes, free)
import Foreign.Ptr (Ptr, plusPtr)
import System.Log.FastLogger.Imports
import System.Log.FastLogger.LogStr
type Buffer = Ptr Word8
-- | The type for buffer size of each core.
type BufSize = Int
-- | The default buffer size (4,096 bytes).
defaultBufSize :: BufSize
defaultBufSize = 4096
getBuffer :: BufSize -> IO Buffer
getBuffer = mallocBytes
freeBuffer :: Buffer -> IO ()
freeBuffer = free
toBufIOWith :: Buffer -> BufSize -> (Buffer -> Int -> IO ()) -> Builder -> IO ()
toBufIOWith buf size io builder = loop $ BBE.runBuilder builder
where
loop writer = do
(len, next) <- writer buf size
io buf len
case next of
Done -> return ()
More minSize writer'
| size < minSize -> error "toBufIOWith: More: minSize"
| otherwise -> loop writer'
Chunk (PS fptr off siz) writer' ->
withForeignPtr fptr $ \ptr -> io (ptr `plusPtr` off) siz >> loop writer'
|