From c91dbdc0f5be0f1ec6a0cafc441eef0aa6da58d7 Mon Sep 17 00:00:00 2001 From: AndrewRademacher Date: Wed, 24 Feb 2016 21:02:15 -0600 Subject: [PATCH] Defined sendLog function. --- graylog.cabal | 2 ++ src/Graylog/Types.hs | 26 ++++++++++++++++++-------- src/Graylog/UDP.hs | 19 ++++++++++++++----- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/graylog.cabal b/graylog.cabal index cab25b9..3eaa812 100644 --- a/graylog.cabal +++ b/graylog.cabal @@ -27,6 +27,8 @@ library , aeson , aeson-casing + , bytestring + , mwc-random , network , scientific , text diff --git a/src/Graylog/Types.hs b/src/Graylog/Types.hs index e18826b..bfee38a 100644 --- a/src/Graylog/Types.hs +++ b/src/Graylog/Types.hs @@ -5,7 +5,11 @@ module Graylog.Types , _graylogHost , _graylogPort , _graylogAddress + , _graylogSocket + , _graylogHostName + , ChunkSize + , defaultChunkSize , openGraylog , closeGraylog ) where @@ -15,17 +19,23 @@ import qualified Data.Text as T import Network.BSD import Network.Socket +type ChunkSize = Word + data Graylog = Graylog - { _graylogHost :: String - , _graylogPort :: String - , _graylogAddress :: AddrInfo - , _graylogSocket :: Socket - , _graylogHostName :: Text + { _graylogHost :: String + , _graylogPort :: String + , _graylogAddress :: AddrInfo + , _graylogSocket :: Socket + , _graylogHostName :: Text + , _graylogChunkSize :: ChunkSize } -openGraylog :: HostName -> ServiceName -> IO (Either String Graylog) -openGraylog host port = do +defaultChunkSize :: ChunkSize +defaultChunkSize = 8192 + +openGraylog :: HostName -> ServiceName -> ChunkSize -> IO (Either String Graylog) +openGraylog host port chksize = do infos <- getAddrInfo Nothing (Just host) (Just port) case infos of [] -> return $ Left "No address info found." @@ -33,7 +43,7 @@ openGraylog host port = do sock <- socket (addrFamily info) Datagram defaultProtocol connect sock (addrAddress info) hostname <- getHostName - return $ Right $ Graylog host port info sock (T.pack hostname) + return $ Right $ Graylog host port info sock (T.pack hostname) chksize _ -> return $ Left "Too many address infos found." closeGraylog :: Graylog -> IO () diff --git a/src/Graylog/UDP.hs b/src/Graylog/UDP.hs index 00b6a12..dc172b9 100644 --- a/src/Graylog/UDP.hs +++ b/src/Graylog/UDP.hs @@ -1,13 +1,22 @@ module Graylog.UDP - ( Graylog (..) - , sendLog + ( sendLog , module Export ) where -import Graylog.Gelf as Export -import Graylog.Types as Export +import Data.Aeson +import qualified Data.ByteString.Lazy as LBS +import Network.Socket.ByteString.Lazy +import System.Random.MWC + +import Graylog.Gelf as Export +import Graylog.Types as Export sendLog :: Graylog -> GELF -> IO () -sendLog glog msg = undefined +sendLog glog msg = mapM_ (send $ _graylogSocket glog) cks + where + raw = encode msg + cks = chunky raw +chunky :: LBS.ByteString -> [LBS.ByteString] +chunky = undefined -- 2.41.0