Better pid handling and conversion
factored out some logic from recon:info/1 into recon_lib:term_to_pid
and allowed arbitrary terms to be used for pids in recon:get_state/1.
diff --git a/README.md b/README.md
index 11a338e..97053cd 100644
--- a/README.md
+++ b/README.md
@@ -9,3 +9,8 @@
Documentation for the library can be obtained at http://ferd.github.io/recon/
+Changelog
+---------
+
+- 0.3.1: factored out some logic from `recon:info/1` into `recon_lib:term_to_pid`
+ and allowed arbitrary terms to be used for pids in `recon:get_state/1`.
diff --git a/src/recon.app.src b/src/recon.app.src
index 5ae63e0..107fce2 100644
--- a/src/recon.app.src
+++ b/src/recon.app.src
@@ -1,6 +1,6 @@
{application, recon,
[{description, "Diagnostic tools for production use"},
- {vsn, "0.3.0"},
+ {vsn, "0.3.1"},
{modules, [recon]},
{registered, []},
{applications, [kernel, stdlib]}]}.
diff --git a/src/recon.erl b/src/recon.erl
index 32c01b0..5cf4049 100644
--- a/src/recon.erl
+++ b/src/recon.erl
@@ -94,7 +94,11 @@
Attr::_,
[{atom(), term()}]}.
--export_type([proc_attrs/0, inet_attrs/0]).
+-type pid_term() :: pid() | atom() | string()
+ | {global, term()} | {via, module(), term()}
+ | {non_neg_integer(), non_neg_integer(), non_neg_integer()}.
+
+-export_type([proc_attrs/0, inet_attrs/0, pid_term/0]).
%%%%%%%%%%%%%%%%%%
%%% PUBLIC API %%%
%%%%%%%%%%%%%%%%%%
@@ -117,25 +121,16 @@
%% registered locally (an atom), globally (`{global, Name}'), or through
%% another registry supported in the `{via, Module, Name}' syntax (must have a
%% `Module:whereis_name/1' function). Pids can also be passed in as a string
-%% (`"<0.39.0>"') and will be converted to be used.
--spec info(Name) -> [{Type, [{Key, Value}]},...] when
- Name :: pid() | atom() | string()
- | {global, term()} | {via, module(), term()},
+%% (`"<0.39.0>"') or a triple (`{0,39,0}') and will be converted to be used.
+-spec info(pid_term()) -> [{Type, [{Key, Value}]},...] when
Type :: meta | signals | location | memory | work,
Key :: registered_name | dictionary | group_leader | status
| links | monitors | monitored_by | trap_exit | initial_call
| current_stacktrace | memory | message_queue_len | heap_size
| total_heap_size | garbage_collection | reductions,
Value :: term().
-info(Name) when is_atom(Name) ->
- info(whereis(Name));
-info(List = "<0."++_) ->
- info(list_to_pid(List));
-info({global, Name}) ->
- info(global:whereis_name(Name));
-info({via, Module, Name}) ->
- info(Module:whereis_name(Name));
-info(Pid) when is_pid(Pid) ->
+info(PidTerm) ->
+ Pid = recon_lib:term_to_pid(PidTerm),
Info = fun(List) -> erlang:process_info(Pid, List) end,
[{meta, Info([registered_name, dictionary, group_leader, status])},
{signals, Info([links, monitors, monitored_by, trap_exit])},
@@ -303,10 +298,10 @@
%% @doc Fetch the internal state of an OTP process.
%% Calls `sys:get_state/1' directly in R16B01+, and fetches
%% it dynamically on older versions of OTP.
--spec get_state(Name) -> term() when
- Name :: pid() | atom() | {global, term()} | {via, module(), term()}.
-get_state(Proc) ->
- try
+-spec get_state(pid_term()) -> term().
+get_state(PidTerm) ->
+ Proc = recon_lib:term_to_pid(PidTerm),
+ try
sys:get_state(Proc)
catch
error:undef ->
diff --git a/src/recon_lib.erl b/src/recon_lib.erl
index 394e7c6..56378af 100644
--- a/src/recon_lib.erl
+++ b/src/recon_lib.erl
@@ -3,7 +3,7 @@
port_list/1, port_list/2,
proc_attrs/1, proc_attrs/2,
inet_attrs/1, inet_attrs/2,
- triple_to_pid/3,
+ triple_to_pid/3, term_to_pid/1,
time_map/5, time_fold/6]).
-type diff() :: [recon:proc_attrs() | recon:inet_attrs()].
@@ -115,6 +115,16 @@
integer_to_list(Y) ++ "." ++
integer_to_list(Z) ++ ">").
+%% @doc Transforms a given term to a pid.
+-spec term_to_pid(recon:pid_term()) -> pid().
+term_to_pid(Pid) when is_pid(Pid) -> Pid;
+term_to_pid(Name) when is_atom(Name) -> whereis(Name);
+term_to_pid(List = "<0."++_) -> list_to_pid(List);
+term_to_pid({global, Name}) -> global:whereis_name(Name);
+term_to_pid({via, Module, Name}) -> Module:whereis_name(Name);
+term_to_pid({X,Y,Z}) when is_integer(X), is_integer(Y), is_integer(Z) ->
+ triple_to_pid(X,Y,Z).
+
%% @doc Calls a given function every `Interval' milliseconds and supports
%% a map-like interface (each result is modified and returned)
-spec time_map(N, Interval, Fun, State, MapFun) -> [term()] when