2018-05-24 10:10 GMT+02:00 Thomas Danckaert <p...@thomasdanckaert.be>:
> Hi Guilers, > > I want to run an external process, and capture both its stdout and stderr > output streams. I didn't find an easy way to get a port for an external > process' stderr, other than wrapping the call to the process in > "with-error-to-port". > > My questions are: > 0) Did I miss something obvious? :) > 1) why doesn't the second approach in the code below work (the > output-string is always empty)? > 2) are there any other (better) solutions? > 3) I suppose my approach with (pipe) might block if the process writes > too much data to stderr before I read from the pipe. Should I use "select" > to interleave reads from stdout and stderr? > > thank you! > > Thomas > > (use-modules (ice-9 format) > (ice-9 popen) > (ice-9 textual-ports)) > > (define (run-command) > (let ((process (open-input-pipe "./stdoutstderr.sh"))) > (format #t "stdout: '~a'~%" (get-string-all process)) > (close-pipe process))) > > ;; Works: > (let ((err-pipe (pipe))) > (with-error-to-port (cdr err-pipe) run-command) > > (close-port (cdr err-pipe)) > (format #t "stderr: '~a' ~%" (get-string-all (car err-pipe)))) > > ;; Doesn't work: (get-output-string) returns empty string. > (let ((err-port (open-output-string))) > (with-error-to-port err-port run-command) > > (format #t "stderr: '~a'~%" (get-output-string err-port)) > (close-output-port err-port)) > > Hi Thomas I'm the most incompetent guile user on earth and yet I want to venture into offering a couple of observations The first observation is that in the first case, you close the port cotaining the error _prior_ to attempting reading from the port In the second case, you attempt reading from the port AND THEN you close the port I undertsand that the writing on ports is mediated by some form of buffering, or something, and closing the port also "flushes" the port, meaning that if there are some remnants to be written yet, they get written I don't even know of with-error-to-port automatically closes the port so that manually closing it shouldn't be necessary If with-error-to-port closes the port, then my observations doesn't apply I run into this misundertsanding not a long time ago and I wrote about it on Mastodon, here https://mastodon.social/@catonano/99979214792830581 The second observation is that you could try to see forr yourself by expanding your calls and visually inspect the expanded versions They should differ in that you should be able to see the closing of the port (I never checed this out) Ok I rambled enough Hope this helps ! Ciao