module Test.MuCheck.Utils.Print where
import Debug.Trace
import Data.List(intercalate)
import Control.Monad (liftM)
import GHC.IO.Handle
import System.IO
import System.Directory
import System.Environment
import System.IO.Temp (withSystemTempFile)
(./.) :: (Show a, Integral a) => a -> a -> String
n ./. t = "(" ++ show (n * 100 `div` t) ++ "%)"
showAS :: [String] -> String
showAS = intercalate "\n"
showA :: Show a => [a] -> String
showA = showAS . map show
tt :: Show a => a -> a
tt v = trace (">" ++ show v) v
catchOutputStr :: IO a -> IO (a,String)
catchOutputStr f = do
isdebug <- lookupEnv "MuDEBUG"
case isdebug of
Just _ -> liftM (,"") f
Nothing -> withSystemTempFile "_mucheck" $ \tmpf tmph -> do
res <- redirectToHandle f tmph
str <- readFile tmpf
removeFile tmpf
return (res,str)
catchOutput :: String -> IO a -> IO a
catchOutput fn f = withFile fn WriteMode (redirectToHandle f)
redirectToHandle :: IO b -> Handle -> IO b
redirectToHandle f tmph = do
stdout_dup <- hDuplicate stdout
stderr_dup <- hDuplicate stderr
hDuplicateTo tmph stdout
hDuplicateTo tmph stderr
hClose tmph
res <- f
hDuplicateTo stdout_dup stdout
hDuplicateTo stderr_dup stderr
return res