{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE FlexibleInstances, Rank2Types, TypeSynonymInstances #-}
module Criterion.IO.Printf
(
CritHPrintfType
, note
, printError
, prolix
, writeCsv
) where
import Control.Monad (when)
import Control.Monad.Reader (ask, asks)
import Control.Monad.Trans (liftIO)
import Criterion.Monad (Criterion)
import Criterion.Types (Config(csvFile, verbosity), Verbosity(..))
import Data.Foldable (forM_)
import System.IO (Handle, hFlush, stderr, stdout)
import Text.Printf (PrintfArg)
import qualified Data.ByteString.Lazy as B
import qualified Data.Csv as Csv
import qualified Text.Printf (HPrintfType, hPrintf)
data PrintfCont = PrintfCont (IO ()) (forall a . PrintfArg a => a -> PrintfCont)
class CritHPrintfType a where
chPrintfImpl :: (Config -> Bool) -> PrintfCont -> a
instance CritHPrintfType (Criterion a) where
chPrintfImpl :: (Config -> Bool) -> PrintfCont -> Criterion a
chPrintfImpl Config -> Bool
check (PrintfCont IO ()
final forall a. PrintfArg a => a -> PrintfCont
_)
= do Config
x <- forall r (m :: * -> *). MonadReader r m => m r
ask
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Config -> Bool
check Config
x) (forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO ()
final forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> IO ()
hFlush Handle
stderr forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> IO ()
hFlush Handle
stdout))
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. HasCallStack => a
undefined
instance CritHPrintfType (IO a) where
chPrintfImpl :: (Config -> Bool) -> PrintfCont -> IO a
chPrintfImpl Config -> Bool
_ (PrintfCont IO ()
final forall a. PrintfArg a => a -> PrintfCont
_)
= IO ()
final forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> IO ()
hFlush Handle
stderr forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> IO ()
hFlush Handle
stdout forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return forall a. HasCallStack => a
undefined
instance (CritHPrintfType r, PrintfArg a) => CritHPrintfType (a -> r) where
chPrintfImpl :: (Config -> Bool) -> PrintfCont -> a -> r
chPrintfImpl Config -> Bool
check (PrintfCont IO ()
_ forall a. PrintfArg a => a -> PrintfCont
anotherArg) a
x
= forall a. CritHPrintfType a => (Config -> Bool) -> PrintfCont -> a
chPrintfImpl Config -> Bool
check (forall a. PrintfArg a => a -> PrintfCont
anotherArg a
x)
chPrintf :: CritHPrintfType r => (Config -> Bool) -> Handle -> String -> r
chPrintf :: forall r.
CritHPrintfType r =>
(Config -> Bool) -> Handle -> String -> r
chPrintf Config -> Bool
shouldPrint Handle
h String
s
= forall a. CritHPrintfType a => (Config -> Bool) -> PrintfCont -> a
chPrintfImpl Config -> Bool
shouldPrint (IO ()
-> (forall a r. (PrintfArg a, HPrintfType r) => a -> r)
-> PrintfCont
make (forall r. HPrintfType r => Handle -> String -> r
Text.Printf.hPrintf Handle
h String
s)
(forall r. HPrintfType r => Handle -> String -> r
Text.Printf.hPrintf Handle
h String
s))
where
make :: IO () -> (forall a r. (PrintfArg a, Text.Printf.HPrintfType r) =>
a -> r) -> PrintfCont
make :: IO ()
-> (forall a r. (PrintfArg a, HPrintfType r) => a -> r)
-> PrintfCont
make IO ()
curCall forall a r. (PrintfArg a, HPrintfType r) => a -> r
curCall' = IO () -> (forall a. PrintfArg a => a -> PrintfCont) -> PrintfCont
PrintfCont IO ()
curCall (\a
x -> IO ()
-> (forall a r. (PrintfArg a, HPrintfType r) => a -> r)
-> PrintfCont
make (forall a r. (PrintfArg a, HPrintfType r) => a -> r
curCall' a
x)
(forall a r. (PrintfArg a, HPrintfType r) => a -> r
curCall' a
x))
note :: (CritHPrintfType r) => String -> r
note :: forall r. CritHPrintfType r => String -> r
note = forall r.
CritHPrintfType r =>
(Config -> Bool) -> Handle -> String -> r
chPrintf ((forall a. Ord a => a -> a -> Bool
> Verbosity
Quiet) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Config -> Verbosity
verbosity) Handle
stdout
prolix :: (CritHPrintfType r) => String -> r
prolix :: forall r. CritHPrintfType r => String -> r
prolix = forall r.
CritHPrintfType r =>
(Config -> Bool) -> Handle -> String -> r
chPrintf ((forall a. Eq a => a -> a -> Bool
== Verbosity
Verbose) forall b c a. (b -> c) -> (a -> b) -> a -> c
. Config -> Verbosity
verbosity) Handle
stdout
printError :: (CritHPrintfType r) => String -> r
printError :: forall r. CritHPrintfType r => String -> r
printError = forall r.
CritHPrintfType r =>
(Config -> Bool) -> Handle -> String -> r
chPrintf (forall a b. a -> b -> a
const Bool
True) Handle
stderr
writeCsv :: Csv.ToRecord a => a -> Criterion ()
writeCsv :: forall a. ToRecord a => a -> Criterion ()
writeCsv a
val = do
Maybe String
csv <- forall r (m :: * -> *) a. MonadReader r m => (r -> a) -> m a
asks Config -> Maybe String
csvFile
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ Maybe String
csv forall a b. (a -> b) -> a -> b
$ \String
fn ->
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString -> IO ()
B.appendFile String
fn forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ToRecord a => [a] -> ByteString
Csv.encode forall a b. (a -> b) -> a -> b
$ [a
val]