diff options
Diffstat (limited to 'src/Graylog/UDP.hs')
-rw-r--r-- | src/Graylog/UDP.hs | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/Graylog/UDP.hs b/src/Graylog/UDP.hs index c925d7e..cd7fe5a 100644 --- a/src/Graylog/UDP.hs +++ b/src/Graylog/UDP.hs | |||
@@ -5,37 +5,45 @@ module Graylog.UDP | |||
5 | ) where | 5 | ) where |
6 | 6 | ||
7 | import Data.Aeson | 7 | import Data.Aeson |
8 | {-import Data.ByteString.Builder-} | 8 | import Data.ByteString.Builder |
9 | {-import qualified Data.ByteString.Lazy as LBS-} | 9 | import qualified Data.ByteString.Lazy as LBS |
10 | {-import Data.Word-} | 10 | import Data.Monoid |
11 | import Data.Word | ||
11 | import Network.Socket.ByteString.Lazy | 12 | import Network.Socket.ByteString.Lazy |
12 | {-import System.Random-} | 13 | import System.Random |
13 | 14 | ||
14 | import Graylog.Gelf as Export | 15 | import Graylog.Gelf as Export |
15 | import Graylog.Types as Export | 16 | import Graylog.Types as Export |
16 | 17 | ||
17 | sendLog :: Graylog -> GELF -> IO () | 18 | sendLog :: Graylog -> GELF -> IO () |
18 | sendLog glog msg = do | 19 | sendLog glog msg = do |
19 | _ <- send (_graylogSocket glog) raw | 20 | cks <- chunky glog raw |
20 | print raw | 21 | mapM_ (send $ _graylogSocket glog) cks |
21 | return () | ||
22 | where | 22 | where |
23 | raw = encode msg | 23 | raw = encode msg |
24 | 24 | ||
25 | {-sendLog :: Graylog -> GELF -> IO ()-} | 25 | chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString] |
26 | {-sendLog glog msg = do-} | 26 | chunky glog raw = do |
27 | {-cks <- chunky glog raw-} | 27 | groupId <- randomIO |
28 | {-mapM_ (send $ _graylogSocket glog) cks-} | 28 | let groups = divide totalNum raw |
29 | {-where-} | 29 | return $ append groupId groups seqNums |
30 | {-raw = encode msg-} | 30 | where |
31 | magic = word8 0x1e <> word8 0x0f | ||
32 | seqNums = [0..] :: [Word8] | ||
33 | totalNum = if excess > 0 then count + 1 else count | ||
34 | (count, excess) = quotRem (LBS.length raw) gsize | ||
35 | hlen = 12 | ||
36 | gsize = (fromIntegral (_graylogChunkSize glog)) - hlen | ||
37 | |||
38 | divide 0 dat = [dat] | ||
39 | divide num dat = let (pre, post) = LBS.splitAt gsize dat | ||
40 | in pre : divide (num - 1) post | ||
31 | 41 | ||
32 | {-chunky :: Graylog -> LBS.ByteString -> IO [LBS.ByteString]-} | 42 | append _ [] _ = [] |
33 | {-chunky glog raw = do-} | 43 | append _ _ [] = error "the impossible has happened." |
34 | {-groupId <- randomIO-} | 44 | append gid (g:gs) (s:ss) = (toLazyByteString |
35 | {-splitAt gsize-} | 45 | $ magic |
36 | {-where-} | 46 | <> word64BE gid |
37 | {-magic = word8 0x1e <> word8 0x0f-} | 47 | <> word8 s |
38 | {-seqNum = undefined-} | 48 | <> word8 (fromIntegral totalNum) |
39 | {-(count, excess) = quotRem (LBS.length raw) gzie-} | 49 | <> lazyByteString g) : append gid gs ss |
40 | {-hlen = 12-} | ||
41 | {-gsize = (fromIntegral (_graylogChunkSize glog)) - hlen-} | ||