{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE UndecidableInstances #-}

{-# OPTIONS_GHC -Wno-name-shadowing -Wno-orphans #-}

module Cardano.Node.Tracing.Tracers.Shutdown
  ( namesForShutdown
  , severityShutdown
  , ppShutdownTrace
  , docShutdown
  ) where

import           Cardano.Logging
import           Cardano.Node.Handlers.Shutdown
import           Data.Aeson (Value (..), (.=))
import           Data.Monoid (mconcat, (<>))
import           Data.Text (Text, pack)
import           Prelude (show)

--------------------------------------------------------------------------------
-- ShutdownTrace Tracer
--------------------------------------------------------------------------------

namesForShutdown :: ShutdownTrace -> [Text]
namesForShutdown :: ShutdownTrace -> [Text]
namesForShutdown = \case
  ShutdownRequested{}         -> [Text
"ShutdownRequested"]
  AbnormalShutdown{}          -> [Text
"AbnormalShutdown"]
  ShutdownUnexpectedInput{}   -> [Text
"ShutdownUnexpectedInput"]
  RequestingShutdown{}        -> [Text
"RequestingShutdown"]
  ShutdownArmedAt{}           -> [Text
"ShutdownArmedAt"]

severityShutdown :: ShutdownTrace -> SeverityS
severityShutdown :: ShutdownTrace -> SeverityS
severityShutdown = \case
  ShutdownRequested{}         -> SeverityS
Warning
  AbnormalShutdown{}          -> SeverityS
Error
  ShutdownUnexpectedInput{}   -> SeverityS
Error
  RequestingShutdown{}        -> SeverityS
Warning
  ShutdownArmedAt{}           -> SeverityS
Warning

ppShutdownTrace :: ShutdownTrace -> Text
ppShutdownTrace :: ShutdownTrace -> Text
ppShutdownTrace = \case
  ShutdownTrace
ShutdownRequested             -> Text
"Received shutdown request"
  ShutdownTrace
AbnormalShutdown              -> Text
"non-isEOFerror shutdown request"
  ShutdownUnexpectedInput Text
text  ->
    Text
"Received shutdown request but found unexpected input in --shutdown-ipc FD: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
text
  RequestingShutdown Text
reason     -> Text
"Ringing the node shutdown doorbell:  " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
reason
  ShutdownArmedAt ShutdownOn
lim           -> Text
"Will terminate upon reaching " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
pack (ShutdownOn -> String
forall a. Show a => a -> String
show ShutdownOn
lim)

instance LogFormatting ShutdownTrace where
  forHuman :: ShutdownTrace -> Text
forHuman = ShutdownTrace -> Text
ppShutdownTrace

  forMachine :: DetailLevel -> ShutdownTrace -> Object
forMachine DetailLevel
_ = \case
    ShutdownTrace
ShutdownRequested ->
          [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind"   Key -> Value -> Object
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"ShutdownRequested" ]
    ShutdownTrace
AbnormalShutdown  ->
          [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind"   Key -> Value -> Object
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"AbnormalShutdown" ]
    ShutdownUnexpectedInput Text
text ->
          [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind"   Key -> Value -> Object
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"AbnormalShutdown"
                  , Key
"unexpected" Key -> Value -> Object
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
text ]
    RequestingShutdown Text
reason ->
          [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind"   Key -> Value -> Object
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"RequestingShutdown"
                  , Key
"reason" Key -> Value -> Object
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
reason ]
    ShutdownArmedAt ShutdownOn
lim ->
          [Object] -> Object
forall a. Monoid a => [a] -> a
mconcat [ Key
"kind"   Key -> Value -> Object
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
String Text
"ShutdownArmedAt"
                  , Key
"limit"  Key -> ShutdownOn -> Object
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= ShutdownOn
lim ]

docShutdown :: Documented ShutdownTrace
docShutdown :: Documented ShutdownTrace
docShutdown = [Text] -> Documented ShutdownTrace -> Documented ShutdownTrace
forall a b. [Text] -> Documented a -> Documented b
addDocumentedNamespace  [] Documented ShutdownTrace
docShutdown'

docShutdown' :: Documented ShutdownTrace
docShutdown' :: Documented ShutdownTrace
docShutdown' = [DocMsg ShutdownTrace] -> Documented ShutdownTrace
forall a. [DocMsg a] -> Documented a
Documented
  [ [Text] -> [(Text, Text)] -> Text -> DocMsg ShutdownTrace
forall a. [Text] -> [(Text, Text)] -> Text -> DocMsg a
DocMsg
      [Text
"ShutdownRequested"]
      []
      Text
"Node shutdown was requested."
  ,  [Text] -> [(Text, Text)] -> Text -> DocMsg ShutdownTrace
forall a. [Text] -> [(Text, Text)] -> Text -> DocMsg a
DocMsg
      [Text
"AbnormalShutdown"]
      []
      Text
"non-isEOFerror shutdown request"
  ,  [Text] -> [(Text, Text)] -> Text -> DocMsg ShutdownTrace
forall a. [Text] -> [(Text, Text)] -> Text -> DocMsg a
DocMsg
      [Text
"ShutdownUnexpectedInput"]
      []
      Text
"Received shutdown request but found unexpected input in --shutdown-ipc FD: "
  , [Text] -> [(Text, Text)] -> Text -> DocMsg ShutdownTrace
forall a. [Text] -> [(Text, Text)] -> Text -> DocMsg a
DocMsg
      [Text
"RequestingShutdown"]
      []
      Text
"Ringing the node shutdown doorbell"
  , [Text] -> [(Text, Text)] -> Text -> DocMsg ShutdownTrace
forall a. [Text] -> [(Text, Text)] -> Text -> DocMsg a
DocMsg
      [Text
"ShutdownArmedAt"]
      []
      Text
"Setting up node shutdown at given slot / block."
  ]