@@ -6,16 +6,17 @@ module Cardano.Tracer.Handlers.Logs.Rotator
66 ) where
77
88import Cardano.Tracer.Configuration
9- import Cardano.Tracer.Environment
9+ import Cardano.Tracer.Environment ( TracerEnv ( .. ), forever'tilShutdown )
1010import Cardano.Tracer.Handlers.Logs.Utils (createOrUpdateEmptyLog , getTimeStampFromLog ,
1111 isItLog )
1212import Cardano.Tracer.MetaTrace
1313import Cardano.Tracer.Types (HandleRegistry , HandleRegistryKey , NodeName )
1414import Cardano.Tracer.Utils (showProblemIfAny , readRegistry )
1515
1616import Control.Concurrent.Async (forConcurrently_ )
17+ import Control.Concurrent.Chan.Unagi (dupChan )
1718import Control.Concurrent.Extra (Lock )
18- import Control.Monad (forM_ , forever , unless , when )
19+ import Control.Monad (forM_ , unless , when )
1920import Control.Monad.Extra (whenJust , whenM )
2021import Data.Foldable (for_ )
2122import Data.List (nub , sort )
@@ -33,38 +34,40 @@ import System.Time.Extra (sleep)
3334
3435-- | Runs rotation mechanism for the log files.
3536runLogsRotator :: TracerEnv -> IO ()
36- runLogsRotator TracerEnv
37- { teConfig = TracerConfig {rotation, verbosity, logging}
38- , teCurrentLogLock
39- , teTracer
40- , teRegistry
41- } = do
42- whenJust rotation \ rotParams -> do
37+ runLogsRotator tracerEnv@ TracerEnv { teConfig = TracerConfig {rotation}, teTracer } = do
38+ whenJust rotation \ rot -> do
4339 traceWith teTracer TracerStartedLogRotator
44- launchRotator loggingParamsForFiles rotParams verbosity teRegistry teCurrentLogLock
45- where
40+ launchRotator tracerEnv rot
41+
42+ launchRotator
43+ :: TracerEnv
44+ -> RotationParams
45+ -> IO ()
46+ launchRotator tracerEnv rot@ RotationParams {rpFrequencySecs} = do
47+ whenNonEmpty loggingParamsForFiles do
48+ outChan <- dupChan teInChan
49+ forever'tilShutdown outChan do
50+ showProblemIfAny verbosity do
51+ forM_ loggingParamsForFiles \ loggingParam -> do
52+ checkRootDir teCurrentLogLock teRegistry rot loggingParam
53+ sleep (fromIntegral rpFrequencySecs)
54+ where
55+ whenNonEmpty :: Applicative f => [a ] -> f () -> f ()
56+ whenNonEmpty = unless . null
57+
58+ TracerEnv
59+ { teConfig = TracerConfig {verbosity, logging}
60+ , teCurrentLogLock
61+ , teRegistry
62+ , teInChan
63+ } = tracerEnv
64+
4665 loggingParamsForFiles :: [LoggingParams ]
4766 loggingParamsForFiles = nub (NE. filter filesOnly logging)
4867
4968 filesOnly :: LoggingParams -> Bool
5069 filesOnly LoggingParams {logMode} = logMode == FileMode
5170
52- launchRotator
53- :: [LoggingParams ]
54- -> RotationParams
55- -> Maybe Verbosity
56- -> HandleRegistry
57- -> Lock
58- -> IO ()
59- launchRotator [] _ _ _ _ = return ()
60- launchRotator loggingParamsForFiles
61- rotParams@ RotationParams {rpFrequencySecs} verb registry currentLogLock =
62- forever do
63- showProblemIfAny verb do
64- forM_ loggingParamsForFiles \ loggingParam -> do
65- checkRootDir currentLogLock registry rotParams loggingParam
66- sleep $ fromIntegral rpFrequencySecs
67-
6871-- | All the logs with 'TraceObject's received from particular node
6972-- will be stored in a separate subdirectory in the root directory.
7073--
0 commit comments