module Csound.Dynamic.Debug
  ( IsDebug (..)
  , traceIf
  , traceShowIf
  ) where

import Debug.Trace (trace, traceShow)
import Data.Default

newtype IsDebug = IsDebug Bool
  deriving newtype (Int -> IsDebug -> ShowS
[IsDebug] -> ShowS
IsDebug -> String
(Int -> IsDebug -> ShowS)
-> (IsDebug -> String) -> ([IsDebug] -> ShowS) -> Show IsDebug
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> IsDebug -> ShowS
showsPrec :: Int -> IsDebug -> ShowS
$cshow :: IsDebug -> String
show :: IsDebug -> String
$cshowList :: [IsDebug] -> ShowS
showList :: [IsDebug] -> ShowS
Show, IsDebug -> IsDebug -> Bool
(IsDebug -> IsDebug -> Bool)
-> (IsDebug -> IsDebug -> Bool) -> Eq IsDebug
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IsDebug -> IsDebug -> Bool
== :: IsDebug -> IsDebug -> Bool
$c/= :: IsDebug -> IsDebug -> Bool
/= :: IsDebug -> IsDebug -> Bool
Eq, Eq IsDebug
Eq IsDebug =>
(IsDebug -> IsDebug -> Ordering)
-> (IsDebug -> IsDebug -> Bool)
-> (IsDebug -> IsDebug -> Bool)
-> (IsDebug -> IsDebug -> Bool)
-> (IsDebug -> IsDebug -> Bool)
-> (IsDebug -> IsDebug -> IsDebug)
-> (IsDebug -> IsDebug -> IsDebug)
-> Ord IsDebug
IsDebug -> IsDebug -> Bool
IsDebug -> IsDebug -> Ordering
IsDebug -> IsDebug -> IsDebug
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: IsDebug -> IsDebug -> Ordering
compare :: IsDebug -> IsDebug -> Ordering
$c< :: IsDebug -> IsDebug -> Bool
< :: IsDebug -> IsDebug -> Bool
$c<= :: IsDebug -> IsDebug -> Bool
<= :: IsDebug -> IsDebug -> Bool
$c> :: IsDebug -> IsDebug -> Bool
> :: IsDebug -> IsDebug -> Bool
$c>= :: IsDebug -> IsDebug -> Bool
>= :: IsDebug -> IsDebug -> Bool
$cmax :: IsDebug -> IsDebug -> IsDebug
max :: IsDebug -> IsDebug -> IsDebug
$cmin :: IsDebug -> IsDebug -> IsDebug
min :: IsDebug -> IsDebug -> IsDebug
Ord, ReadPrec [IsDebug]
ReadPrec IsDebug
Int -> ReadS IsDebug
ReadS [IsDebug]
(Int -> ReadS IsDebug)
-> ReadS [IsDebug]
-> ReadPrec IsDebug
-> ReadPrec [IsDebug]
-> Read IsDebug
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS IsDebug
readsPrec :: Int -> ReadS IsDebug
$creadList :: ReadS [IsDebug]
readList :: ReadS [IsDebug]
$creadPrec :: ReadPrec IsDebug
readPrec :: ReadPrec IsDebug
$creadListPrec :: ReadPrec [IsDebug]
readListPrec :: ReadPrec [IsDebug]
Read)

instance Default IsDebug where
  def :: IsDebug
def = Bool -> IsDebug
IsDebug Bool
False

traceIf :: IsDebug -> String -> a -> a
traceIf :: forall a. IsDebug -> String -> a -> a
traceIf (IsDebug Bool
isDebug) String
str a
a
  | Bool -> Bool
not Bool
isDebug = a
a
  | Bool
otherwise = String -> a -> a
forall a. String -> a -> a
trace String
str a
a

traceShowIf :: Show b => IsDebug -> b -> a -> a
traceShowIf :: forall b a. Show b => IsDebug -> b -> a -> a
traceShowIf (IsDebug Bool
isDebug) b
debugValue a
a
  | Bool -> Bool
not Bool
isDebug = a
a
  | Bool
otherwise   = b -> a -> a
forall a b. Show a => a -> b -> b
traceShow b
debugValue a
a