ghcup-hs/lib/GHCup/Utils/Logger.hs

61 lines
2.1 KiB
Haskell
Raw Normal View History

2020-03-05 17:02:59 +00:00
{-# LANGUAGE QuasiQuotes #-}
2020-03-03 00:59:19 +00:00
module GHCup.Utils.Logger where
2020-02-28 23:33:32 +00:00
2020-03-05 17:02:59 +00:00
import GHCup.Utils
2020-02-28 23:33:32 +00:00
2020-03-08 17:30:08 +00:00
import Control.Monad
2020-02-28 23:33:32 +00:00
import Control.Monad.Logger
2020-03-05 17:02:59 +00:00
import HPath
import HPath.IO
import Prelude hiding ( appendFile )
2020-03-01 00:05:02 +00:00
import System.Console.Pretty
2020-03-05 17:02:59 +00:00
import System.IO.Error
2020-02-28 23:33:32 +00:00
2020-03-05 17:02:59 +00:00
import qualified Data.ByteString as B
2020-02-28 23:33:32 +00:00
2020-03-05 17:02:59 +00:00
data LoggerConfig = LoggerConfig
{ lcPrintDebug :: Bool -- ^ whether to print debug in colorOutter
, colorOutter :: B.ByteString -> IO () -- ^ how to write the color output
, rawOutter :: B.ByteString -> IO () -- ^ how to write the full raw output
}
2020-03-01 00:05:02 +00:00
myLoggerT :: LoggerConfig -> LoggingT m a -> m a
2020-03-05 17:02:59 +00:00
myLoggerT LoggerConfig {..} loggingt = runLoggingT loggingt mylogger
2020-02-28 23:33:32 +00:00
where
mylogger :: Loc -> LogSource -> LogLevel -> LogStr -> IO ()
2020-02-29 23:07:39 +00:00
mylogger _ _ level str' = do
2020-03-05 17:02:59 +00:00
-- color output
2020-02-28 23:33:32 +00:00
let l = case level of
2020-03-08 17:30:08 +00:00
LevelDebug -> toLogStr (style Bold $ color Blue "[ Debug ]")
2020-03-05 17:02:59 +00:00
LevelInfo -> toLogStr (style Bold $ color Green "[ Info ]")
2020-02-28 23:33:32 +00:00
LevelWarn -> toLogStr (style Bold $ color Yellow "[ Warn ]")
2020-03-05 17:02:59 +00:00
LevelError -> toLogStr (style Bold $ color Red "[ Error ]")
2020-02-28 23:33:32 +00:00
LevelOther t -> toLogStr "[ " <> toLogStr t <> toLogStr " ]"
2020-03-05 17:02:59 +00:00
let out = fromLogStr (l <> toLogStr " " <> str' <> toLogStr "\n")
2020-03-08 17:30:08 +00:00
when (lcPrintDebug || (lcPrintDebug == False && not (level == LevelDebug)))
$ colorOutter out
2020-03-05 17:02:59 +00:00
-- raw output
let lr = case level of
LevelDebug -> toLogStr "Debug: "
LevelInfo -> toLogStr "Info:"
LevelWarn -> toLogStr "Warn:"
LevelError -> toLogStr "Error:"
LevelOther t -> toLogStr t <> toLogStr ":"
let outr = fromLogStr (lr <> toLogStr " " <> str' <> toLogStr "\n")
rawOutter outr
initGHCupFileLogging :: Path Rel -> IO (Path Abs)
initGHCupFileLogging context = do
logs <- ghcupLogsDir
let logfile = logs </> context
createDirIfMissing newDirPerms logs
hideError doesNotExistErrorType $ deleteFile logfile
createRegularFile newFilePerms logfile
pure logfile