Skip to content

Commit cc33112

Browse files
committed
Change the default logger to print to stderr
1 parent 53d2e50 commit cc33112

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

ghcide/exe/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ main = do
8484
Typecheck x | not argLSP -> Just x
8585
_ -> Nothing
8686

87-
,Main.argsLogger = logger
87+
,Main.argsLogger = pure logger
8888

8989
,Main.argsRules = do
9090
-- install the main and ghcide-plugin rules

ghcide/src/Development/IDE/Main.hs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module Development.IDE.Main (Arguments(..), defaultMain) where
2-
import Control.Concurrent.Extra (readVar)
2+
import Control.Concurrent.Extra (newLock, readVar, withLock)
33
import Control.Exception.Safe (
44
Exception (displayException),
55
catchAny,
@@ -16,7 +16,8 @@ import Data.List.Extra (
1616
)
1717
import Data.Maybe (catMaybes, fromMaybe, isJust)
1818
import qualified Data.Text as T
19-
import Development.IDE (Action, Rules, noLogging)
19+
import qualified Data.Text.IO as T
20+
import Development.IDE (Action, Rules)
2021
import Development.IDE.Core.Debouncer (newAsyncDebouncer)
2122
import Development.IDE.Core.FileStore (makeVFSHandle)
2223
import Development.IDE.Core.OfInterest (
@@ -49,7 +50,6 @@ import Development.IDE.Plugin (
4950
import Development.IDE.Plugin.HLS (asGhcIdePlugin)
5051
import Development.IDE.Session (SessionLoadingOptions, loadSessionWithOptions, setInitialDynFlags, getHieDbLoc, runWithDb)
5152
import Development.IDE.Types.Location (toNormalizedFilePath')
52-
import Development.IDE.Types.Logger (Logger)
5353
import Development.IDE.Types.Options (
5454
IdeGhcSession,
5555
IdeOptions (optCheckParents, optCheckProject, optReportProgress),
@@ -66,15 +66,16 @@ import qualified Language.LSP.Server as LSP
6666
import qualified System.Directory.Extra as IO
6767
import System.Exit (ExitCode (ExitFailure), exitWith)
6868
import System.FilePath (takeExtension, takeFileName)
69-
import System.IO (hPutStrLn, hSetEncoding, stderr, stdout, utf8)
69+
import System.IO (hSetBuffering, hPutStrLn, hSetEncoding, stderr, stdout, utf8, BufferMode (LineBuffering))
7070
import System.Time.Extra (offsetTime, showDuration)
7171
import Text.Printf (printf)
7272
import qualified Development.IDE.Plugin.HLS.GhcIde as Ghcide
73+
import Development.IDE.Types.Logger (Logger(Logger))
7374

7475
data Arguments = Arguments
7576
{ argsOTMemoryProfiling :: Bool
7677
, argFiles :: Maybe [FilePath] -- ^ Nothing: lsp server ; Just: typecheck and exit
77-
, argsLogger :: Logger
78+
, argsLogger :: IO Logger
7879
, argsRules :: Rules ()
7980
, argsHlsPlugins :: IdePlugins IdeState
8081
, argsGhcidePlugin :: Plugin Config -- ^ Deprecated
@@ -89,7 +90,7 @@ instance Default Arguments where
8990
def = Arguments
9091
{ argsOTMemoryProfiling = False
9192
, argFiles = Nothing
92-
, argsLogger = noLogging
93+
, argsLogger = stderrLogger
9394
, argsRules = mainRule >> action kick
9495
, argsGhcidePlugin = mempty
9596
, argsHlsPlugins = pluginDescToIdePlugins Ghcide.descriptors
@@ -100,9 +101,18 @@ instance Default Arguments where
100101
, argsGetHieDbLoc = getHieDbLoc
101102
}
102103

104+
-- | Cheap stderr logger that relies on LineBuffering
105+
stderrLogger :: IO Logger
106+
stderrLogger = do
107+
lock <- newLock
108+
return $ Logger $ \p m -> withLock lock $
109+
T.hPutStrLn stderr $ "[" <> T.pack (show p) <> "] " <> m
110+
103111
defaultMain :: Arguments -> IO ()
104112
defaultMain Arguments{..} = do
105113
pid <- T.pack . show <$> getProcessID
114+
logger <- argsLogger
115+
hSetBuffering stderr LineBuffering
106116

107117
let hlsPlugin = asGhcIdePlugin argsDefaultHlsConfig argsHlsPlugins
108118
hlsCommands = allLspCmdIds' pid argsHlsPlugins
@@ -141,7 +151,7 @@ defaultMain Arguments{..} = do
141151
argsDefaultHlsConfig
142152
rules
143153
(Just env)
144-
argsLogger
154+
logger
145155
debouncer
146156
options
147157
vfs
@@ -178,7 +188,7 @@ defaultMain Arguments{..} = do
178188
{ optCheckParents = pure NeverCheck
179189
, optCheckProject = pure False
180190
}
181-
ide <- initialise argsDefaultHlsConfig rules Nothing argsLogger debouncer options vfs hiedb hieChan
191+
ide <- initialise argsDefaultHlsConfig rules Nothing logger debouncer options vfs hiedb hieChan
182192

183193
putStrLn "\nStep 4/4: Type checking the files"
184194
setFilesOfInterest ide $ HashMap.fromList $ map ((,OnDisk) . toNormalizedFilePath') files
@@ -205,7 +215,7 @@ defaultMain Arguments{..} = do
205215
Key GhcSessionDeps :
206216
[k | (_, k) <- HashMap.keys values, k /= Key GhcSessionIO]
207217
++ [Key GhcSessionIO]
208-
measureMemory argsLogger [keys] consoleObserver valuesRef
218+
measureMemory logger [keys] consoleObserver valuesRef
209219

210220
unless (null failed) (exitWith $ ExitFailure (length failed))
211221
{-# ANN defaultMain ("HLint: ignore Use nubOrd" :: String) #-}

src/Ide/Main.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ runLspMode lspArgs@LspArguments{..} idePlugins = do
9494
Main.defaultMain def
9595
{ Main.argFiles = if argLSP then Nothing else Just []
9696
, Main.argsHlsPlugins = idePlugins
97-
, Main.argsLogger = hlsLogger
97+
, Main.argsLogger = pure hlsLogger
9898
, Main.argsIdeOptions = \_config sessionLoader ->
9999
let defOptions = Ghcide.defaultIdeOptions sessionLoader
100100
in defOptions

0 commit comments

Comments
 (0)