Logging and really not working light restoration

This commit is contained in:
2026-09-30 13:46:16 +03:00
parent 1098be227d
commit c75fc1a7b9
10 changed files with 180 additions and 151 deletions
+17 -13
View File
@@ -10,6 +10,7 @@ module HomeAssistant.Runtime.Bus
, recordInbound
, recordOutbound
, channelHassEval
, withLogEnv
) where
import Control.Concurrent.STM
@@ -51,33 +52,36 @@ data Bus = Bus
, busFlags :: Flags
}
withBus :: Severity -> AppMetrics -> Flags -> (Bus -> IO a) -> IO a
withBus severity metrics flags callback = do
withLogEnv :: Severity -> (LogEnv -> IO a) -> IO a
withLogEnv severity f = do
let makeLogEnv = registerJournalScribe =<< registerHandleScribe =<< initLogEnv "hass-controller" "production"
-- closeScribes will stop accepting new logs, flush existing ones and clean up resources
bracket makeLogEnv closeScribes $ \le -> do
bus <- Bus
<$> newBroadcastTChanIO
<*> newTChanIO
<*> newTVarIO Nothing
<*> mkCallIdGen 0
<*> pure le
<*> pure metrics
<*> pure flags
callback bus
bracket makeLogEnv closeScribes $ \le -> f le
where
registerJournalScribe :: LogEnv -> IO LogEnv
registerJournalScribe le = do
journalScribe <- mkJournalScribe (permitItem severity) V2
registerScribe "journalctl" journalScribe defaultScribeSettings le
registerHandleScribe le = do
systemdUnit <- isJust <$> lookupEnv "INVOCATION_ID"
systemdUnit <- isJust <$> lookupEnv "HA_CONTROLLER_SYSTEMD"
if systemdUnit
then pure le -- ignore stdout when running in systemd
else do
handleScribe <- mkHandleScribe ColorIfTerminal stdout (permitItem severity) V2
registerScribe "stdout" handleScribe defaultScribeSettings le
withBus :: MonadIO m => LogEnv -> AppMetrics -> Flags -> (Bus -> m a) -> m a
withBus le metrics flags callback = do
bus <- Bus
<$> liftIO newBroadcastTChanIO
<*> liftIO newTChanIO
<*> liftIO (newTVarIO Nothing)
<*> liftIO (mkCallIdGen 0)
<*> pure le
<*> pure metrics
<*> pure flags
callback bus
recordInbound :: Bus -> IO ()
recordInbound bus = inc (amTriggersIn (busMetrics bus))