Skip to content

Commit

Permalink
Use IHP logger instead of directly printing to output
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Aug 28, 2021
1 parent 5306c1a commit 2851921
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
9 changes: 8 additions & 1 deletion IHP/IDE/Types.hs
Expand Up @@ -9,6 +9,9 @@ import qualified Data.ByteString.Char8 as ByteString
import IHP.IDE.PortConfig
import Data.String.Conversions (cs)

import qualified IHP.Log.Types as Log
import qualified IHP.Log as Log

data ManagedProcess = ManagedProcess
{ inputHandle :: !Handle
, outputHandle :: !Handle
Expand All @@ -28,7 +31,7 @@ cleanupManagedProcess (ManagedProcess { .. }) = Process.cleanupProcess (Just inp

sendGhciCommand :: (?context :: Context) => ManagedProcess -> ByteString -> IO ()
sendGhciCommand ManagedProcess { inputHandle } command = do
when (isDebugMode ?context) (putStrLn ("GHCI: " <> cs command))
when (isDebugMode ?context) (Log.debug ("GHCI: " <> cs command :: Text))
ByteString.hPutStrLn inputHandle command
Handle.hFlush inputHandle

Expand Down Expand Up @@ -145,7 +148,11 @@ data Context = Context
, portConfig :: !PortConfig
, appStateRef :: !(IORef AppState)
, isDebugMode :: !Bool
, logger :: !Log.Logger
}

dispatch :: (?context :: Context) => Action -> IO ()
dispatch = let Context { .. } = ?context in putMVar actionVar

instance Log.LoggingProvider Context where
getLogger Context { logger } = logger
11 changes: 7 additions & 4 deletions IHP/Telemetry.hs
Expand Up @@ -18,6 +18,9 @@ import qualified Data.ByteString.Base16 as Base16
import qualified Data.Text as T
import qualified Data.Text.IO as TIO

import qualified IHP.Log.Types as Log
import qualified IHP.Log as Log

data TelemetryInfo = TelemetryInfo
{ ihpVersion :: !Text
, os :: !Text
Expand All @@ -28,16 +31,16 @@ data TelemetryInfo = TelemetryInfo
-- | Reports telemetry info to the IHP Telemetry server
--
-- This can be disabled by setting the env var IHP_TELEMETRY_DISABLED=1
reportTelemetry :: IO ()
reportTelemetry :: (?context :: context, Log.LoggingProvider context) => IO ()
reportTelemetry = do
isDisabled <- maybe False (\value -> value == "1") <$> Env.lookupEnv "IHP_TELEMETRY_DISABLED"
unless isDisabled do
payload <- toPayload <$> getTelemetryInfo
putStrLn $ show payload
Log.info (tshow payload)
result <- Exception.try (Wreq.post "https://ihp-telemetry.digitallyinduced.com/CreateEvent" payload)
case result of
Left (e :: IOException) -> putStrLn ("Telemetry failed: " <> show e)
Right _ -> putStrLn "IHP Telemetry is activated. This can be disabled by setting env variable IHP_TELEMETRY_DISABLED=1"
Left (e :: IOException) -> Log.warn ("Telemetry failed: " <> show e)
Right _ -> Log.info ("IHP Telemetry is activated. This can be disabled by setting env variable IHP_TELEMETRY_DISABLED=1" :: Text)

getTelemetryInfo :: IO TelemetryInfo
getTelemetryInfo = do
Expand Down
32 changes: 19 additions & 13 deletions exe/IHP/IDE/DevServer.hs
Expand Up @@ -26,6 +26,10 @@ import qualified IHP.Telemetry as Telemetry
import qualified IHP.Version as Version
import qualified Data.Time.Clock as Clock

import qualified IHP.Log.Types as Log
import qualified IHP.Log as Log
import Data.Default (def, Default (..))

main :: IO ()
main = do
actionVar <- newEmptyMVar
Expand All @@ -38,11 +42,13 @@ main = do
-- Like: $ DEBUG=1 ./start
isDebugMode <- maybe False (\value -> value == "1") <$> Env.lookupEnv "DEBUG"

-- Print IHP Version when in debug mode
when isDebugMode (putStrLn ("IHP Version: " <> Version.ihpVersion))
setProcessLimits

let ?context = Context { actionVar, portConfig, appStateRef, isDebugMode }
logger <- Log.newLogger def
let ?context = Context { actionVar, portConfig, appStateRef, isDebugMode, logger }

-- Print IHP Version when in debug mode
when isDebugMode (Log.debug ("IHP Version: " <> Version.ihpVersion))

threadId <- myThreadId
let catchHandler = do
Expand All @@ -55,9 +61,9 @@ main = do
async Telemetry.reportTelemetry
forever do
appState <- readIORef appStateRef
when isDebugMode (putStrLn $ " ===> " <> (tshow appState))
when isDebugMode (Log.debug $ " ===> " <> (tshow appState))
action <- takeMVar actionVar
when isDebugMode (putStrLn $ tshow action)
when isDebugMode (Log.debug $ tshow action)
nextAppState <- handleAction appState action
writeIORef appStateRef nextAppState

Expand Down Expand Up @@ -95,7 +101,7 @@ handleAction state@(AppState { appGHCIState, statusServerState, postgresState })
AppGHCINotStarted -> error "Unreachable AppGHCINotStarted"
AppGHCIModulesLoaded { } -> do
-- You can trigger this case by running: $ while true; do touch test.hs; done;
when (get #isDebugMode ?context) (putStrLn "AppGHCIModulesLoaded triggered multiple times. This happens when multiple file change events are detected. Skipping app start as the app is already starting from a previous file change event")
when (get #isDebugMode ?context) (Log.debug ("AppGHCIModulesLoaded triggered multiple times. This happens when multiple file change events are detected. Skipping app start as the app is already starting from a previous file change event" :: Text))
pure state
handleAction state@(AppState { appGHCIState, statusServerState, postgresState, liveReloadNotificationServerState }) (AppModulesLoaded { success = False }) = do
statusServerState' <- case statusServerState of
Expand Down Expand Up @@ -145,15 +151,15 @@ handleAction state@(AppState { liveReloadNotificationServerState, appGHCIState,

handleAction state SchemaChanged = do
async do
SchemaCompiler.compile `catch` (\(exception :: SomeException) -> do putStrLn (tshow exception); dispatch (ReceiveAppOutput { line = ErrorOutput (cs $ tshow exception) }))
SchemaCompiler.compile `catch` (\(exception :: SomeException) -> do Log.error (tshow exception); dispatch (ReceiveAppOutput { line = ErrorOutput (cs $ tshow exception) }))
pure state

handleAction state@(AppState { appGHCIState }) PauseApp =
case appGHCIState of
RunningAppGHCI { .. } -> do
pauseAppGHCI appGHCIState
pure state { appGHCIState = AppGHCIModulesLoaded { .. } }
otherwise -> do putStrLn ("Could not pause app as it's not in running state" <> tshow otherwise); pure state
otherwise -> do Log.info ("Could not pause app as it's not in running state" <> tshow otherwise); pure state



Expand All @@ -169,7 +175,7 @@ start = do

stop :: (?context :: Context) => AppState -> IO ()
stop AppState { .. } = do
when (get #isDebugMode ?context) (putStrLn "Stop called")
when (get #isDebugMode ?context) (Log.debug ("Stop called" :: Text))
stopAppGHCI appGHCIState
stopPostgres postgresState
stopStatusServer statusServerState
Expand Down Expand Up @@ -290,7 +296,7 @@ startAppGHCI = do
]

async $ forever $ ByteString.hGetLine outputHandle >>= \line -> do
unless isDebugMode (ByteString.putStrLn line)
unless isDebugMode (Log.info line)
if "Server started" `isInfixOf` line
then dispatch AppStarted
else if "Failed," `isInfixOf` line
Expand All @@ -302,7 +308,7 @@ startAppGHCI = do
else dispatch ReceiveAppOutput { line = StandardOutput line }

async $ forever $ ByteString.hGetLine errorHandle >>= \line -> do
unless isDebugMode (ByteString.putStrLn line)
unless isDebugMode (Log.info line)
if "cannot find object file for module" `isInfixOf` line
then do
forEach loadAppCommands (sendGhciCommand process)
Expand All @@ -311,7 +317,7 @@ startAppGHCI = do


-- Compile Schema before loading the app
SchemaCompiler.compile `catch` (\(e :: SomeException) -> putStrLn (tshow e))
SchemaCompiler.compile `catch` (\(e :: SomeException) -> Log.error (tshow e))

forEach loadAppCommands (sendGhciCommand process)

Expand All @@ -327,7 +333,7 @@ startLoadedApp (AppGHCIModulesLoaded { .. }) = do
forEach commands (sendGhciCommand process)
startLoadedApp (RunningAppGHCI { .. }) = error "Cannot start app as it's already in running statstate"
startLoadedApp (AppGHCILoading { .. }) = sendGhciCommand process "app <- ClassyPrelude.async (main `catch` \\(e :: SomeException) -> IHP.Prelude.putStrLn (tshow e))"
startLoadedApp _ = when (get #isDebugMode ?context) (putStrLn "startLoadedApp: App not running")
startLoadedApp _ = when (get #isDebugMode ?context) (Log.debug ("startLoadedApp: App not running" :: Text))


stopAppGHCI :: AppGHCIState -> IO ()
Expand Down

0 comments on commit 2851921

Please sign in to comment.