Skip to content

Commit 4eedbb4

Browse files
committed
Add the format_status/2 optional callback to GenServer
1 parent 26a4fc5 commit 4eedbb4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

lib/elixir/lib/gen_server.ex

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,28 @@ defmodule GenServer do
382382
{:ok, new_state :: term} |
383383
{:error, reason :: term} when old_vsn: term | {:down, term}
384384

385+
@doc """
386+
Invoked in some cases to retrieve a formatted version of the `GenServer` status.
387+
388+
This callback can be useful to control the *appearance* of the status of the
389+
`GenServer`. For example, it can be used to return a compact representation of
390+
the `GenServers`'s state to avoid having large state terms printed.
391+
392+
* one of `:sys.get_status/1` or `:sys.get_status/2` is invoked to get the
393+
status of the `GenServer`; in such cases, `reason` is `:normal`
394+
395+
* the `GenServer` terminates abnormally and logs an error; in such cases,
396+
`reason` is `:terminate`
397+
398+
`pdict_and_state` is a two-elements list `[pdict, state]` where `pdict` is a
399+
list of `{key, value}` tuples representing the current process dictionary of
400+
the `GenServer` and `state` is the current state of the `GenServer`.
401+
"""
402+
@callback format_status(reason, pdict_and_state :: list) ::
403+
term when reason: :normal | :terminate
404+
405+
@optional_callbacks format_status: 2
406+
385407
@typedoc "Return values of `start*` functions"
386408
@type on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term}
387409

0 commit comments

Comments
 (0)