import System.IO
import System.Process
--- | Strings or ByteStrings with label, oder nothing at all
-printwithlabel :: String -> Maybe (IO ()) -> IO ()
-printwithlabel _ Nothing = return ()
-printwithlabel labl (Just i) = do
- mapM_ (hPutStr stderr) [" --- ", labl, ": "]
- i
- hFlush stderr
-
class Flexprint a where
- flexprint :: a -> Maybe (IO ())
-
-instance Flexprint (Maybe String) where
- flexprint = fmap (hPutStrLn stderr)
+ flexprint :: a -> IO ()
+ printparam :: String -> a -> IO ()
+ printparam labl x = do
+ mapM_ (hPutStr stderr) [" --- ", labl, ": "]
+ flexprint x
+ hFlush stderr
-instance Flexprint (Maybe Int) where
- flexprint x = flexprint (fmap show x)
+instance (Flexprint a) => Flexprint (Maybe a) where
+ flexprint Nothing = return ()
+ flexprint (Just x) = flexprint x
+ printparam _ Nothing = return ()
+ printparam x (Just y) = printparam x y
instance Flexprint String where
- flexprint x = flexprint (Just x)
+ flexprint = hPutStrLn stderr
instance Flexprint [String] where
flexprint = flexprint . unwords
instance Flexprint T.Text where
flexprint = flexprint . T.unpack
--- | optional or required parameters
-printparam :: Flexprint a => String -> a -> IO ()
-printparam labl x = printwithlabel labl $ flexprint x
+instance Flexprint BL.ByteString where
+ flexprint x = hPutStrLn stderr "" >> BL.hPut stderr x >> hPutStrLn stderr ""
--- | head chars of body
-printbody :: String -> Maybe BL.ByteString -> IO ()
-printbody labl ms = do
- printwithlabel labl $
- fmap
- (\s -> hPutStrLn stderr "" >> BL.hPut stderr s >> hPutStrLn stderr "")
- ms
- hFlush stderr
+instance Flexprint ExitCode where
+ flexprint = flexprint . show
-- | log marker
hr :: String -> IO ()
, ("expiration", mexp)
, ("delivery mode", mdelivmode)
]
- printbody label anriss
+ printparam label anriss
mapM_ (\hdl -> BL.hPut hdl body >> hFlush hdl) h
return $
concat
createProcess (proc exe args) {std_out = out, std_err = Inherit}
sout <- mapM BL.hGetContents h
exitcode <- maybe 0 id (fmap BL.length sout) `seq` waitForProcess processhandle
- printparam (numstring ++ " call returned") $ show exitcode
+ printparam (numstring ++ " call returned") exitcode
if isJust action && isJust sout
then ((fromJust action $ exitcode) (fromJust sout)) >> acke envi a
else case exitcode of
(\(x, _, _) -> return x))
(return)
(fmap T.pack (qName args))
- printparam "queue name" $ T.unpack queue
+ printparam "queue name" queue
if (currentExchange args /= "")
then do
printparam "exchange" $ currentExchange args
then Ack
else NoAck)
(rpcServerCallback tid args addiArgs chan)
- printparam "consumer tag" $ T.unpack ctag
- printparam "send acks" $ show (ack args)
+ printparam "consumer tag" ctag
+ printparam "send acks" (ack args)
printparam "requeue if rejected" $
if (ack args)
- then Just (show (requeuenack args))
+ then Just (requeuenack args)
else Nothing
hr "entering main loop"
X.catch
printparam "output file" $ outputFile args
h <- if outputFile args == "-" then return stdout else openBinaryFile (outputFile args) WriteMode
ctag <- consumeMsgs chan q NoAck (rpcClientCallback h tid args)
- printparam "consumer tag" $ T.unpack ctag
+ printparam "consumer tag" ctag
now <- getCurrentTime >>= return . floor . utcTimeToPOSIXSeconds
hr "publishing request"
_ <- publishMsg
ec <- exceptionHandler x
hr "closing connection"
closeConnection conn
- printparam "exiting" $ show ec
+ printparam "exiting" ec
exitWith ec)
exceptionHandler :: RpcException -> IO (ExitCode)