OzMcp.Transport.Stdio.Console (OzMcp v0.9.0)

Copy Markdown View Source

Puts the standard input and output of the process in a shape the stdio transport can use.

A server on stdio must write JSON-RPC messages to stdout and nothing else: one log line or one stray IO.puts/1 breaks the protocol of the client. open/0 returns the two functions the transport takes as its :read and :write options and prepares the environment: the encoding of the standard IO device becomes UTF-8, the default handler of the Logger moves to stderr and the group leader of the calling process moves to the standard error device, so every process that starts later writes its stray output to stderr too.

Call it once, first thing, in the process that starts the transport.

Summary

Functions

The function the transport takes as its :read option: one line of stdin per call, as {:ok, line}, :eof, or {:error, reason}.

Moves the default handler of the Logger to stderr and keeps its level, formatter and filters. Does nothing when there is no default handler.

The function the transport takes as its :write option: it writes the message to stdout, :ok, or {:error, reason} when the write fails.

Functions

open()

@spec open() :: %{
  read: (-> {:ok, binary()} | :eof | {:error, term()}),
  write: (iodata() -> :ok | {:error, term()})
}

reader(device)

@spec reader(device :: pid() | atom()) :: (-> {:ok, binary()}
                                        | :eof
                                        | {:error, term()})

The function the transport takes as its :read option: one line of stdin per call, as {:ok, line}, :eof, or {:error, reason}.

redirect_logger()

@spec redirect_logger() :: :ok

Moves the default handler of the Logger to stderr and keeps its level, formatter and filters. Does nothing when there is no default handler.

writer(device)

@spec writer(device :: pid() | atom()) :: (iodata() -> :ok | {:error, term()})

The function the transport takes as its :write option: it writes the message to stdout, :ok, or {:error, reason} when the write fails.