From 2ff46fce748b0e813f13f03c034065092145a28d Mon Sep 17 00:00:00 2001 From: AndrewRademacher Date: Wed, 24 Feb 2016 20:39:47 -0600 Subject: Defined Graylog type to contain connection informaiton. --- graylog.cabal | 3 +++ src/Graylog/Types.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/Graylog/UDP.hs | 14 ++++---------- 3 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 src/Graylog/Types.hs diff --git a/graylog.cabal b/graylog.cabal index b3c1a53..cab25b9 100644 --- a/graylog.cabal +++ b/graylog.cabal @@ -17,6 +17,9 @@ library default-language: Haskell2010 exposed-modules: Graylog.Gelf + , Graylog.UDP + + other-modules: Graylog.Types ghc-options: -Wall -rtsopts diff --git a/src/Graylog/Types.hs b/src/Graylog/Types.hs new file mode 100644 index 0000000..e18826b --- /dev/null +++ b/src/Graylog/Types.hs @@ -0,0 +1,42 @@ +{-# LANGUAGE RecordWildCards #-} + +module Graylog.Types + ( Graylog + , _graylogHost + , _graylogPort + , _graylogAddress + + , openGraylog + , closeGraylog + ) where + +import Data.Text (Text) +import qualified Data.Text as T +import Network.BSD +import Network.Socket + +data Graylog + = Graylog + { _graylogHost :: String + , _graylogPort :: String + , _graylogAddress :: AddrInfo + , _graylogSocket :: Socket + , _graylogHostName :: Text + } + +openGraylog :: HostName -> ServiceName -> IO (Either String Graylog) +openGraylog host port = do + infos <- getAddrInfo Nothing (Just host) (Just port) + case infos of + [] -> return $ Left "No address info found." + [info] -> do + sock <- socket (addrFamily info) Datagram defaultProtocol + connect sock (addrAddress info) + hostname <- getHostName + return $ Right $ Graylog host port info sock (T.pack hostname) + _ -> return $ Left "Too many address infos found." + +closeGraylog :: Graylog -> IO () +closeGraylog Graylog{..} = close _graylogSocket + + diff --git a/src/Graylog/UDP.hs b/src/Graylog/UDP.hs index 6aec108..00b6a12 100644 --- a/src/Graylog/UDP.hs +++ b/src/Graylog/UDP.hs @@ -2,18 +2,12 @@ module Graylog.UDP ( Graylog (..) , sendLog - , module Graylog.Gelf + , module Export ) where -import Network.Socket - -import Graylog.Gelf - -data Graylog - = Graylog - { _graylogHost :: String - , _graylogPort :: String - } +import Graylog.Gelf as Export +import Graylog.Types as Export sendLog :: Graylog -> GELF -> IO () sendLog glog msg = undefined + -- cgit v1.2.3