1
1
{-# OPTIONS_GHC -Wno-orphans #-}
2
2
module Development.IDE.Main
3
3
(Arguments (.. )
4
+ ,defaultArguments
4
5
,Command (.. )
5
6
,IdeCommand (.. )
6
7
,isLSP
@@ -22,12 +23,17 @@ import Data.Hashable (hashed)
22
23
import Data.List.Extra (intercalate , isPrefixOf ,
23
24
nub , nubOrd , partition )
24
25
import Data.Maybe (catMaybes , isJust )
26
+ import Data.String
25
27
import qualified Data.Text as T
28
+ import Data.Text.Encoding (encodeUtf8 )
26
29
import qualified Data.Text.IO as T
27
30
import Data.Text.Lazy.Encoding (decodeUtf8 )
28
31
import qualified Data.Text.Lazy.IO as LT
32
+ import Data.Word (Word16 )
33
+ import Debug.Trace.Flags (userTracingEnabled )
29
34
import Development.IDE (Action , GhcVersion (.. ),
30
- Rules , ghcVersion ,
35
+ Priority (Debug ), Rules ,
36
+ ghcVersion ,
31
37
hDuplicateTo' )
32
38
import Development.IDE.Core.Debouncer (Debouncer ,
33
39
newAsyncDebouncer )
@@ -64,6 +70,7 @@ import Development.IDE.Session (SessionLoadingOptions,
64
70
import Development.IDE.Types.Location (NormalizedUri ,
65
71
toNormalizedFilePath' )
66
72
import Development.IDE.Types.Logger (Logger (Logger ),
73
+ Priority (Info ),
67
74
logDebug , logInfo )
68
75
import Development.IDE.Types.Options (IdeGhcSession ,
69
76
IdeOptions (optCheckParents , optCheckProject , optReportProgress , optRunSubset ),
@@ -94,6 +101,7 @@ import Ide.Types (IdeCommand (IdeCommand),
94
101
ipMap )
95
102
import qualified Language.LSP.Server as LSP
96
103
import Numeric.Natural (Natural )
104
+ import OpenTelemetry.Eventlog (addEvent , withSpan )
97
105
import Options.Applicative hiding (action )
98
106
import qualified System.Directory.Extra as IO
99
107
import System.Exit (ExitCode (ExitFailure ),
@@ -175,10 +183,13 @@ data Arguments = Arguments
175
183
}
176
184
177
185
instance Default Arguments where
178
- def = Arguments
186
+ def = defaultArguments Info
187
+
188
+ defaultArguments :: Priority -> Arguments
189
+ defaultArguments priority = Arguments
179
190
{ argsOTMemoryProfiling = False
180
191
, argCommand = LSP
181
- , argsLogger = stderrLogger
192
+ , argsLogger = stderrLogger priority <> telemetryLogger
182
193
, argsRules = mainRule >> action kick
183
194
, argsGhcidePlugin = mempty
184
195
, argsHlsPlugins = pluginDescToIdePlugins Ghcide. descriptors
@@ -207,7 +218,7 @@ instance Default Arguments where
207
218
}
208
219
209
220
testing :: Arguments
210
- testing = def {
221
+ testing = (defaultArguments Debug ) {
211
222
argsHlsPlugins = pluginDescToIdePlugins $
212
223
idePluginsToPluginDesc (argsHlsPlugins def)
213
224
++ [Test. blockCommandDescriptor " block-command" , Test. plugin],
@@ -219,12 +230,22 @@ testing = def {
219
230
}
220
231
221
232
-- | Cheap stderr logger that relies on LineBuffering
222
- stderrLogger :: IO Logger
223
- stderrLogger = do
233
+ stderrLogger :: Priority -> IO Logger
234
+ stderrLogger logLevel = do
224
235
lock <- newLock
225
- return $ Logger $ \ p m -> withLock lock $
236
+ return $ Logger $ \ p m -> when (p >= logLevel) $ withLock lock $
226
237
T. hPutStrLn stderr $ " [" <> T. pack (show p) <> " ] " <> m
227
238
239
+ telemetryLogger :: IO Logger
240
+ telemetryLogger
241
+ | userTracingEnabled = return $ Logger $ \ p m ->
242
+ withSpan " log" $ \ sp ->
243
+ addEvent sp (fromString $ " Log " <> show p) (encodeUtf8 $ trim m)
244
+ | otherwise = mempty
245
+ where
246
+ -- eventlog message size is limited by EVENT_PAYLOAD_SIZE_MAX = STG_WORD16_MAX
247
+ trim = T. take (fromIntegral (maxBound :: Word16 ) - 10 )
248
+
228
249
defaultMain :: Arguments -> IO ()
229
250
defaultMain Arguments {.. } = do
230
251
setLocaleEncoding utf8
0 commit comments