{-# LANGUAGE PackageImports #-}

module Cardano.Node.Tracing.Tracers.Resources
  ( startResourceTracer
  , namesForResources
  , severityResources
  ) where

import           "contra-tracer" Control.Tracer

import           Cardano.Logging (SeverityS (..))
import           Cardano.Logging.Resources
import           Cardano.Prelude hiding (trace)

startResourceTracer
  :: Tracer IO ResourceStats
  -> Int
  -> IO ()
startResourceTracer :: Tracer IO ResourceStats -> Int -> IO ()
startResourceTracer Tracer IO ResourceStats
tr Int
delayMilliseconds = do
    Async ()
as <- IO () -> IO (Async ())
forall a. IO a -> IO (Async a)
async IO ()
resourceThread
    Async () -> IO ()
forall a. Async a -> IO ()
link Async ()
as
  where
    resourceThread :: IO ()
    resourceThread :: IO ()
resourceThread = IO () -> IO ()
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
      Maybe ResourceStats
mbrs <- IO (Maybe ResourceStats)
readResourceStats
      case Maybe ResourceStats
mbrs of
        Just ResourceStats
rs -> Tracer IO ResourceStats -> ResourceStats -> IO ()
forall (m :: * -> *) a. Tracer m a -> a -> m ()
traceWith Tracer IO ResourceStats
tr ResourceStats
rs
        Maybe ResourceStats
Nothing -> () -> IO ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      Int -> IO ()
threadDelay (Int
delayMilliseconds Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
1000)

--------------------------------------------------------------------------------
-- ResourceStats Tracer
--------------------------------------------------------------------------------

namesForResources :: ResourceStats -> [Text]
namesForResources :: ResourceStats -> [Text]
namesForResources ResourceStats
_ = []

severityResources :: ResourceStats -> SeverityS
severityResources :: ResourceStats -> SeverityS
severityResources ResourceStats
_ = SeverityS
Info