Skip to content

Instantly share code, notes, and snippets.

@rlb3
Last active August 26, 2022 01:36
Show Gist options
  • Save rlb3/a096141d8821a7572f835cbbdfb0fad2 to your computer and use it in GitHub Desktop.
Save rlb3/a096141d8821a7572f835cbbdfb0fad2 to your computer and use it in GitHub Desktop.
In iex redirect stdio to file.
defmodule Console.Helpers do
defmacro capture_io(filename, do: block) do
quote do
filename = unquote(filename)
current_leader = Process.group_leader()
{:ok, fh} = File.open(filename, [:write])
Process.group_leader(self(), fh)
unquote(block)
Process.group_leader(self(), current_leader)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment