Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
lisp:捕获stdout和stderr,将其存储在单独的变量中_Lisp_Common Lisp_Sbcl - Fatal编程技术网

lisp:捕获stdout和stderr,将其存储在单独的变量中

lisp:捕获stdout和stderr,将其存储在单独的变量中,lisp,common-lisp,sbcl,Lisp,Common Lisp,Sbcl,我有一个函数,它返回一个值并将数据打印到stdout和stderr。我无法修改此函数。我现在想执行这个函数,捕获打印到stdout和stderr的数据,并将其存储在两个单独的变量中。如果可能,我还希望将函数的返回值存储在第三个变量中 我是accross(输出到字符串(*standard output*)…),但这不允许我同时捕获stdout和stderr。我有哪些选项?您可以使用let将流绑定到输出字符串流。例如: (defun print-stuff (x y) (format t "st

我有一个函数,它返回一个值并将数据打印到stdout和stderr。我无法修改此函数。我现在想执行这个函数,捕获打印到stdout和stderr的数据,并将其存储在两个单独的变量中。如果可能,我还希望将函数的返回值存储在第三个变量中


我是accross
(输出到字符串(*standard output*)…)
,但这不允许我同时捕获stdout和stderr。我有哪些选项?

您可以使用
let
将流绑定到输出字符串流。例如:

(defun print-stuff (x y)
  (format t "standard output ~a" x)
  (format *error-output* "error output ~a" y)
  (+ x y))

(defun capture (x y)
  (let ((*standard-output* (make-string-output-stream))
        (*error-output* (make-string-output-stream)))
    (values (print-stuff x y)
            (get-output-stream-string *standard-output*)
            (get-output-stream-string *error-output*))))


(capture 43 12)
; => 55
;    "standard output 43"
;    "error output 12"

把其中的两个套起来怎么样,每根绳子套一个?这确实管用,但对我来说似乎有点不雅观。还有其他的方法吗?这正是我想要的。非常感谢。有时使用*trace output*,比如(time form)函数。