package out-channel-redirect

  1. Overview
  2. Docs
Redirect and capture output written to out_channels

Install

dune-project
 Dependency

Authors

Maintainers

Sources

out-channel-redirect-0.2.tbz
sha256=ad427e8dd36fe18347cc4a574af17777e0650f5585345fab582ceefb16ee219e
sha512=3e186e5f9b40979f4909d772eb6475ed587f997ad392c85b62c8c1f81a89cd20d44624832b82939ca2833a560c3244123a7d33a66581c94243ff8eef69a96640

Description

An OCaml library for redirecting and capturing output written to out_channels, with support for native, JavaScript (js_of_ocaml), and WebAssembly (wasm_of_ocaml) targets.

Added to opam-repository:

README

out-channel-redirect

An OCaml library for redirecting and capturing output written to out_channels, with support for native, JavaScript (js_of_ocaml), and WebAssembly (wasm_of_ocaml) targets.

On native, it works by redirecting file descriptors at the OS level (via dup/dup2), so it captures output from OCaml code as well as C stubs and foreign calls. On JavaScript and WebAssembly targets, it redirects at the channel level using the runtime's channel primitives.

Installation

opam install out-channel-redirect

Usage

(* Capture an out_channel into a string *)
let output, result = Out_channel_redirect.capture_channel stdout ~f:(fun () ->
    print_endline "hello";
    42)
(* output = "hello\n", result = 42 *)

(* Convenience shorthands for stdout, stderr, or both *)
let output, result = Out_channel_redirect.capture_stdout ~f:(fun () -> print_endline "hello"; 42)
let output, result = Out_channel_redirect.capture_stderr ~f:(fun () -> prerr_endline "hello"; 42)
let output, result = Out_channel_redirect.capture_outputs_interleaved ~f:(fun () ->
    print_endline "hello"; prerr_endline "world"; 42)

Redirecting a channel into another

redirect redirects a channel into another without capturing to a string:

Out_channel_redirect.redirect stderr ~into:stdout ~f:(fun () ->
    prerr_endline "this goes to stdout now")

Expert API

For manual control over redirection lifetime:

let redir = Out_channel_redirect.Expert.redirect ~into:stdout stderr in
prerr_endline "redirected to stdout";
Out_channel_redirect.Expert.stop redir

API

val redirect : out_channel -> into:out_channel -> f:(unit -> 'a) -> 'a

val capture_channel : out_channel -> f:(unit -> 'a) -> string * 'a
val capture_channels : out_channel list -> f:(unit -> 'a) -> string list * 'a
val capture_channels_interleaved :
  out_channel list -> f:(unit -> 'a) -> string * 'a

val capture_stdout : f:(unit -> 'a) -> string * 'a
val capture_stderr : f:(unit -> 'a) -> string * 'a
val capture_outputs : f:(unit -> 'a) -> string * string * 'a
val capture_outputs_interleaved : f:(unit -> 'a) -> string * 'a
val capture : f:(unit -> 'a) -> string * 'a
(* alias for capture_outputs_interleaved *)

module Expert : sig
  type redirection
  val redirect : into:out_channel -> out_channel -> redirection
  val stop : redirection -> unit
end

Requirements

  • OCaml >= 4.11
  • Dune >= 3.17

License

MIT

Dependencies (2)

  1. ocaml >= "4.11"
  2. dune >= "3.17"

Dev Dependencies (3)

  1. odoc with-doc
  2. js_of_ocaml with-test
  3. ocamlformat = "0.28.1" & with-dev-setup

Used by

None

Conflicts

None