module GHC.Utils.Logger
( Logger
, initLogger
, HasLogger (..)
, ContainsLogger (..)
, LogAction
, DumpAction
, TraceAction
, DumpFormat (..)
, putLogMsg
, putDumpMsg
, putTraceMsg
, popLogHook
, pushLogHook
, popDumpHook
, pushDumpHook
, popTraceHook
, pushTraceHook
, makeThreadSafe
, jsonLogAction
, defaultLogAction
, defaultLogActionHPrintDoc
, defaultLogActionHPutStrDoc
, defaultDumpAction
, withDumpFileHandle
, touchDumpFile
, dumpIfSet
, dumpIfSet_dyn
, dumpIfSet_dyn_printer
, defaultTraceAction
)
where
import GHC.Prelude
import GHC.Driver.Session
import GHC.Driver.Ppr
import GHC.Types.Error
import GHC.Types.SrcLoc
import qualified GHC.Utils.Ppr as Pretty
import GHC.Utils.Outputable
import GHC.Utils.Json
import GHC.Utils.Panic
import Data.IORef
import System.Directory
import System.FilePath ( takeDirectory, (</>) )
import qualified Data.Set as Set
import Data.Set (Set)
import Data.List (intercalate, stripPrefix)
import Data.Time
import System.IO
import Control.Monad
import Control.Concurrent.MVar
import System.IO.Unsafe
type LogAction = DynFlags
-> WarnReason
-> Severity
-> SrcSpan
-> SDoc
-> IO ()
type DumpAction = DynFlags
-> PprStyle
-> DumpFlag
-> String
-> DumpFormat
-> SDoc
-> IO ()
type TraceAction a = DynFlags -> String -> SDoc -> a -> a
data DumpFormat
= FormatHaskell
| FormatCore
| FormatSTG
| FormatByteCode
| FormatCMM
| FormatASM
| FormatC
| FormatLLVM
| FormatText
deriving (Show,Eq)
type DumpCache = IORef (Set FilePath)
data Logger = Logger
{ log_hook :: [LogAction -> LogAction]
, dump_hook :: [DumpAction -> DumpAction]
, trace_hook :: forall a. [TraceAction a -> TraceAction a]
, generated_dumps :: DumpCache
}
initLogger :: IO Logger
initLogger = do
dumps <- newIORef Set.empty
return $ Logger
{ log_hook = []
, dump_hook = []
, trace_hook = []
, generated_dumps = dumps
}
putLogMsg :: Logger -> LogAction
putLogMsg logger = foldr ($) defaultLogAction (log_hook logger)
putDumpMsg :: Logger -> DumpAction
putDumpMsg logger =
let
fallback = putLogMsg logger
dumps = generated_dumps logger
deflt = defaultDumpAction dumps fallback
in foldr ($) deflt (dump_hook logger)
putTraceMsg :: Logger -> TraceAction a
putTraceMsg logger = foldr ($) defaultTraceAction (trace_hook logger)
pushLogHook :: (LogAction -> LogAction) -> Logger -> Logger
pushLogHook h logger = logger { log_hook = h:log_hook logger }
popLogHook :: Logger -> Logger
popLogHook logger = case log_hook logger of
[] -> panic "popLogHook: empty hook stack"
_:hs -> logger { log_hook = hs }
pushDumpHook :: (DumpAction -> DumpAction) -> Logger -> Logger
pushDumpHook h logger = logger { dump_hook = h:dump_hook logger }
popDumpHook :: Logger -> Logger
popDumpHook logger = case dump_hook logger of
[] -> panic "popDumpHook: empty hook stack"
_:hs -> logger { dump_hook = hs }
pushTraceHook :: (forall a. TraceAction a -> TraceAction a) -> Logger -> Logger
pushTraceHook h logger = logger { trace_hook = h:trace_hook logger }
popTraceHook :: Logger -> Logger
popTraceHook logger = case trace_hook logger of
[] -> panic "popTraceHook: empty hook stack"
_ -> logger { trace_hook = tail (trace_hook logger) }
makeThreadSafe :: Logger -> IO Logger
makeThreadSafe logger = do
lock <- newMVar ()
let
with_lock :: forall a. IO a -> IO a
with_lock act = withMVar lock (const act)
log action dflags reason sev loc doc =
with_lock (action dflags reason sev loc doc)
dmp action dflags sty opts str fmt doc =
with_lock (action dflags sty opts str fmt doc)
trc :: forall a. TraceAction a -> TraceAction a
trc action dflags str doc v =
unsafePerformIO (with_lock (return $! action dflags str doc v))
return $ pushLogHook log
$ pushDumpHook dmp
$ pushTraceHook trc
$ logger
jsonLogAction :: LogAction
jsonLogAction dflags reason severity srcSpan msg
=
defaultLogActionHPutStrDoc dflags True stdout
(withPprStyle (PprCode CStyle) (doc $$ text ""))
where
str = renderWithContext (initSDocContext dflags defaultUserStyle) msg
doc = renderJSON $
JSObject [ ( "span", json srcSpan )
, ( "doc" , JSString str )
, ( "severity", json severity )
, ( "reason" , json reason )
]
defaultLogAction :: LogAction
defaultLogAction dflags reason severity srcSpan msg
| dopt Opt_D_dump_json dflags = jsonLogAction dflags reason severity srcSpan msg
| otherwise = case severity of
SevOutput -> printOut msg
SevDump -> printOut (msg $$ blankLine)
SevInteractive -> putStrSDoc msg
SevInfo -> printErrs msg
SevFatal -> printErrs msg
SevWarning -> printWarns
SevError -> printWarns
where
printOut = defaultLogActionHPrintDoc dflags False stdout
printErrs = defaultLogActionHPrintDoc dflags False stderr
putStrSDoc = defaultLogActionHPutStrDoc dflags False stdout
message = mkLocMessageAnn flagMsg severity srcSpan msg
printWarns = do
hPutChar stderr '\n'
caretDiagnostic <-
if gopt Opt_DiagnosticsShowCaret dflags
then getCaretDiagnostic severity srcSpan
else pure empty
printErrs $ getPprStyle $ \style ->
withPprStyle (setStyleColoured True style)
(message $+$ caretDiagnostic)
flagMsg =
case reason of
NoReason -> Nothing
Reason wflag -> do
spec <- flagSpecOf wflag
return ("-W" ++ flagSpecName spec ++ warnFlagGrp wflag)
ErrReason Nothing ->
return "-Werror"
ErrReason (Just wflag) -> do
spec <- flagSpecOf wflag
return $
"-W" ++ flagSpecName spec ++ warnFlagGrp wflag ++
", -Werror=" ++ flagSpecName spec
warnFlagGrp flag
| gopt Opt_ShowWarnGroups dflags =
case smallestGroups flag of
[] -> ""
groups -> " (in " ++ intercalate ", " (map ("-W"++) groups) ++ ")"
| otherwise = ""
defaultLogActionHPrintDoc :: DynFlags -> Bool -> Handle -> SDoc -> IO ()
defaultLogActionHPrintDoc dflags asciiSpace h d
= defaultLogActionHPutStrDoc dflags asciiSpace h (d $$ text "")
defaultLogActionHPutStrDoc :: DynFlags -> Bool -> Handle -> SDoc -> IO ()
defaultLogActionHPutStrDoc dflags asciiSpace h d
= printSDoc ctx (Pretty.PageMode asciiSpace) h d
where
ctx = initSDocContext dflags defaultUserStyle
defaultDumpAction :: DumpCache -> LogAction -> DumpAction
defaultDumpAction dumps log_action dflags sty flag title _fmt doc =
dumpSDocWithStyle dumps log_action sty dflags flag title doc
dumpSDocWithStyle :: DumpCache -> LogAction -> PprStyle -> DynFlags -> DumpFlag -> String -> SDoc -> IO ()
dumpSDocWithStyle dumps log_action sty dflags flag hdr doc =
withDumpFileHandle dumps dflags flag writeDump
where
writeDump (Just handle) = do
doc' <- if null hdr
then return doc
else do t <- getCurrentTime
let timeStamp = if (gopt Opt_SuppressTimestamps dflags)
then empty
else text (show t)
let d = timeStamp
$$ blankLine
$$ doc
return $ mkDumpDoc hdr d
defaultLogActionHPrintDoc dflags True handle (withPprStyle sty doc')
writeDump Nothing = do
let (doc', severity)
| null hdr = (doc, SevOutput)
| otherwise = (mkDumpDoc hdr doc, SevDump)
log_action dflags NoReason severity noSrcSpan (withPprStyle sty doc')
withDumpFileHandle :: DumpCache -> DynFlags -> DumpFlag -> (Maybe Handle -> IO ()) -> IO ()
withDumpFileHandle dumps dflags flag action = do
let mFile = chooseDumpFile dflags flag
case mFile of
Just fileName -> do
gd <- readIORef dumps
let append = Set.member fileName gd
mode = if append then AppendMode else WriteMode
unless append $
writeIORef dumps (Set.insert fileName gd)
createDirectoryIfMissing True (takeDirectory fileName)
withFile fileName mode $ \handle -> do
hSetEncoding handle utf8
action (Just handle)
Nothing -> action Nothing
chooseDumpFile :: DynFlags -> DumpFlag -> Maybe FilePath
chooseDumpFile dflags flag
| gopt Opt_DumpToFile dflags || forced_to_file
, Just prefix <- getPrefix
= Just $ setDir (prefix ++ dump_suffix)
| otherwise
= Nothing
where
(forced_to_file, dump_suffix) = case flag of
Opt_D_th_dec_file -> (True, "th.hs")
_ -> (False, default_suffix)
default_suffix = map (\c -> if c == '_' then '-' else c) $
let str = show flag
in case stripPrefix "Opt_D_" str of
Just x -> x
Nothing -> panic ("chooseDumpFile: bad flag name: " ++ str)
getPrefix
| Just prefix <- dumpPrefixForce dflags
= Just prefix
| Just prefix <- dumpPrefix dflags
= Just prefix
| otherwise
= Nothing
setDir f = case dumpDir dflags of
Just d -> d </> f
Nothing -> f
doDump :: Logger -> DynFlags -> String -> SDoc -> IO ()
doDump logger dflags hdr doc =
putLogMsg logger dflags
NoReason
SevDump
noSrcSpan
(withPprStyle defaultDumpStyle
(mkDumpDoc hdr doc))
mkDumpDoc :: String -> SDoc -> SDoc
mkDumpDoc hdr doc
= vcat [blankLine,
line <+> text hdr <+> line,
doc,
blankLine]
where
line = text "===================="
dumpIfSet :: Logger -> DynFlags -> Bool -> String -> SDoc -> IO ()
dumpIfSet logger dflags flag hdr doc
| not flag = return ()
| otherwise = doDump logger dflags hdr doc
dumpIfSet_dyn :: Logger -> DynFlags -> DumpFlag -> String -> DumpFormat -> SDoc -> IO ()
dumpIfSet_dyn = dumpIfSet_dyn_printer alwaysQualify
dumpIfSet_dyn_printer
:: PrintUnqualified
-> Logger
-> DynFlags
-> DumpFlag
-> String
-> DumpFormat
-> SDoc
-> IO ()
dumpIfSet_dyn_printer printer logger dflags flag hdr fmt doc
= when (dopt flag dflags) $ do
let sty = mkDumpStyle printer
putDumpMsg logger dflags sty flag hdr fmt doc
touchDumpFile :: Logger -> DynFlags -> DumpFlag -> IO ()
touchDumpFile logger dflags flag =
withDumpFileHandle (generated_dumps logger) dflags flag (const (return ()))
defaultTraceAction :: TraceAction a
defaultTraceAction dflags title doc = pprTraceWithFlags dflags title doc
class HasLogger m where
getLogger :: m Logger
class ContainsLogger t where
extractLogger :: t -> Logger