[erlang-patches] format_status analog for error_info

Steve Vinoski vinoski@REDACTED
Thu Feb 25 08:25:17 CET 2010


On Wed, Feb 24, 2010 at 5:59 PM, Andrew Thompson <andrew@REDACTED> wrote:

> On Wed, Feb 24, 2010 at 05:22:48PM -0500, Steve Vinoski wrote:
> >
> > I think since format_status is already an optional export, reusing it for
> > the purposes you mention would make sense. Probably not much point in
> > inventing another mechanism to do something similar, and overloading
> > existing functions (like terminate) has a chance of breaking something.
> > Since format_status is new (again! :-) ) the chance of breaking anything
> by
> > reusing it for this is pretty much nil.
> >
>
> The reason I was a little leery of format_status was that its return
> value is a little odd to my eyes, is that format standardized at all or
> can it be more or less freeform (even missing the "State" key)?
>

The default for gen_server (for example) is [{data, [{"State", State}]}],
but it really can be anything you want. The unit test, for example, just
returns [format_status_called]. Note that this freeform nature fits really
well with how error_info works too.


> Using format_status for this has the happy side effect of ensuring that
> neither sys:get_status or an error log message will choke and die on
> trying to prettyprint a gigantic term. Maybe we could call format_status
> with a different first argument to indicate that the status is for an
> error, not for normal operation?
>

Sure, I think that would work. Only sys.erl currently initiates a call to
format_status, so only the atom 'normal' is currently ever passed. A
possible patch for gen_server.erl, for example, to reuse format_status for
error printing might look like this (this is just an example, not intended
an official patch submission):

diff --git a/lib/stdlib/src/gen_server.erl b/lib/stdlib/src/gen_server.erl
index f1a9a31..f4ab898 100644
--- a/lib/stdlib/src/gen_server.erl
+++ b/lib/stdlib/src/gen_server.erl
@@ -705,7 +705,18 @@ terminate(Reason, Name, Msg, Mod, State, Debug) ->
                {shutdown,_}=Shutdown ->
                    exit(Shutdown);
                _ ->
-                   error_info(Reason, Name, Msg, State, Debug),
+                    NState =
+                        case erlang:function_exported(Mod, format_status,
2) of
+                            true ->
+                                FmtArgs = [get(), State],
+                                case catch Mod:format_status(error,
FmtArgs) of
+                                    {'EXIT', _} -> State;
+                                    Else -> Else
+                                end;
+                            _ ->
+                                State
+                        end,
+                   error_info(Reason, Name, Msg, NState, Debug),
                    exit(Reason)
            end
     end.

I think this could work pretty well. Shall I work up an official patch?

--steve


More information about the erlang-patches mailing list