packages feed

unclogging-0.1.0.0: src/Unclog/IO/Text.hs

{-# 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 lvl =
  [|
    \chan t -> liftIO do
      ts <- now -- TODO(mangoiv): should be cached
      let entry = $(unTypeCode $ mkLogEntry lvl) ts (T.encodeUtf8 t)
      atomically $ publishLogEntry chan entry
    |]

debug :: Q Exp
debug = logText Debug

info :: Q Exp
info = logText Info

warn :: Q Exp
warn = logText Warn

fatal :: Q Exp
fatal = logText Fatal