[ANN] out-channel-redirect 0.1 — Redirect and capture out_channel output

I’m happy to announce the first release of out-channel-redirect, a library for redirecting and capturing output written to out_channels.

Features:

  • Capture any out_channel into a string (with convenience shorthands for stdout and stderr)
  • Redirect one channel into another
  • Works on native (via dup/dup2), JavaScript (js_of_ocaml), and WebAssembly (wasm_of_ocaml)
  • Expert API for manual redirection lifetime control

Installation:

opam install out-channel-redirect

Quick example:

let output, result =
  Out_channel_redirect.capture_channel some_channel ~f:(fun () → 
    Printf.fprintf some_channel “hello”;
    42)
(* output = “hello”, result = 42 *)
6 Likes

Hello,

is it possible to have also an echo of what is captured ? Something like a ‘tee’ functionality.

Thanks.

2 Likes

A function that captures stdout and stderr and returns them as two strings would also be nice. See Idiomatic calling other process with stdin, stdout, stderr stdlib for an example of use.

1 Like