git.iko.soy/typeable/pandoc
on master help sign in
Process.pipeProcess: stream stderr rather than capturing.

Signature of pipeProcess has changed: the return value is now IO (ExitCode, ByteString) -- with only stdout. Stderr is just inherited from the parent. This means that stderr from filters will now be streamed as the filters are run. Closes #2729.

commit9570f59066c1e89500fcd8ab6ac6a401159ece27 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 9 Dec 2016, 15:59 (10 years ago) parentda2055d
3 files changed+16−29
pandoc.hs+1−2 src/Text/Pandoc/PDF.hs+7−13 src/Text/Pandoc/Process.hs+8−14
pandoc.hs+1−2
@@ -137,9 +137,8 @@ externalFilter f args' d = do
137137 "Could not find executable '" ++ f' ++ "'."
138138 env <- getEnvironment
139139 let env' = Just $ ("PANDOC_VERSION", pandocVersion) : env
140− (exitcode, outbs, errbs) <- E.handle filterException $
140+ (exitcode, outbs) <- E.handle filterException $
141141 pipeProcess env' f' args'' $ encode d
142− unless (B.null errbs) $ B.hPutStr stderr errbs
143142 case exitcode of
144143 ExitSuccess -> return $ either error id $ eitherDecode' outbs
145144 ExitFailure ec -> err 83 $ "Error running filter " ++ f ++ "\n" ++
src/Text/Pandoc/PDF.hs+7−13
@@ -37,7 +37,7 @@ import qualified Data.ByteString as BS
3737import Data.Monoid ((<>))
3838import System.Exit (ExitCode (..))
3939import System.FilePath
40−import System.IO (stderr, stdout)
40+import System.IO (stdout)
4141import System.IO.Temp (withTempFile)
4242import System.Directory
4343import Data.Digest.Pure.SHA (showDigest, sha1)
@@ -247,11 +247,10 @@ runTeXProgram verbose program args runNumber numRuns tmpDir source = do
247247 putStrLn $ "[makePDF] Contents of " ++ file' ++ ":"
248248 B.readFile file' >>= B.putStr
249249 putStr "\n"
250− (exit, out, err) <- pipeProcess (Just env'') program programArgs BL.empty
250+ (exit, out) <- pipeProcess (Just env'') program programArgs BL.empty
251251 when verbose $ do
252252 putStrLn $ "[makePDF] Run #" ++ show runNumber
253253 B.hPutStr stdout out
254− B.hPutStr stderr err
255254 putStr "\n"
256255 if runNumber <= numRuns
257256 then runTeXProgram verbose program args (runNumber + 1) numRuns tmpDir source
@@ -264,7 +263,7 @@ runTeXProgram verbose program args runNumber numRuns tmpDir source = do
264263 -- See https://github.com/jgm/pandoc/issues/1192.
265264 then (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
266265 else return Nothing
267− return (exit, out <> err, pdf)
266+ return (exit, out, pdf)
268267
269268html2pdf :: Bool -- ^ Verbose output
270269 -> [String] -- ^ Args to wkhtmltopdf
@@ -286,12 +285,10 @@ html2pdf verbose args source = do
286285 putStrLn $ "[makePDF] Contents of " ++ file ++ ":"
287286 B.readFile file >>= B.putStr
288287 putStr "\n"
289− (exit, out, err) <- pipeProcess (Just env') "wkhtmltopdf"
290− programArgs BL.empty
288+ (exit, out) <- pipeProcess (Just env') "wkhtmltopdf" programArgs BL.empty
291289 removeFile file
292290 when verbose $ do
293291 B.hPutStr stdout out
294− B.hPutStr stderr err
295292 putStr "\n"
296293 pdfExists <- doesFileExist pdfFile
297294 mbPdf <- if pdfExists
@@ -303,9 +300,8 @@ html2pdf verbose args source = do
303300 removeFile pdfFile
304301 return res
305302 else return Nothing
306− let log' = out <> err
307303 return $ case (exit, mbPdf) of
308− (ExitFailure _, _) -> Left log'
304+ (ExitFailure _, _) -> Left out
309305 (ExitSuccess, Nothing) -> Left ""
310306 (ExitSuccess, Just pdf) -> Right pdf
311307
@@ -341,10 +337,9 @@ context2pdf verbose tmpDir source = inDirectory tmpDir $ do
341337 putStrLn $ "[makePDF] Contents of " ++ file ++ ":"
342338 B.readFile file >>= B.putStr
343339 putStr "\n"
344− (exit, out, err) <- pipeProcess (Just env'') "context" programArgs BL.empty
340+ (exit, out) <- pipeProcess (Just env'') "context" programArgs BL.empty
345341 when verbose $ do
346342 B.hPutStr stdout out
347− B.hPutStr stderr err
348343 putStr "\n"
349344 let pdfFile = replaceExtension file ".pdf"
350345 pdfExists <- doesFileExist pdfFile
@@ -354,10 +349,9 @@ context2pdf verbose tmpDir source = inDirectory tmpDir $ do
354349 -- See https://github.com/jgm/pandoc/issues/1192.
355350 then (Just . B.fromChunks . (:[])) `fmap` BS.readFile pdfFile
356351 else return Nothing
357− let log' = out <> err
358352 case (exit, mbPdf) of
359353 (ExitFailure _, _) -> do
360− let logmsg = extractConTeXtMsg log'
354+ let logmsg = extractConTeXtMsg out
361355 return $ Left logmsg
362356 (ExitSuccess, Nothing) -> return $ Left ""
363357 (ExitSuccess, Just pdf) -> return $ Right pdf
src/Text/Pandoc/Process.hs+8−14
@@ -42,9 +42,9 @@ Version of 'System.Process.readProcessWithExitCode' that uses lazy bytestrings
4242instead of strings and allows setting environment variables.
4343
4444@readProcessWithExitCode@ creates an external process, reads its
45−standard output and standard error strictly, waits until the process
46−terminates, and then returns the 'ExitCode' of the process,
47−the standard output, and the standard error.
45+standard output strictly, waits until the process
46+terminates, and then returns the 'ExitCode' of the process
47+and the standard output. stderr is inherited from the parent.
4848
4949If an asynchronous exception is thrown to the thread executing
5050@readProcessWithExitCode@, the forked process will be terminated and
@@ -57,25 +57,21 @@ pipeProcess
5757 -> FilePath -- ^ Filename of the executable (see 'proc' for details)
5858 -> [String] -- ^ any arguments
5959 -> BL.ByteString -- ^ standard input
60− -> IO (ExitCode,BL.ByteString,BL.ByteString) -- ^ exitcode, stdout, stderr
60+ -> IO (ExitCode,BL.ByteString) -- ^ exitcode, stdout
6161pipeProcess mbenv cmd args input =
6262 mask $ \restore -> do
63− (Just inh, Just outh, Just errh, pid) <- createProcess (proc cmd args)
63+ (Just inh, Just outh, Nothing, pid) <- createProcess (proc cmd args)
6464 { env = mbenv,
6565 std_in = CreatePipe,
6666 std_out = CreatePipe,
67− std_err = CreatePipe }
67+ std_err = Inherit }
6868 flip onException
69− (do hClose inh; hClose outh; hClose errh;
69+ (do hClose inh; hClose outh;
7070 terminateProcess pid; waitForProcess pid) $ restore $ do
7171 -- fork off a thread to start consuming stdout
7272 out <- BL.hGetContents outh
7373 waitOut <- forkWait $ evaluate $ BL.length out
7474
75− -- fork off a thread to start consuming stderr
76− err <- BL.hGetContents errh
77− waitErr <- forkWait $ evaluate $ BL.length err
78−
7975 -- now write and flush any input
8076 let writeInput = do
8177 unless (BL.null input) $ do
@@ -87,15 +83,13 @@ pipeProcess mbenv cmd args input =
8783
8884 -- wait on the output
8985 waitOut
90− waitErr
9186
9287 hClose outh
93− hClose errh
9488
9589 -- wait on the process
9690 ex <- waitForProcess pid
9791
98− return (ex, out, err)
92+ return (ex, out)
9993
10094forkWait :: IO a -> IO (IO a)
10195forkWait a = do