2021-04-29 12:47:30 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-07-21 23:08:58 +00:00
|
|
|
{-|
|
|
|
|
Module : GHCup.Utils.Logger
|
|
|
|
Description : logger definition
|
|
|
|
Copyright : (c) Julian Ospald, 2020
|
2020-07-30 18:04:02 +00:00
|
|
|
License : LGPL-3.0
|
2020-07-21 23:08:58 +00:00
|
|
|
Maintainer : hasufell@hasufell.de
|
|
|
|
Stability : experimental
|
2021-05-14 21:09:45 +00:00
|
|
|
Portability : portable
|
2020-07-21 23:08:58 +00:00
|
|
|
|
|
|
|
Here we define our main logger.
|
|
|
|
-}
|
2020-01-11 20:15:05 +00:00
|
|
|
module GHCup.Utils.Logger where
|
|
|
|
|
2021-07-21 13:43:45 +00:00
|
|
|
import GHCup.Types
|
|
|
|
import GHCup.Types.Optics
|
2021-04-29 12:47:30 +00:00
|
|
|
import GHCup.Utils.File
|
|
|
|
import GHCup.Utils.String.QQ
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2021-07-21 13:43:45 +00:00
|
|
|
import Control.Exception.Safe
|
2020-01-11 20:15:05 +00:00
|
|
|
import Control.Monad
|
2020-08-05 19:50:39 +00:00
|
|
|
import Control.Monad.IO.Class
|
2021-07-21 13:43:45 +00:00
|
|
|
import Control.Monad.Reader
|
2020-01-11 20:15:05 +00:00
|
|
|
import Prelude hiding ( appendFile )
|
2021-05-14 21:09:45 +00:00
|
|
|
import System.FilePath
|
2020-01-11 20:15:05 +00:00
|
|
|
import System.IO.Error
|
2021-04-29 12:47:30 +00:00
|
|
|
import Text.Regex.Posix
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
import qualified Data.ByteString as B
|
2021-05-14 21:09:45 +00:00
|
|
|
import GHCup.Utils.Prelude
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2021-07-21 13:43:45 +00:00
|
|
|
initGHCupFileLogging :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, MonadIO m
|
|
|
|
, MonadMask m
|
|
|
|
) => m FilePath
|
|
|
|
initGHCupFileLogging = do
|
|
|
|
Dirs { logsDir } <- getDirs
|
2021-05-14 21:09:45 +00:00
|
|
|
let logfile = logsDir </> "ghcup.log"
|
2021-07-21 13:43:45 +00:00
|
|
|
logFiles <- liftIO $ findFiles
|
|
|
|
logsDir
|
|
|
|
(makeRegexOpts compExtended
|
|
|
|
execBlank
|
|
|
|
([s|^.*\.log$|] :: B.ByteString)
|
|
|
|
)
|
2021-07-22 13:45:08 +00:00
|
|
|
forM_ logFiles $ hideError doesNotExistErrorType . recycleFile . (logsDir </>)
|
2021-04-29 12:47:30 +00:00
|
|
|
|
2021-07-21 13:43:45 +00:00
|
|
|
liftIO $ writeFile logfile ""
|
|
|
|
pure logfile
|