相关疑难解决方法(0)

Haskell:System.Process合并stdout和stderr

我想从haskell程序中调用一个进程并捕获stdout以及stderr.

我所做的:

(_, stdout, stderr) <- readProcessWithExitCode "command" [] ""
Run Code Online (Sandbox Code Playgroud)

问题:这样,stdout和stderr被单独捕获,但是我希望消息出现在正确的位置(否则我只是stdout ++ stderr将错误消息与其stdout对应物分开).

我知道如果我将输出传输到文件中,我就可以实现这一点,即

tmp <- openFile "temp.file" ...
createProcess (proc "command" []) { stdout = UseHandle tmp,
                                    stderr = UseHandle tmp }
Run Code Online (Sandbox Code Playgroud)

因此,我目前的解决方法是将输出管道传输到临时文件并重新读取.但是我正在寻找更直接的方法.

如果我在unix上肯定我只是调用一个shell命令ála

command 2>&1
Run Code Online (Sandbox Code Playgroud)

就是这样.但是,我想让它尽可能便携.

我需要这个:我已经构建了一个小的haskell cgi脚本(只是为了玩它),它调用某个程序并打印输出.我想html-escape输出,因此我不能简单地将它传递给stdout.

我在想:也许可以创建一个内存中的句柄,比如Java中的PipedInputStream/PipedOutputStream,或ArrayInputStream/ArrayOutputStream,它允许在内存中处理IO流.我环顾四周寻找一个函数:: Handle,但没有找到任何东西.

也许还有另一个Haskell模块允许我合并两个流?

haskell pipe process

9
推荐指数
2
解决办法
1180
查看次数

标签 统计

haskell ×1

pipe ×1

process ×1