module Hypermedia.Datastar.Logger
  ( DatastarLogger (..)
  , nullLogger
  , stderrLogger
  )
where

import Data.Text (Text)
import Data.Text qualified as T
import System.IO (hPutStrLn, stderr)

data DatastarLogger = DatastarLogger
  { DatastarLogger -> Text -> IO ()
logDebug :: Text -> IO ()
  , DatastarLogger -> Text -> IO ()
logInfo :: Text -> IO ()
  , DatastarLogger -> Text -> IO ()
logWarn :: Text -> IO ()
  , DatastarLogger -> Text -> IO ()
logError :: Text -> IO ()
  }

nullLogger :: DatastarLogger
nullLogger :: DatastarLogger
nullLogger =
  DatastarLogger
    { logDebug :: Text -> IO ()
logDebug = \Text
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    , logInfo :: Text -> IO ()
logInfo = \Text
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    , logWarn :: Text -> IO ()
logWarn = \Text
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    , logError :: Text -> IO ()
logError = \Text
_ -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    }

stderrLogger :: DatastarLogger
stderrLogger :: DatastarLogger
stderrLogger =
  DatastarLogger
    { logDebug :: Text -> IO ()
logDebug = Text -> Text -> IO ()
logAt Text
"DEBUG"
    , logInfo :: Text -> IO ()
logInfo = Text -> Text -> IO ()
logAt Text
"INFO"
    , logWarn :: Text -> IO ()
logWarn = Text -> Text -> IO ()
logAt Text
"WARN"
    , logError :: Text -> IO ()
logError = Text -> Text -> IO ()
logAt Text
"ERROR"
    }
 where
  logAt :: Text -> Text -> IO ()
  logAt :: Text -> Text -> IO ()
logAt Text
level Text
msg = Handle -> String -> IO ()
hPutStrLn Handle
stderr (String -> IO ()) -> String -> IO ()
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text
"[datastar] [" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
level Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"] " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
msg)