1
1
module Development.IDE.Main (Arguments (.. ), defaultMain ) where
2
- import Control.Concurrent.Extra (readVar )
2
+ import Control.Concurrent.Extra (newLock , readVar , withLock )
3
3
import Control.Exception.Safe (
4
4
Exception (displayException ),
5
5
catchAny ,
@@ -16,7 +16,8 @@ import Data.List.Extra (
16
16
)
17
17
import Data.Maybe (catMaybes , fromMaybe , isJust )
18
18
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 )
20
21
import Development.IDE.Core.Debouncer (newAsyncDebouncer )
21
22
import Development.IDE.Core.FileStore (makeVFSHandle )
22
23
import Development.IDE.Core.OfInterest (
@@ -49,7 +50,6 @@ import Development.IDE.Plugin (
49
50
import Development.IDE.Plugin.HLS (asGhcIdePlugin )
50
51
import Development.IDE.Session (SessionLoadingOptions , loadSessionWithOptions , setInitialDynFlags , getHieDbLoc , runWithDb )
51
52
import Development.IDE.Types.Location (toNormalizedFilePath' )
52
- import Development.IDE.Types.Logger (Logger )
53
53
import Development.IDE.Types.Options (
54
54
IdeGhcSession ,
55
55
IdeOptions (optCheckParents , optCheckProject , optReportProgress ),
@@ -66,15 +66,16 @@ import qualified Language.LSP.Server as LSP
66
66
import qualified System.Directory.Extra as IO
67
67
import System.Exit (ExitCode (ExitFailure ), exitWith )
68
68
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 ) )
70
70
import System.Time.Extra (offsetTime , showDuration )
71
71
import Text.Printf (printf )
72
72
import qualified Development.IDE.Plugin.HLS.GhcIde as Ghcide
73
+ import Development.IDE.Types.Logger (Logger (Logger ))
73
74
74
75
data Arguments = Arguments
75
76
{ argsOTMemoryProfiling :: Bool
76
77
, argFiles :: Maybe [FilePath ] -- ^ Nothing: lsp server ; Just: typecheck and exit
77
- , argsLogger :: Logger
78
+ , argsLogger :: IO Logger
78
79
, argsRules :: Rules ()
79
80
, argsHlsPlugins :: IdePlugins IdeState
80
81
, argsGhcidePlugin :: Plugin Config -- ^ Deprecated
@@ -89,7 +90,7 @@ instance Default Arguments where
89
90
def = Arguments
90
91
{ argsOTMemoryProfiling = False
91
92
, argFiles = Nothing
92
- , argsLogger = noLogging
93
+ , argsLogger = stderrLogger
93
94
, argsRules = mainRule >> action kick
94
95
, argsGhcidePlugin = mempty
95
96
, argsHlsPlugins = pluginDescToIdePlugins Ghcide. descriptors
@@ -100,9 +101,18 @@ instance Default Arguments where
100
101
, argsGetHieDbLoc = getHieDbLoc
101
102
}
102
103
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
+
103
111
defaultMain :: Arguments -> IO ()
104
112
defaultMain Arguments {.. } = do
105
113
pid <- T. pack . show <$> getProcessID
114
+ logger <- argsLogger
115
+ hSetBuffering stderr LineBuffering
106
116
107
117
let hlsPlugin = asGhcIdePlugin argsDefaultHlsConfig argsHlsPlugins
108
118
hlsCommands = allLspCmdIds' pid argsHlsPlugins
@@ -141,7 +151,7 @@ defaultMain Arguments{..} = do
141
151
argsDefaultHlsConfig
142
152
rules
143
153
(Just env)
144
- argsLogger
154
+ logger
145
155
debouncer
146
156
options
147
157
vfs
@@ -178,7 +188,7 @@ defaultMain Arguments{..} = do
178
188
{ optCheckParents = pure NeverCheck
179
189
, optCheckProject = pure False
180
190
}
181
- ide <- initialise argsDefaultHlsConfig rules Nothing argsLogger debouncer options vfs hiedb hieChan
191
+ ide <- initialise argsDefaultHlsConfig rules Nothing logger debouncer options vfs hiedb hieChan
182
192
183
193
putStrLn " \n Step 4/4: Type checking the files"
184
194
setFilesOfInterest ide $ HashMap. fromList $ map ((,OnDisk ) . toNormalizedFilePath') files
@@ -205,7 +215,7 @@ defaultMain Arguments{..} = do
205
215
Key GhcSessionDeps :
206
216
[k | (_, k) <- HashMap. keys values, k /= Key GhcSessionIO ]
207
217
++ [Key GhcSessionIO ]
208
- measureMemory argsLogger [keys] consoleObserver valuesRef
218
+ measureMemory logger [keys] consoleObserver valuesRef
209
219
210
220
unless (null failed) (exitWith $ ExitFailure (length failed))
211
221
{-# ANN defaultMain ("HLint: ignore Use nubOrd" :: String) #-}
0 commit comments