{-# LANGUAGE DerivingStrategies  #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Plutus.ChainIndex.Logging where

import Cardano.BM.Configuration (setup)
import Cardano.BM.Configuration.Model qualified as CM
import Cardano.BM.Data.BackendKind (BackendKind (AggregationBK, EKGViewBK, KatipBK, MonitoringBK))
import Cardano.BM.Data.Configuration (Endpoint (Endpoint))
import Cardano.BM.Data.Output (ScribeDefinition (ScribeDefinition, scFormat, scKind, scMaxSev, scMinSev, scName, scPrivacy, scRotation),
                               ScribeFormat (ScText), ScribeKind (StdoutSK), ScribePrivacy (ScPublic))
import Cardano.BM.Data.Severity (Severity (Info))

-- | Logging (definitions from Plutus.PAB.Monitoring.Config)

-- | A default 'CM.Configuration' that logs on 'Info' and above
--   to stdout
defaultConfig :: IO CM.Configuration
defaultConfig :: IO Configuration
defaultConfig = do
  Configuration
c <- IO Configuration
CM.empty
  Configuration -> Severity -> IO ()
CM.setMinSeverity Configuration
c Severity
Info
  Configuration -> [BackendKind] -> IO ()
CM.setSetupBackends Configuration
c [ BackendKind
KatipBK
                        , BackendKind
AggregationBK
                        , BackendKind
MonitoringBK
                        , BackendKind
EKGViewBK
                        ]
  Configuration -> [BackendKind] -> IO ()
CM.setDefaultBackends Configuration
c [BackendKind
KatipBK, BackendKind
AggregationBK, BackendKind
EKGViewBK]
  Configuration -> [ScribeDefinition] -> IO ()
CM.setSetupScribes Configuration
c [ ScribeDefinition :: ScribeKind
-> ScribeFormat
-> Text
-> ScribePrivacy
-> Maybe RotationParameters
-> Severity
-> Severity
-> ScribeDefinition
ScribeDefinition {
                          scName :: Text
scName = Text
"stdout"
                        , scKind :: ScribeKind
scKind = ScribeKind
StdoutSK
                        , scFormat :: ScribeFormat
scFormat = ScribeFormat
ScText
                        , scPrivacy :: ScribePrivacy
scPrivacy = ScribePrivacy
ScPublic
                        , scRotation :: Maybe RotationParameters
scRotation = Maybe RotationParameters
forall a. Maybe a
Nothing
                        , scMinSev :: Severity
scMinSev = Severity
forall a. Bounded a => a
minBound
                        , scMaxSev :: Severity
scMaxSev = Severity
forall a. Bounded a => a
maxBound
                        }]
  Configuration -> [Text] -> IO ()
CM.setDefaultScribes Configuration
c [Text
"StdoutSK::stdout"]
  Configuration -> Maybe Endpoint -> IO ()
CM.setEKGBindAddr Configuration
c (Maybe Endpoint -> IO ()) -> Maybe Endpoint -> IO ()
forall a b. (a -> b) -> a -> b
$ Endpoint -> Maybe Endpoint
forall a. a -> Maybe a
Just (HostPort -> Endpoint
Endpoint (String
"localhost", Port
12790))
  Configuration -> IO Configuration
forall (f :: * -> *) a. Applicative f => a -> f a
pure Configuration
c

-- | Load a 'CM.Configuration' from a YAML file.
loadConfig :: FilePath -> IO CM.Configuration
loadConfig :: String -> IO Configuration
loadConfig = String -> IO Configuration
setup