{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE DataKinds        #-}
{-# LANGUAGE OverloadedStrings   #-}

{-|
Module      : GHCup.Utils.Logger
Description : logger definition
Copyright   : (c) Julian Ospald, 2020
License     : LGPL-3.0
Maintainer  : hasufell@hasufell.de
Stability   : experimental
Portability : portable

Here we define our main logger.
-}
module GHCup.Prelude.Logger
  ( module GHCup.Prelude.Logger
  , module GHCup.Prelude.Logger.Internal
  )
where

import           GHCup.Prelude.Logger.Internal
import           GHCup.Types
import           GHCup.Types.Optics
import           GHCup.Utils.Dirs (fromGHCupPath)
import           GHCup.Prelude.Internal
import           GHCup.Prelude.File.Search (findFiles)
import           GHCup.Prelude.File (recycleFile)
import           GHCup.Prelude.String.QQ

import           Control.Exception.Safe
import           Control.Monad
import           Control.Monad.IO.Class
import           Control.Monad.Reader
import           Prelude                 hiding ( appendFile )
import           System.FilePath
import           System.IO.Error
import           Text.Regex.Posix

import qualified Data.ByteString               as B



initGHCupFileLogging :: ( MonadReader env m
                        , HasDirs env
                        , MonadIO m
                        , MonadMask m
                        ) => m FilePath
initGHCupFileLogging :: forall env (m :: * -> *).
(MonadReader env m, HasDirs env, MonadIO m, MonadMask m) =>
m FilePath
initGHCupFileLogging = do
  Dirs { GHCupPath
$sel:logsDir:Dirs :: Dirs -> GHCupPath
logsDir :: GHCupPath
logsDir } <- forall env (m :: * -> *).
(MonadReader env m, LabelOptic' "dirs" A_Lens env Dirs) =>
m Dirs
getDirs
  let logfile :: FilePath
logfile = GHCupPath -> FilePath
fromGHCupPath GHCupPath
logsDir FilePath -> FilePath -> FilePath
</> FilePath
"ghcup.log"
  [FilePath]
logFiles <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> Regex -> IO [FilePath]
findFiles
    (GHCupPath -> FilePath
fromGHCupPath GHCupPath
logsDir)
    (forall regex compOpt execOpt source.
RegexMaker regex compOpt execOpt source =>
compOpt -> execOpt -> source -> regex
makeRegexOpts CompOption
compExtended
                   ExecOption
execBlank
                   ([s|^.*\.log$|] :: B.ByteString)
    )
  forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [FilePath]
logFiles forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *).
(MonadIO m, MonadCatch m) =>
IOErrorType -> m () -> m ()
hideError IOErrorType
doesNotExistErrorType forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) env.
(MonadIO m, MonadMask m, MonadReader env m, HasDirs env) =>
FilePath -> m ()
recycleFile forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GHCupPath -> FilePath
fromGHCupPath GHCupPath
logsDir FilePath -> FilePath -> FilePath
</>)

  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ FilePath -> FilePath -> IO ()
writeFile FilePath
logfile FilePath
""
  forall (f :: * -> *) a. Applicative f => a -> f a
pure FilePath
logfile