ghc-mod/core/GhcMod/DebugLogger.hs

139 lines
5.4 KiB
Haskell
Raw Normal View History

2017-03-06 23:19:57 +00:00
-- ghc-mod: Happy Haskell Hacking
2015-11-26 14:03:14 +00:00
-- Copyright (C) 2015 Daniel Gröber <dxld ÄT darkboxed DOT org>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
{-# LANGUAGE CPP, RankNTypes #-}
module GhcMod.DebugLogger where
2015-09-08 04:19:58 +00:00
2015-11-26 14:03:14 +00:00
-- (c) The University of Glasgow 2005
--
-- The Glasgow Haskell Compiler License
--
-- Copyright 2002, The University Court of the University of Glasgow.
-- All rights reserved.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- - Redistributions of source code must retain the above copyright notice,
-- this list of conditions and the following disclaimer.
--
-- - Redistributions in binary form must reproduce the above copyright notice,
-- this list of conditions and the following disclaimer in the documentation
-- and/or other materials provided with the distribution.
--
-- - Neither name of the University nor the names of its contributors may be
-- used to endorse or promote products derived from this software without
-- specific prior written permission.
--
-- THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY COURT OF THE UNIVERSITY OF
-- GLASGOW AND THE CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
-- INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
-- FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
-- UNIVERSITY COURT OF THE UNIVERSITY OF GLASGOW OR THE CONTRIBUTORS BE LIABLE
-- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
-- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
-- CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
-- DAMAGE.
2015-09-08 04:19:58 +00:00
import GHC
import FastString
import Pretty
import Outputable (SDoc, PprStyle, runSDoc, initSDocContext, blankLine)
import qualified Outputable
import ErrUtils
import GhcMod.Error
import GhcMod.Gap
2015-09-08 04:19:58 +00:00
import Prelude
2015-09-11 02:13:44 +00:00
debugLogAction :: (String -> IO ()) -> GmLogAction
debugLogAction putErr _reason dflags severity srcSpan style' msg
2015-09-08 04:19:58 +00:00
= case severity of
2016-02-14 11:35:57 +00:00
SevOutput -> printSDoc putErr msg style'
2015-09-11 02:13:44 +00:00
#if __GLASGOW_HASKELL__ >= 706
2016-02-14 11:35:57 +00:00
SevDump -> printSDoc putErr (msg Outputable.$$ blankLine) style'
2015-09-11 02:13:44 +00:00
#endif
#if __GLASGOW_HASKELL__ >= 708
SevInteractive -> let
putStrSDoc = debugLogActionHPutStrDoc dflags putErr
in
2016-02-14 11:35:57 +00:00
putStrSDoc msg style'
2015-09-11 02:13:44 +00:00
#endif
2016-02-14 11:35:57 +00:00
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
2016-02-14 11:35:57 +00:00
printErrs putErr (mkLocMessage severity srcSpan msg) style'
2015-09-11 02:13:44 +00:00
#else
2016-02-14 11:35:57 +00:00
printErrs putErr (mkLocMessage srcSpan msg) style'
2015-09-11 02:13:44 +00:00
#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'