{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
module Debug.Trace.Text
(
traceEvent
, traceEventIO
, traceMarker
, traceMarkerIO
) where
import Control.Monad (when)
import Foreign.C.String (CString)
import GHC.Exts (Ptr(..), traceEvent#, traceMarker#)
import GHC.IO (IO(..))
import qualified System.IO.Unsafe as Unsafe
import qualified Data.ByteString as B
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Debug.Trace.Flags (userTracingEnabled)
traceEvent :: T.Text -> a -> a
traceEvent :: forall a. Text -> a -> a
traceEvent Text
message a
a
| Bool
userTracingEnabled = Text -> a -> a
forall a. Text -> a -> a
traceEvent' Text
message a
a
| Bool
otherwise = a
a
traceEvent' :: T.Text -> a -> a
traceEvent' :: forall a. Text -> a -> a
traceEvent' Text
message a
a = IO a -> a
forall a. IO a -> a
Unsafe.unsafeDupablePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ do
Text -> IO ()
traceEventIO' Text
message
a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# NOINLINE traceEvent' #-}
traceEventIO :: T.Text -> IO ()
traceEventIO :: Text -> IO ()
traceEventIO Text
message = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
userTracingEnabled (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
traceEventIO' Text
message
traceEventIO' :: T.Text -> IO ()
traceEventIO' :: Text -> IO ()
traceEventIO' Text
message = Text -> (CString -> IO ()) -> IO ()
forall a. Text -> (CString -> IO a) -> IO a
withCString Text
message ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr Addr#
p) -> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case Addr# -> State# RealWorld -> State# RealWorld
forall d. Addr# -> State# d -> State# d
traceEvent# Addr#
p State# RealWorld
s of
State# RealWorld
s' -> (# State# RealWorld
s', () #)
traceMarker :: T.Text -> a -> a
traceMarker :: forall a. Text -> a -> a
traceMarker Text
message a
a
| Bool
userTracingEnabled = Text -> a -> a
forall a. Text -> a -> a
traceMarker' Text
message a
a
| Bool
otherwise = a
a
traceMarker' :: T.Text -> a -> a
traceMarker' :: forall a. Text -> a -> a
traceMarker' Text
message a
a = IO a -> a
forall a. IO a -> a
Unsafe.unsafeDupablePerformIO (IO a -> a) -> IO a -> a
forall a b. (a -> b) -> a -> b
$ do
Text -> IO ()
traceMarkerIO' Text
message
a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
{-# NOINLINE traceMarker' #-}
traceMarkerIO :: T.Text -> IO ()
traceMarkerIO :: Text -> IO ()
traceMarkerIO Text
message = Bool -> IO () -> IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when Bool
userTracingEnabled (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
traceMarkerIO' Text
message
traceMarkerIO' :: T.Text -> IO ()
traceMarkerIO' :: Text -> IO ()
traceMarkerIO' Text
message = Text -> (CString -> IO ()) -> IO ()
forall a. Text -> (CString -> IO a) -> IO a
withCString Text
message ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(Ptr Addr#
p) -> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case Addr# -> State# RealWorld -> State# RealWorld
forall d. Addr# -> State# d -> State# d
traceMarker# Addr#
p State# RealWorld
s of
State# RealWorld
s' -> (# State# RealWorld
s', () #)
withCString :: T.Text -> (CString -> IO a) -> IO a
withCString :: forall a. Text -> (CString -> IO a) -> IO a
withCString Text
text = ByteString -> (CString -> IO a) -> IO a
forall a. ByteString -> (CString -> IO a) -> IO a
B.useAsCString (Text -> ByteString
TE.encodeUtf8 Text
text)