ghc-mod/Language/Haskell/GhcMod/DebugLogger.hs

91 lines
3.0 KiB
Haskell
Raw Normal View History

2015-09-11 02:13:44 +00:00
{-# LANGUAGE CPP #-}
2015-09-08 04:19:58 +00:00
module Language.Haskell.GhcMod.DebugLogger where
import GHC
import FastString
import Pretty
import Outputable (SDoc, PprStyle, runSDoc, initSDocContext, blankLine)
import qualified Outputable
import ErrUtils
import Language.Haskell.GhcMod.Error
2015-09-11 02:13:44 +00:00
import Language.Haskell.GhcMod.Gap
2015-09-08 04:19:58 +00:00
import Prelude
2015-09-11 02:13:44 +00:00
debugLogAction :: (String -> IO ()) -> GmLogAction
2015-09-08 04:19:58 +00:00
debugLogAction putErr dflags severity srcSpan style msg
= case severity of
2015-09-11 02:13:44 +00:00
SevOutput -> printSDoc putErr msg style
#if __GLASGOW_HASKELL__ >= 706
SevDump -> printSDoc putErr (msg Outputable.$$ blankLine) style
#endif
#if __GLASGOW_HASKELL__ >= 708
SevInteractive -> let
putStrSDoc = debugLogActionHPutStrDoc dflags putErr
in
putStrSDoc msg style
#endif
SevInfo -> printErrs putErr msg style
SevFatal -> printErrs putErr msg style
2015-09-08 04:19:58 +00:00
_ -> do putErr "\n"
2015-09-11 02:13:44 +00:00
#if __GLASGOW_HASKELL__ >= 706
printErrs putErr (mkLocMessage severity srcSpan msg) style
#else
printErrs putErr (mkLocMessage srcSpan msg) style
#endif
2015-09-08 04:19:58 +00:00
-- careful (#2302): printErrs prints in UTF-8,
-- whereas converting to string first and using
-- hPutStr would just emit the low 8 bits of
-- each unicode char.
2015-09-11 02:13:44 +00:00
where
#if __GLASGOW_HASKELL__ >= 706
printSDoc put = debugLogActionHPrintDoc dflags put
printErrs put = debugLogActionHPrintDoc dflags put
#endif
#if __GLASGOW_HASKELL__ >= 706
2015-09-08 04:19:58 +00:00
debugLogActionHPrintDoc :: DynFlags -> (String -> IO ()) -> SDoc -> PprStyle -> IO ()
debugLogActionHPrintDoc dflags put d sty
= debugLogActionHPutStrDoc dflags put (d Outputable.$$ Outputable.text "") sty
-- Adds a newline
debugLogActionHPutStrDoc :: DynFlags -> (String -> IO ()) -> SDoc -> PprStyle -> IO ()
debugLogActionHPutStrDoc dflags put d sty
= gmPrintDoc_ Pretty.PageMode (pprCols dflags) put doc
where -- Don't add a newline at the end, so that successive
-- calls to this log-action can output all on the same line
doc = runSDoc d (initSDocContext dflags sty)
2015-09-11 02:13:44 +00:00
#else
printSDoc = printErrs
printErrs :: (String -> IO ()) -> SDoc -> PprStyle -> IO ()
printErrs put doc sty = do
gmPrintDoc PageMode 100 put (runSDoc doc (initSDocContext sty))
#endif
2015-09-08 04:19:58 +00:00
gmPrintDoc :: Mode -> Int -> (String -> IO ()) -> Doc -> IO ()
-- printDoc adds a newline to the end
gmPrintDoc mode cols put doc = gmPrintDoc_ mode cols put (doc $$ text "")
gmPrintDoc_ :: Mode -> Int -> (String -> IO ()) -> Doc -> IO ()
gmPrintDoc_ mode pprCols putS doc
= fullRender mode pprCols 1.5 put done doc
where
put (Chr c) next = putS [c] >> next
put (Str s) next = putS s >> next
put (PStr s) next = putS (unpackFS s) >> next
2015-09-11 02:13:44 +00:00
#if __GLASGOW_HASKELL__ >= 708
2015-09-08 04:19:58 +00:00
put (ZStr s) next = putS (zString s) >> next
2015-09-11 02:13:44 +00:00
#endif
2015-09-08 04:19:58 +00:00
put (LStr s _l) next = putS (unpackLitString s) >> next
done = return () -- hPutChar hdl '\n'