From 4eedbb4e4c6c3408afc9c5b704bab331b43f397f Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Sat, 28 May 2016 15:15:08 +0200 Subject: [PATCH 1/2] Add the format_status/2 optional callback to GenServer --- lib/elixir/lib/gen_server.ex | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/elixir/lib/gen_server.ex b/lib/elixir/lib/gen_server.ex index 8e3eea48fb3..9b835728d2a 100644 --- a/lib/elixir/lib/gen_server.ex +++ b/lib/elixir/lib/gen_server.ex @@ -382,6 +382,28 @@ defmodule GenServer do {:ok, new_state :: term} | {:error, reason :: term} when old_vsn: term | {:down, term} + @doc """ + Invoked in some cases to retrieve a formatted version of the `GenServer` status. + + This callback can be useful to control the *appearance* of the status of the + `GenServer`. For example, it can be used to return a compact representation of + the `GenServers`'s state to avoid having large state terms printed. + + * one of `:sys.get_status/1` or `:sys.get_status/2` is invoked to get the + status of the `GenServer`; in such cases, `reason` is `:normal` + + * the `GenServer` terminates abnormally and logs an error; in such cases, + `reason` is `:terminate` + + `pdict_and_state` is a two-elements list `[pdict, state]` where `pdict` is a + list of `{key, value}` tuples representing the current process dictionary of + the `GenServer` and `state` is the current state of the `GenServer`. + """ + @callback format_status(reason, pdict_and_state :: list) :: + term when reason: :normal | :terminate + + @optional_callbacks format_status: 2 + @typedoc "Return values of `start*` functions" @type on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term} From d0879c499f6cc2660e49a64678fe41d826465fb9 Mon Sep 17 00:00:00 2001 From: Andrea Leopardi Date: Sat, 28 May 2016 15:15:15 +0200 Subject: [PATCH 2/2] Use GenServer over :gen_server as the behaviour for GenServers We can do this now since we support @optional_callbacks and can thus have the GenServer.format_status/2 optional callback. --- lib/elixir/lib/gen_server.ex | 2 +- lib/elixir/test/elixir/module_test.exs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/elixir/lib/gen_server.ex b/lib/elixir/lib/gen_server.ex index 9b835728d2a..aaceeec488b 100644 --- a/lib/elixir/lib/gen_server.ex +++ b/lib/elixir/lib/gen_server.ex @@ -436,7 +436,7 @@ defmodule GenServer do @doc false defmacro __using__(_) do quote location: :keep do - @behaviour :gen_server + @behaviour GenServer @doc false def init(args) do diff --git a/lib/elixir/test/elixir/module_test.exs b/lib/elixir/test/elixir/module_test.exs index 15b69b2009b..2bd7155140c 100644 --- a/lib/elixir/test/elixir/module_test.exs +++ b/lib/elixir/test/elixir/module_test.exs @@ -122,7 +122,7 @@ defmodule ModuleTest do ## Attributes test "reserved attributes" do - assert List.keyfind(ExUnit.Server.__info__(:attributes), :behaviour, 0) == {:behaviour, [:gen_server]} + assert List.keyfind(ExUnit.Server.__info__(:attributes), :behaviour, 0) == {:behaviour, [GenServer]} end test "registered attributes" do