From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Dr. Arne Babenhauserheide" Newsgroups: gmane.lisp.guile.user Subject: Re: How do you record output of both STDERR and STDIN to a string? Date: Thu, 10 Sep 2020 19:48:52 +0200 Message-ID: <87o8mdjzej.fsf@web.de> References: <865z8mph4e.fsf@gmail.com> <4ede1fc86c21609bfb45dd3f33eaa64b7df785ba.camel@gnu.org> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35216"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.4.13; emacs 27.1 Cc: guile-user@gnu.org, "Bonface M. K." To: Roel Janssen Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Thu Sep 10 19:50:34 2020 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kGQiP-00091a-JN for guile-user@m.gmane-mx.org; Thu, 10 Sep 2020 19:50:33 +0200 Original-Received: from localhost ([::1]:37986 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kGQiO-0000ys-DU for guile-user@m.gmane-mx.org; Thu, 10 Sep 2020 13:50:32 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:47098) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kGQgx-0000yD-9p for guile-user@gnu.org; Thu, 10 Sep 2020 13:49:03 -0400 Original-Received: from mout.web.de ([212.227.17.12]:39185) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kGQgu-0004n9-P1; Thu, 10 Sep 2020 13:49:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=dbaedf251592; t=1599760136; bh=R+S5/xam8oaBejez+dUjKGUloTwHQmPq8yogW21wJp4=; h=X-UI-Sender-Class:References:From:To:Cc:Subject:In-reply-to:Date; b=gXIzebHuxHTHHH4u5rvCMd+OTQAg7cnVYoGrWmWspAPrQ4VEoiSrV4D43sJPWXIv7 Dr43ayDFn4C4pEMlKbyV0s6XiUq0EMtem1wlc4h3Y3yOcCnM0jltxMlWHbPHEpKYom wQDKu/IAK71Uv5Z1rKiQbQMzu6VtcnATcJkKTxsg= X-UI-Sender-Class: c548c8c5-30a9-4db5-a2e7-cb6cb037b8f9 Original-Received: from fluss ([80.136.29.179]) by smtp.web.de (mrweb105 [213.165.67.124]) with ESMTPSA (Nemesis) id 1MYcpt-1k2rnI3yhn-00Vtdu; Thu, 10 Sep 2020 19:48:55 +0200 In-reply-to: <4ede1fc86c21609bfb45dd3f33eaa64b7df785ba.camel@gnu.org> X-Provags-ID: V03:K1:QlAGccmW6B/6bRbO86T+V7/1HTZsAaCv2qm3lGtW8WBvXTSa6NM 60ZDGddT4PXt/w8P2bcGS8sc9GorJeLWCAoQj2WlfzCi7pBYGOsn+Ls8zHP+u9xhEveyHlK 7QX4+zoA8wNr3xheoULZaQx5V56RfU4yV0f56g6wsV4VGbhRxtueaPMGdxauoJ4Lxq3mZKR uNF70CNgAvqv2ONugoQJg== X-UI-Out-Filterresults: notjunk:1;V03:K0:7zU+nuNlg/0=:79415G0dSVlS3RmBCbyd7/ WWf4pshYJ4mPtIb3k9xss4bWrqD6OWmomhFQliEi2GUJA+pl/5R7WF37kwU73qwy87Ym+dFOx 2QxIvcGSN1RCc9XphcRVUfPZDJdYzRdzFLbzfDQKQWUyjmRCd/H8XqquVIDArkG5dvfp2iQDq 6qe9ibwI0tI+WwA88HIDVYnvum4Nkkfs0n3QhvBp1TZdeCD3uai5Nx9GrGCnERHJ3GXSIUZw/ iA+LreZB3rK+ckZ5PITLPIle4d66WWfK5KioqQvDp0g2OZkkLyqbwRMfdcge0fBApUd9pP4mL psq1JfnQ5ygaF2u9i2C4v6vRGF5eyAEKEpKT5xIVxWrs4RuKOKEoaqF5hzARNWqbsI6grpY55 ZhKm4/pojkqJ0Syx3HNnsMzBEOqvQDM7afuP5MaoWOX79Osxe21Dij1GCs37dDDmy5nG2zXsl Lx9VfWTDehPZnpEF1hkfc63B4AtrVnsWtX4jKpJvIkIJYSy9TqNJGeb8x171QpUSIQKLFzeB8 dO0LRUdpQYgil0+DUYSZzkMHYV3SLeBHSI5a7WlgmkBQngDHq52MDqWHRoitFKne/ZVgU2vLo 1xn8GhJBKaRRgEfyZ2tphWyd5UrA49w9QpeIHvGEsvrEDpSC8xkLRURLGxRfYLatJTkxTz3B2 I8cL+DJjJR1N8buwZgCqnnWMZHwwTaTp06vPwJFrDjOkQrhhW2jsVzjNRzcjCek3zYTCbxKGK COSBbKqtreiimaJ6/J6xUVUYh6hfO3h5PQnktCSqocAHGh5w3t4xjeFN+wpMb4xt7SoUSEWy Received-SPF: pass client-ip=212.227.17.12; envelope-from=arne_bab@web.de; helo=mout.web.de X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/10 13:48:57 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:16902 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Roel Janssen writes: > On Thu, 2020-09-10 at 04:14 +0300, Bonface M. K. wrote: >> Hi all. Is there a way to "record" the output from both *stderr* and >> *stdout* from a guile process if you wrap it around a "system" call >> to a >> string? Here's something that works. It's not /exactly/ what I want >> because "open-input-pipe" runs the command in a subprocess. >>=20 >> --8<---------------cut here---------------start------------->8--- >> (define (run-job job) >> (let* ((port (open-input-pipe job)) >> (str (read-line port))) >> (close-pipe port) >> str)) >>=20 >> (display (format #t "~s ~s ~s" >> "padding" >> (run-job "echo hello") >> "testing")) >> --8<---------------cut here---------------end--------------->8--- >>=20 >> I've tried out creating a fork: >>=20 >> --8<---------------cut here---------------start------------->8--- >> (define (run-job thunk) >> (call-with-output-string >> (=CE=BB (port) >> (match (pipe) >> ((in . out) >> (match (primitive-fork) >> (0 ; child >> (close in) >> (with-error-to-port out thunk)) >> ((=3D waitpid (pid . exit-code)) ;; parent >> (close out) >> (display (read-line in) port)))))))) >>=20 >> ;; Doesn't work: >> (display (format #t "~s ~s ~s" >> "padding" >> (run-job (system "echo hello")) >> "testing")) >> --8<---------------cut here---------------end--------------->8--- >>=20 >> Ideally for a correct output without errors, I'd like to have as >> output: >>=20 >> --8<---------------cut here---------------start------------->8--- >> "padding" "hello" "testing" >> --8<---------------cut here---------------end--------------->8--- >>=20 >> and in the event I have an error, like say, by running (system >> "echoooo hello"), I get >> the output: >>=20 >> --8<---------------cut here---------------start------------->8--- >> "padding" "sh: command not found" "testing"" >> --8<---------------cut here---------------end--------------->8--- >>=20 >> PS: I'm new to Guile :) >>=20 > > Having spent a day on this myself, this is the best I had come up with: > > --8<---------------cut here---------------start------------->8--- > (define (virtuoso-isql-query query) > "Executes QUERY via ISQL and returns a both an ERROR-PORT and a PORT > to read CSV output from." > (let* ((tmp (getenv "TMPDIR")) > (error-port (mkstemp! (string-append (if tmp tmp "/tmp") "/sg- > XXXXXX"))) > (port (open-input-pipe > (format #f "~a ~a -U ~a -P ~a verbose=3Doff > csv_rfc4180=3Don csv_rfc4180_field_separator=3D, exec=3D'~:a' 2> ~a" > (isql-bin) (isql-port) (rdf-store- > username) > (rdf-store-password) > (string-append "SPARQL " query) > (port-filename error-port))))) > (setvbuf port 'block 4096) > (values error-port port))) > --8<---------------cut here---------------end--------------->8--- > I certainly hope someone can come up with a better solution! I=E2=80=99m not sure whether it=E2=80=99s better, but this is the solution = I found: (import (ice-9 rdelim) (ice-9 popen) (rnrs io ports)) (define (call-command-with-output-error-to-string cmd) (let* ((err-cons (pipe)) (port (with-error-to-port (cdr err-cons) (=CE=BB() (open-input-pipe cmd)))) (_ (setvbuf (car err-cons) 'block=20 (* 1024 1024 16))) (result (read-delimited "" port))) (close-port (cdr err-cons)) (values result (read-delimited "" (car err-cons))))) (call-command-with-output-error-to-string "echo 1; echo 2 >&2") Also available on my website: https://www.draketo.de/software/guile-capture-stdout-stderr.html Best wishes, Arne =2D-=20 Unpolitisch sein hei=C3=9Ft politisch sein ohne es zu merken --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJEBAEBCAAuFiEE801qEjXQSQPNItXAE++NRSQDw+sFAl9aZwYQHGFybmVfYmFi QHdlYi5kZQAKCRAT741FJAPD6070D/4j5HaD5AAqXF9PswMtbHKxmXGgkgGSUd5c 9NbVrk5HOqUWe+dAOs0R1ZgO1Pp4k9wuN7S/Nfl4jl9Z59Fse+J4L8uGe1UqBBHG m8wB7MNl0J9eDuA6byyElj462fFuz6zrWEhv4W+lYIwFJKrZTIe145Dlu+AhTwKj fZBiZ4ht4KkGnlyNJzi9H3W/xMcSTmrf+hZ339cegH7QUTCS1UjleKxYdbmK/peB nz4/OVorR4T5OzQlZnQPYgC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