module Network.Gelf.Chunk (
split
) where
import Data.Bits (shiftR, (.&.))
import qualified Data.ByteString.Lazy as BSL
import Data.Word
split :: Int
-> Word64
-> BSL.ByteString
-> [BSL.ByteString]
split size id bs =
let cs = split' bs
in zipWith (chunkify (length cs)) cs [0..]
where split' :: BSL.ByteString -> [BSL.ByteString]
split' bs =
if BSL.length bs == 0
then []
else let (h,ts) = BSL.splitAt (fromIntegral size) bs
in h : split' ts
idBytes = BSL.pack [fromIntegral (shiftR id (x * 8) .&. 0xff) | x <- [7, 6 .. 0]]
chunkify :: Int
-> BSL.ByteString
-> Int
-> BSL.ByteString
chunkify total s i = BSL.cons 0x1e $ BSL.cons 0x0f $ BSL.append idBytes $ BSL.cons (fromIntegral i :: Word8) $ BSL.cons (fromIntegral total :: Word8) s