{-# LANGUAGE TemplateHaskellQuotes #-}

-- | The 'IO' + 'Text' frontend. Using 'MonadUnliftIO' to abstract over things in 'IO'
module Unclog.IO.Text
  ( -- * logging functions for different loglevels
    debug
  , info
  , warn
  , fatal

    -- * run the frontend
  , withLoggingWithSubscribers

    -- * helpers
  , logText
  )
where

import Chronos (now)
import Data.Text.Encoding qualified as T
import Language.Haskell.TH.Syntax (Exp, Q, unTypeCode)
import Unclog.Common (LogLevel (..))
import Unclog.Frontend (mkLogEntry)
import Unclog.Subscriber (withLoggingWithSubscribers)
import UnliftIO (atomically, liftIO)

logText :: LogLevel -> Q Exp
logText :: LogLevel -> Q Exp
logText LogLevel
lvl =
  [|
    \chan t -> liftIO do
      ts <- now -- TODO(mangoiv): should be cached
      let entry = $(Code Q (Time -> StrictByteString -> LogEntry) -> Q Exp
forall a (m :: * -> *). Quote m => Code m a -> m Exp
unTypeCode (Code Q (Time -> StrictByteString -> LogEntry) -> Q Exp)
-> Code Q (Time -> StrictByteString -> LogEntry) -> Q Exp
forall a b. (a -> b) -> a -> b
$ LogLevel -> Code Q (Time -> StrictByteString -> LogEntry)
forall (q :: * -> *).
(Quote q, Quasi q) =>
LogLevel -> Code q (Time -> StrictByteString -> LogEntry)
mkLogEntry LogLevel
lvl) ts (T.encodeUtf8 t)
      atomically $ publishLogEntry chan entry
    |]

debug :: Q Exp
debug :: Q Exp
debug = LogLevel -> Q Exp
logText LogLevel
Debug

info :: Q Exp
info :: Q Exp
info = LogLevel -> Q Exp
logText LogLevel
Info

warn :: Q Exp
warn :: Q Exp
warn = LogLevel -> Q Exp
logText LogLevel
Warn

fatal :: Q Exp
fatal :: Q Exp
fatal = LogLevel -> Q Exp
logText LogLevel
Fatal