module OpenTelemetry.Debug where

import Control.Concurrent.STM
import Control.Monad.IO.Class
import System.Environment
import System.IO
import System.IO.Unsafe

dd_ :: Show a => String -> a -> IO ()
dd_ :: String -> a -> IO ()
dd_ =
  IO (String -> a -> IO ()) -> String -> a -> IO ()
forall a. IO a -> a
unsafePerformIO (IO (String -> a -> IO ()) -> String -> a -> IO ())
-> IO (String -> a -> IO ()) -> String -> a -> IO ()
forall a b. (a -> b) -> a -> b
$
    String -> IO (Maybe String)
lookupEnv String
"OPENTELEMETRY_DEBUG" IO (Maybe String)
-> (Maybe String -> IO (String -> a -> IO ()))
-> IO (String -> a -> IO ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Maybe String
Nothing -> (String -> a -> IO ()) -> IO (String -> a -> IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> a -> IO ()) -> IO (String -> a -> IO ()))
-> (String -> a -> IO ()) -> IO (String -> a -> IO ())
forall a b. (a -> b) -> a -> b
$ \String
_ a
_ -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Just String
"0" -> (String -> a -> IO ()) -> IO (String -> a -> IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> a -> IO ()) -> IO (String -> a -> IO ()))
-> (String -> a -> IO ()) -> IO (String -> a -> IO ())
forall a b. (a -> b) -> a -> b
$ \String
_ a
_ -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Just String
"false" -> (String -> a -> IO ()) -> IO (String -> a -> IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> a -> IO ()) -> IO (String -> a -> IO ()))
-> (String -> a -> IO ()) -> IO (String -> a -> IO ())
forall a b. (a -> b) -> a -> b
$ \String
_ a
_ -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Maybe String
_ -> (String -> a -> IO ()) -> IO (String -> a -> IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> a -> IO ()) -> IO (String -> a -> IO ()))
-> (String -> a -> IO ()) -> IO (String -> a -> IO ())
forall a b. (a -> b) -> a -> b
$ \String
s a
thing -> IO () -> IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        Handle -> String -> IO ()
hPutStr Handle
stderr (String
s String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": ")
        Handle -> String -> IO ()
hPutStrLn Handle
stderr (a -> String
forall a. Show a => a -> String
show a
thing)
{-# NOINLINE dd_ #-}

d_ :: String -> IO ()
d_ :: String -> IO ()
d_ =
  IO (String -> IO ()) -> String -> IO ()
forall a. IO a -> a
unsafePerformIO (IO (String -> IO ()) -> String -> IO ())
-> IO (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$
    String -> IO (Maybe String)
lookupEnv String
"OPENTELEMETRY_DEBUG" IO (Maybe String)
-> (Maybe String -> IO (String -> IO ())) -> IO (String -> IO ())
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Maybe String
Nothing -> (String -> IO ()) -> IO (String -> IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> IO ()) -> IO (String -> IO ()))
-> (String -> IO ()) -> IO (String -> IO ())
forall a b. (a -> b) -> a -> b
$ \String
_ -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Just String
"0" -> (String -> IO ()) -> IO (String -> IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> IO ()) -> IO (String -> IO ()))
-> (String -> IO ()) -> IO (String -> IO ())
forall a b. (a -> b) -> a -> b
$ \String
_ -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Just String
"false" -> (String -> IO ()) -> IO (String -> IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> IO ()) -> IO (String -> IO ()))
-> (String -> IO ()) -> IO (String -> IO ())
forall a b. (a -> b) -> a -> b
$ \String
_ -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Maybe String
_ -> (String -> IO ()) -> IO (String -> IO ())
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((String -> IO ()) -> IO (String -> IO ()))
-> (String -> IO ()) -> IO (String -> IO ())
forall a b. (a -> b) -> a -> b
$ \String
s -> IO () -> IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
        Handle -> String -> IO ()
hPutStrLn Handle
stderr String
s
{-# NOINLINE d_ #-}

inc :: Int -> TVar Int -> IO ()
inc :: Int -> TVar Int -> IO ()
inc Int
amount TVar Int
counterVar = STM () -> IO ()
forall a. STM a -> IO a
atomically (STM () -> IO ()) -> STM () -> IO ()
forall a b. (a -> b) -> a -> b
$ TVar Int -> (Int -> Int) -> STM ()
forall a. TVar a -> (a -> a) -> STM ()
modifyTVar TVar Int
counterVar (Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
amount)