module Ouroboros.Network.Tracers
  ( NetworkSubscriptionTracers (..)
  , NetworkIPSubscriptionTracers
  , nullNetworkSubscriptionTracers
  , NetworkDNSSubscriptionTracers (..)
  , nullNetworkDNSSubscriptionTracers
  ) where

import Codec.CBOR.Term qualified as CBOR
import Control.Tracer (Tracer, nullTracer)

import Network.Mux.Trace qualified as Mx

import Ouroboros.Network.Driver (TraceSendRecv)
import Ouroboros.Network.ErrorPolicy
import Ouroboros.Network.Protocol.Handshake.Type
import Ouroboros.Network.Socket (ConnectionId)
import Ouroboros.Network.Subscription.Dns
import Ouroboros.Network.Subscription.Ip

-- | IP subscription tracers.
--
data NetworkSubscriptionTracers withIPList addr vNumber = NetworkSubscriptionTracers {
      forall (withIPList :: * -> *) addr vNumber.
NetworkSubscriptionTracers withIPList addr vNumber
-> Tracer IO (WithBearer (ConnectionId addr) Trace)
nsMuxTracer          :: Tracer IO (Mx.WithBearer (ConnectionId addr) Mx.Trace),
      -- ^ low level mux-network tracer, which logs mux sdu (send and received)
      -- and other low level multiplexing events.
      forall (withIPList :: * -> *) addr vNumber.
NetworkSubscriptionTracers withIPList addr vNumber
-> Tracer
     IO
     (WithBearer
        (ConnectionId addr) (TraceSendRecv (Handshake vNumber Term)))
nsHandshakeTracer    :: Tracer IO (Mx.WithBearer (ConnectionId addr)
                                            (TraceSendRecv (Handshake vNumber CBOR.Term))),
      -- ^ handshake protocol tracer; it is important for analysing version
      -- negotation mismatches.
      forall (withIPList :: * -> *) addr vNumber.
NetworkSubscriptionTracers withIPList addr vNumber
-> Tracer IO (WithAddr addr ErrorPolicyTrace)
nsErrorPolicyTracer  :: Tracer IO (WithAddr addr ErrorPolicyTrace),
      -- ^ error policy tracer; must not be 'nullTracer', otherwise all the
      -- exceptions which are not matched by any error policy will be caught
      -- and not logged or rethrown.
      forall (withIPList :: * -> *) addr vNumber.
NetworkSubscriptionTracers withIPList addr vNumber
-> Tracer IO (withIPList (SubscriptionTrace addr))
nsSubscriptionTracer :: Tracer IO (withIPList (SubscriptionTrace addr))
      -- ^ subscription tracers; it is infrequent it should not be 'nullTracer'
      -- by default.
    }

type NetworkIPSubscriptionTracers addr vNumber =
     NetworkSubscriptionTracers WithIPList addr vNumber

nullNetworkSubscriptionTracers :: NetworkSubscriptionTracers withIPList addr vNumber
nullNetworkSubscriptionTracers :: forall (withIPList :: * -> *) addr vNumber.
NetworkSubscriptionTracers withIPList addr vNumber
nullNetworkSubscriptionTracers = NetworkSubscriptionTracers {
      nsMuxTracer :: Tracer IO (WithBearer (ConnectionId addr) Trace)
nsMuxTracer          = Tracer IO (WithBearer (ConnectionId addr) Trace)
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer,
      nsHandshakeTracer :: Tracer
  IO
  (WithBearer
     (ConnectionId addr) (TraceSendRecv (Handshake vNumber Term)))
nsHandshakeTracer    = Tracer
  IO
  (WithBearer
     (ConnectionId addr) (TraceSendRecv (Handshake vNumber Term)))
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer,
      nsErrorPolicyTracer :: Tracer IO (WithAddr addr ErrorPolicyTrace)
nsErrorPolicyTracer  = Tracer IO (WithAddr addr ErrorPolicyTrace)
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer,
      nsSubscriptionTracer :: Tracer IO (withIPList (SubscriptionTrace addr))
nsSubscriptionTracer = Tracer IO (withIPList (SubscriptionTrace addr))
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer
    }

-- | DNS subscription tracers.
--
data NetworkDNSSubscriptionTracers vNumber addr = NetworkDNSSubscriptionTracers {
      forall vNumber addr.
NetworkDNSSubscriptionTracers vNumber addr
-> Tracer IO (WithBearer (ConnectionId addr) Trace)
ndstMuxTracer          :: Tracer IO (Mx.WithBearer (ConnectionId addr) Mx.Trace),
      -- ^ low level mux-network tracer, which logs mux sdu (send and received)
      -- and other low level multiplexing events.
      forall vNumber addr.
NetworkDNSSubscriptionTracers vNumber addr
-> Tracer
     IO
     (WithBearer
        (ConnectionId addr) (TraceSendRecv (Handshake vNumber Term)))
ndstHandshakeTracer    :: Tracer IO (Mx.WithBearer (ConnectionId addr)
                                            (TraceSendRecv (Handshake vNumber CBOR.Term))),
      -- ^ handshake protocol tracer; it is important for analysing version
      -- negotation mismatches.
      forall vNumber addr.
NetworkDNSSubscriptionTracers vNumber addr
-> Tracer IO (WithAddr addr ErrorPolicyTrace)
ndstErrorPolicyTracer  :: Tracer IO (WithAddr addr ErrorPolicyTrace),
      -- ^ error policy tracer; must not be 'nullTracer', otherwise all the
      -- exceptions which are not matched by any error policy will be caught
      -- and not logged or rethrown.
      forall vNumber addr.
NetworkDNSSubscriptionTracers vNumber addr
-> Tracer IO (WithDomainName (SubscriptionTrace addr))
ndstSubscriptionTracer :: Tracer IO (WithDomainName (SubscriptionTrace addr)),
      -- ^ subscription tracer; it is infrequent it should not be 'nullTracer'
      -- by default.
      forall vNumber addr.
NetworkDNSSubscriptionTracers vNumber addr
-> Tracer IO (WithDomainName DnsTrace)
ndstDnsTracer          :: Tracer IO (WithDomainName DnsTrace)
      -- ^ dns resolver tracer; it is infrequent it should not be 'nullTracer'
      -- by default.

    }

nullNetworkDNSSubscriptionTracers :: NetworkDNSSubscriptionTracers vNumber peerid
nullNetworkDNSSubscriptionTracers :: forall vNumber peerid. NetworkDNSSubscriptionTracers vNumber peerid
nullNetworkDNSSubscriptionTracers = NetworkDNSSubscriptionTracers {
      ndstMuxTracer :: Tracer IO (WithBearer (ConnectionId peerid) Trace)
ndstMuxTracer          = Tracer IO (WithBearer (ConnectionId peerid) Trace)
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer,
      ndstHandshakeTracer :: Tracer
  IO
  (WithBearer
     (ConnectionId peerid) (TraceSendRecv (Handshake vNumber Term)))
ndstHandshakeTracer    = Tracer
  IO
  (WithBearer
     (ConnectionId peerid) (TraceSendRecv (Handshake vNumber Term)))
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer,
      ndstErrorPolicyTracer :: Tracer IO (WithAddr peerid ErrorPolicyTrace)
ndstErrorPolicyTracer  = Tracer IO (WithAddr peerid ErrorPolicyTrace)
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer,
      ndstSubscriptionTracer :: Tracer IO (WithDomainName (SubscriptionTrace peerid))
ndstSubscriptionTracer = Tracer IO (WithDomainName (SubscriptionTrace peerid))
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer,
      ndstDnsTracer :: Tracer IO (WithDomainName DnsTrace)
ndstDnsTracer          = Tracer IO (WithDomainName DnsTrace)
forall (m :: * -> *) a. Applicative m => Tracer m a
nullTracer
    }