on master help sign in
Add logger to Compiler monad
commit8b57ab509f9fbf7bb2058251beb60f39e8b476ca authorJasper Van der Jeugt <jaspervdj@gmail.com> dateMon 21 Feb 2011, 13:02 (16 years ago) parent12a0e53
4 files changed+34−6
src/Hakyll/Core/Compiler.hs+15−2 src/Hakyll/Core/Compiler/Internal.hs+6−1 src/Hakyll/Core/Run.hs+2−2 src/Hakyll/Core/UnixFilter.hs+11−1
src/Hakyll/Core/Compiler.hs+15−2
@@ -102,6 +102,7 @@ module Hakyll.Core.Compiler
102102 , cached
103103 , unsafeCompiler
104104 , mapCompiler
105+ , timedCompiler
105106 ) where
106107
107108import Prelude hiding ((.), id)
@@ -124,6 +125,7 @@ import Hakyll.Core.Compiler.Internal
124125import Hakyll.Core.Store
125126import Hakyll.Core.Rules.Internal
126127import Hakyll.Core.Routes
128+import Hakyll.Core.Logger
127129
128130-- | Run a compiler, yielding the resulting target and it's dependencies. This
129131-- version of 'runCompilerJob' also stores the result
@@ -134,10 +136,12 @@ runCompiler :: Compiler () CompileRule -- ^ Compiler to run
134136 -> Routes -- ^ Route
135137 -> Store -- ^ Store
136138 -> Bool -- ^ Was the resource modified?
139+ -> Logger -- ^ Logger
137140 -> IO CompileRule -- ^ Resulting item
138−runCompiler compiler identifier provider routes store modified = do
141+runCompiler compiler identifier provider routes store modified logger = do
139142 -- Run the compiler job
140− result <- runCompilerJob compiler identifier provider routes store modified
143+ result <-
144+ runCompilerJob compiler identifier provider routes store modified logger
141145
142146 -- Inspect the result
143147 case result of
@@ -274,3 +278,12 @@ unsafeCompiler f = fromJob $ CompilerM . liftIO . f
274278mapCompiler :: Compiler a b
275279 -> Compiler [a] [b]
276280mapCompiler (Compiler d j) = Compiler d $ mapM j
281+
282+-- | Log and time a compiler
283+--
284+timedCompiler :: String -- ^ Message
285+ -> Compiler a b -- ^ Compiler to time
286+ -> Compiler a b -- ^ Resulting compiler
287+timedCompiler msg (Compiler d j) = Compiler d $ \x -> CompilerM $ do
288+ logger <- compilerLogger <$> ask
289+ timed logger msg $ unCompilerM $ j x
src/Hakyll/Core/Compiler/Internal.hs+6−1
@@ -26,6 +26,7 @@ import Hakyll.Core.Identifier
2626import Hakyll.Core.ResourceProvider
2727import Hakyll.Core.Store
2828import Hakyll.Core.Routes
29+import Hakyll.Core.Logger
2930
3031-- | A set of dependencies
3132--
@@ -44,6 +45,8 @@ data CompilerEnvironment = CompilerEnvironment
4445 compilerStore :: Store
4546 , -- | Flag indicating if the underlying resource was modified
4647 compilerResourceModified :: Bool
48+ , -- | Logger
49+ compilerLogger :: Logger
4750 }
4851
4952-- | The compiler monad
@@ -91,8 +94,9 @@ runCompilerJob :: Compiler () a -- ^ Compiler to run
9194 -> Routes -- ^ Route
9295 -> Store -- ^ Store
9396 -> Bool -- ^ Was the resource modified?
97+ -> Logger -- ^ Logger
9498 -> IO a
95−runCompilerJob compiler identifier provider route store modified =
99+runCompilerJob compiler identifier provider route store modified logger =
96100 runReaderT (unCompilerM $ compilerJob compiler ()) env
97101 where
98102 env = CompilerEnvironment
@@ -101,6 +105,7 @@ runCompilerJob compiler identifier provider route store modified =
101105 , compilerRoutes = route
102106 , compilerStore = store
103107 , compilerResourceModified = modified
108+ , compilerLogger = logger
104109 }
105110
106111runCompilerDependencies :: Compiler () a
src/Hakyll/Core/Run.hs+2−2
@@ -183,8 +183,8 @@ runCompilers ((id', compiler) : compilers) = Runtime $ do
183183 isModified = id' `S.member` modified'
184184
185185 -- Run the compiler
186− result <- timed logger "Compiling item" $
187− liftIO $ runCompiler compiler id' provider routes store isModified
186+ result <- timed logger "Total compile time" $ liftIO $
187+ runCompiler compiler id' provider routes store isModified logger
188188
189189 case result of
190190 -- Compile rule for one item, easy stuff
src/Hakyll/Core/UnixFilter.hs+11−1
@@ -34,7 +34,17 @@ import Hakyll.Core.Compiler
3434unixFilter :: String -- ^ Program name
3535 -> [String] -- ^ Program args
3636 -> Compiler String String -- ^ Resulting compiler
37−unixFilter programName args = unsafeCompiler $ \input -> do
37+unixFilter programName args =
38+ timedCompiler ("Executing external program " ++ programName) $
39+ unsafeCompiler $ \input -> unixFilterIO programName args input
40+
41+-- | Internally used function
42+--
43+unixFilterIO :: String
44+ -> [String]
45+ -> String
46+ -> IO String
47+unixFilterIO programName args input = do
3848 -- Create pipes
3949 (stdinRead, stdinWrite) <- createPipe
4050 (stdoutRead, stdoutWrite) <- createPipe