{-# LANGUAGE UndecidableInstances #-}

-- | This module provides the `Log` type and associated functions.
module Shrun.Logging.Types
  ( -- * Basic Types
    Log (..),
    LogMode (..),
    LogLevel (..),

    -- * Console Logs
    ConsoleLog,
    LogRegion (..),

    -- * File Logs
    FileLog,
  )
where

import Shrun.Data.Command (CommandP1)
import Shrun.Data.Text (UnlinedText)
import Shrun.Logging.Types.Internal
  ( ConsoleLog,
    FileLog,
    LogMode
      ( LogModeAppend,
        LogModeFinish,
        LogModeSet
      ),
  )
import Shrun.Prelude

-- | Determines the logging level.
data LogLevel
  = LevelCommand
  | LevelFinished
  | LevelTimer
  | LevelSuccess
  | LevelWarn
  | LevelError
  | LevelFatal
  deriving stock (LogLevel
LogLevel -> LogLevel -> Bounded LogLevel
forall a. a -> a -> Bounded a
$cminBound :: LogLevel
minBound :: LogLevel
$cmaxBound :: LogLevel
maxBound :: LogLevel
Bounded, Int -> LogLevel
LogLevel -> Int
LogLevel -> [LogLevel]
LogLevel -> LogLevel
LogLevel -> LogLevel -> [LogLevel]
LogLevel -> LogLevel -> LogLevel -> [LogLevel]
(LogLevel -> LogLevel)
-> (LogLevel -> LogLevel)
-> (Int -> LogLevel)
-> (LogLevel -> Int)
-> (LogLevel -> [LogLevel])
-> (LogLevel -> LogLevel -> [LogLevel])
-> (LogLevel -> LogLevel -> [LogLevel])
-> (LogLevel -> LogLevel -> LogLevel -> [LogLevel])
-> Enum LogLevel
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: LogLevel -> LogLevel
succ :: LogLevel -> LogLevel
$cpred :: LogLevel -> LogLevel
pred :: LogLevel -> LogLevel
$ctoEnum :: Int -> LogLevel
toEnum :: Int -> LogLevel
$cfromEnum :: LogLevel -> Int
fromEnum :: LogLevel -> Int
$cenumFrom :: LogLevel -> [LogLevel]
enumFrom :: LogLevel -> [LogLevel]
$cenumFromThen :: LogLevel -> LogLevel -> [LogLevel]
enumFromThen :: LogLevel -> LogLevel -> [LogLevel]
$cenumFromTo :: LogLevel -> LogLevel -> [LogLevel]
enumFromTo :: LogLevel -> LogLevel -> [LogLevel]
$cenumFromThenTo :: LogLevel -> LogLevel -> LogLevel -> [LogLevel]
enumFromThenTo :: LogLevel -> LogLevel -> LogLevel -> [LogLevel]
Enum, LogLevel -> LogLevel -> Bool
(LogLevel -> LogLevel -> Bool)
-> (LogLevel -> LogLevel -> Bool) -> Eq LogLevel
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LogLevel -> LogLevel -> Bool
== :: LogLevel -> LogLevel -> Bool
$c/= :: LogLevel -> LogLevel -> Bool
/= :: LogLevel -> LogLevel -> Bool
Eq, Int -> LogLevel -> ShowS
[LogLevel] -> ShowS
LogLevel -> [Char]
(Int -> LogLevel -> ShowS)
-> (LogLevel -> [Char]) -> ([LogLevel] -> ShowS) -> Show LogLevel
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LogLevel -> ShowS
showsPrec :: Int -> LogLevel -> ShowS
$cshow :: LogLevel -> [Char]
show :: LogLevel -> [Char]
$cshowList :: [LogLevel] -> ShowS
showList :: [LogLevel] -> ShowS
Show)

-- | 'ConsoleLog' with possible region.
data LogRegion r
  = -- | Log with region.
    LogRegion LogMode r ConsoleLog
  | -- | Log without region.
    LogNoRegion ConsoleLog

-- | Captures the relevant information concerning a specific log
-- (i.e. command, text, level, and mode).
data Log = MkLog
  { -- | Optional command that produced this log.
    Log -> Maybe CommandP1
cmd :: Maybe CommandP1,
    -- | The 'Text' for a given log.
    Log -> UnlinedText
msg :: UnlinedText,
    -- | The 'LogLevel' for a given log.
    Log -> LogLevel
lvl :: LogLevel,
    -- | The 'LogMode' for a given log.
    Log -> LogMode
mode :: LogMode
  }
  deriving stock (Int -> Log -> ShowS
[Log] -> ShowS
Log -> [Char]
(Int -> Log -> ShowS)
-> (Log -> [Char]) -> ([Log] -> ShowS) -> Show Log
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Log -> ShowS
showsPrec :: Int -> Log -> ShowS
$cshow :: Log -> [Char]
show :: Log -> [Char]
$cshowList :: [Log] -> ShowS
showList :: [Log] -> ShowS
Show)

instance
  ( k ~ A_Lens,
    a ~ Maybe CommandP1,
    b ~ Maybe CommandP1
  ) =>
  LabelOptic "cmd" k Log Log a b
  where
  labelOptic :: Optic k NoIx Log Log a b
labelOptic =
    LensVL Log Log a b -> Lens Log Log a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL Log Log a b -> Lens Log Log a b)
-> LensVL Log Log a b -> Lens Log Log a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f
         (MkLog Maybe CommandP1
_cmd UnlinedText
_msg LogLevel
_lvl LogMode
_mode) ->
          (Maybe CommandP1 -> Log) -> f (Maybe CommandP1) -> f Log
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
            (\Maybe CommandP1
cmd' -> Maybe CommandP1 -> UnlinedText -> LogLevel -> LogMode -> Log
MkLog Maybe CommandP1
cmd' UnlinedText
_msg LogLevel
_lvl LogMode
_mode)
            (a -> f b
f a
Maybe CommandP1
_cmd)
  {-# INLINE labelOptic #-}

instance
  ( k ~ A_Lens,
    a ~ UnlinedText,
    b ~ UnlinedText
  ) =>
  LabelOptic "msg" k Log Log a b
  where
  labelOptic :: Optic k NoIx Log Log a b
labelOptic =
    LensVL Log Log a b -> Lens Log Log a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL Log Log a b -> Lens Log Log a b)
-> LensVL Log Log a b -> Lens Log Log a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f
         (MkLog Maybe CommandP1
_cmd UnlinedText
_msg LogLevel
_lvl LogMode
_mode) ->
          (UnlinedText -> Log) -> f UnlinedText -> f Log
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
            (\UnlinedText
msg' -> Maybe CommandP1 -> UnlinedText -> LogLevel -> LogMode -> Log
MkLog Maybe CommandP1
_cmd UnlinedText
msg' LogLevel
_lvl LogMode
_mode)
            (a -> f b
f a
UnlinedText
_msg)
  {-# INLINE labelOptic #-}

instance
  ( k ~ A_Lens,
    a ~ LogLevel,
    b ~ LogLevel
  ) =>
  LabelOptic "lvl" k Log Log a b
  where
  labelOptic :: Optic k NoIx Log Log a b
labelOptic =
    LensVL Log Log a b -> Lens Log Log a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL Log Log a b -> Lens Log Log a b)
-> LensVL Log Log a b -> Lens Log Log a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f
         (MkLog Maybe CommandP1
_cmd UnlinedText
_msg LogLevel
_lvl LogMode
_mode) ->
          (LogLevel -> Log) -> f LogLevel -> f Log
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
            (\LogLevel
lvl' -> Maybe CommandP1 -> UnlinedText -> LogLevel -> LogMode -> Log
MkLog Maybe CommandP1
_cmd UnlinedText
_msg LogLevel
lvl' LogMode
_mode)
            (a -> f b
f a
LogLevel
_lvl)
  {-# INLINE labelOptic #-}

instance
  ( k ~ A_Lens,
    a ~ LogMode,
    b ~ LogMode
  ) =>
  LabelOptic "mode" k Log Log a b
  where
  labelOptic :: Optic k NoIx Log Log a b
labelOptic =
    LensVL Log Log a b -> Lens Log Log a b
forall s t a b. LensVL s t a b -> Lens s t a b
lensVL
      (LensVL Log Log a b -> Lens Log Log a b)
-> LensVL Log Log a b -> Lens Log Log a b
forall a b. (a -> b) -> a -> b
$ \a -> f b
f
         (MkLog Maybe CommandP1
_cmd UnlinedText
_msg LogLevel
_lvl LogMode
_mode) ->
          (LogMode -> Log) -> f LogMode -> f Log
forall a b. (a -> b) -> f a -> f b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
            (Maybe CommandP1 -> UnlinedText -> LogLevel -> LogMode -> Log
MkLog Maybe CommandP1
_cmd UnlinedText
_msg LogLevel
_lvl)
            (a -> f b
f a
LogMode
_mode)
  {-# INLINE labelOptic #-}