Blame Network/StreamDebugger.hs

Packit acf257
-----------------------------------------------------------------------------
Packit acf257
-- |
Packit acf257
-- Module      :  Network.StreamDebugger
Packit acf257
-- Copyright   :  See LICENSE file
Packit acf257
-- License     :  BSD
Packit acf257
--
Packit acf257
-- Maintainer  :  Ganesh Sittampalam <ganesh@earth.li>
Packit acf257
-- Stability   :  experimental
Packit acf257
-- Portability :  non-portable (not tested)
Packit acf257
--
Packit acf257
-- Implements debugging of @Stream@s.  Originally part of Gray's\/Bringert's
Packit acf257
-- HTTP module.
Packit acf257
--
Packit acf257
-- * Changes by Robin Bate Boerop <robin@bateboerop.name>:
Packit acf257
--      - Created.  Made minor formatting changes.
Packit acf257
--      
Packit acf257
-----------------------------------------------------------------------------
Packit acf257
module Network.StreamDebugger
Packit acf257
   ( StreamDebugger
Packit acf257
   , debugStream
Packit acf257
   , debugByteStream
Packit acf257
   ) where
Packit acf257
Packit acf257
import Network.Stream (Stream(..))
Packit acf257
import System.IO
Packit acf257
   ( Handle, hFlush, hPutStrLn, IOMode(AppendMode), hClose, openFile,
Packit acf257
     hSetBuffering, BufferMode(NoBuffering)
Packit acf257
   )
Packit acf257
import Network.TCP ( HandleStream, HStream, 
Packit acf257
                     StreamHooks(..), setStreamHooks, getStreamHooks )
Packit acf257
Packit acf257
-- | Allows stream logging.  Refer to 'debugStream' below.
Packit acf257
data StreamDebugger x
Packit acf257
   = Dbg Handle x
Packit acf257
Packit acf257
instance (Stream x) => Stream (StreamDebugger x) where
Packit acf257
    readBlock (Dbg h x) n =
Packit acf257
        do val <- readBlock x n
Packit acf257
           hPutStrLn h ("--readBlock " ++ show n)
Packit acf257
           hPutStrLn h (show val)
Packit acf257
           return val
Packit acf257
    readLine (Dbg h x) =
Packit acf257
        do val <- readLine x
Packit acf257
           hPutStrLn h ("--readLine")
Packit acf257
           hPutStrLn h (show val)
Packit acf257
           return val
Packit acf257
    writeBlock (Dbg h x) str =
Packit acf257
        do val <- writeBlock x str
Packit acf257
           hPutStrLn h ("--writeBlock" ++ show str)
Packit acf257
           hPutStrLn h (show val)
Packit acf257
           return val
Packit acf257
    close (Dbg h x) =
Packit acf257
        do hPutStrLn h "--closing..."
Packit acf257
           hFlush h
Packit acf257
           close x
Packit acf257
           hPutStrLn h "--closed."
Packit acf257
           hClose h
Packit acf257
    closeOnEnd (Dbg h x) f =
Packit acf257
        do hPutStrLn h ("--close-on-end.." ++ show f)
Packit acf257
           hFlush h 
Packit acf257
           closeOnEnd x f
Packit acf257
Packit acf257
-- | Wraps a stream with logging I\/O.
Packit acf257
--   The first argument is a filename which is opened in @AppendMode@.
Packit acf257
debugStream :: (Stream a) => FilePath -> a -> IO (StreamDebugger a)
Packit acf257
debugStream file stream = 
Packit acf257
    do h <- openFile file AppendMode
Packit acf257
       hPutStrLn h ("File \"" ++ file ++ "\" opened for appending.")
Packit acf257
       return (Dbg h stream)
Packit acf257
Packit acf257
debugByteStream :: HStream ty => FilePath -> HandleStream ty -> IO (HandleStream ty)
Packit acf257
debugByteStream file stream = do
Packit acf257
   sh <- getStreamHooks stream 
Packit acf257
   case sh of
Packit acf257
     Just h 
Packit acf257
      | hook_name h == file -> return stream -- reuse the stream hooks.
Packit acf257
     _ -> do
Packit acf257
       h <- openFile file AppendMode
Packit acf257
       hSetBuffering h NoBuffering
Packit acf257
       hPutStrLn h ("File \"" ++ file ++ "\" opened for appending.")
Packit acf257
       setStreamHooks stream (debugStreamHooks h file)
Packit acf257
       return stream
Packit acf257
Packit acf257
debugStreamHooks :: HStream ty => Handle -> String -> StreamHooks ty
Packit acf257
debugStreamHooks h nm = 
Packit acf257
  StreamHooks
Packit acf257
    { hook_readBlock = \ toStr n val -> do
Packit acf257
       let eval = case val of { Left e -> Left e ; Right v -> Right $ toStr v}
Packit acf257
       hPutStrLn h ("--readBlock " ++ show n)
Packit acf257
       hPutStrLn h (either show show eval)
Packit acf257
    , hook_readLine = \ toStr val -> do
Packit acf257
           let eval = case val of { Left e -> Left e ; Right v -> Right $ toStr v}
Packit acf257
           hPutStrLn h ("--readLine")
Packit acf257
           hPutStrLn h (either show show eval)
Packit acf257
    , hook_writeBlock = \ toStr str val -> do
Packit acf257
           hPutStrLn h ("--writeBlock " ++ show val)
Packit acf257
           hPutStrLn h (toStr str)
Packit acf257
    , hook_close = do
Packit acf257
           hPutStrLn h "--closing..."
Packit acf257
           hFlush h
Packit acf257
           hClose h
Packit acf257
    , hook_name = nm
Packit acf257
    }