module BB8.Common (debug, debugShow) where

import qualified Control.Concurrent.MVar as M
import qualified Data.Time.Clock as Time
import qualified Data.Time.Format as Time
import qualified System.IO.Unsafe as Sys
import qualified System.IO as Sys

globalLock :: M.MVar ()
globalLock = Sys.unsafePerformIO $ M.newMVar ()

debug :: String -> IO ()
debug msg = M.withMVar globalLock $ \ () -> do
  now <- Time.formatTime Time.defaultTimeLocale "%F %T:%q: " <$> Time.getCurrentTime
  Sys.hPutStrLn Sys.stderr (now ++ msg)

debugShow :: (Show t) => t -> IO ()
debugShow = debug . show
