[erlang-questions] [patch] Suggestion for enhancement to sasl_report

Jay Nelson <>
Fri May 16 15:24:45 CEST 2008


I recently tried to replace sasl_report with my own handler, but I  
decided I would like to retain the sasl error formatting.  Much to my  
chagrin I discovered that there is a call to io:format deep below  
write_report/3.

Below is a patch that is backwards compatible.  It adds a new  
function format_report/3 that is identical to write_report/3,  
however, it uses io_lib:format rather than io:format.  This is  
accomplished by adding an internal function write_report_action/4  
which calls either io_lib or io depending on the requested feature.   
Presumably this should be as fast as before because I explicitly set  
the module name and only add an extra function call (which hopefully  
inlines).

jay

-------------------

--- sasl/src/sasl_report.erl.orig       2008-04-20 22:48:50.000000000  
-0700
+++ sasl/src/sasl_report.erl    2008-04-23 08:20:20.000000000 -0700
@@ -17,23 +17,29 @@
  %%
  -module(sasl_report).

--export([write_report/3]).
+-export([write_report/3, format_report/3]).

-write_report(Fd, What, {Time, {error_report, _GL, {Pid, Type,  
Report}}}) ->
+format_report(Fd, What, Report) ->
+    io_report(io_lib, Fd, What, Report).
+
+write_report(Fd, What, Report) ->
+    io_report(io, Fd, What, Report).
+
+io_report(IO, Fd, What, {Time, {error_report, _GL, {Pid, Type,  
Report}}}) ->
      case is_my_error_report(What, Type) of
         true ->
             Head = write_head(Type, Time, Pid),
-           write_report2(Fd, Head, Type, Report);
+           write_report2(IO, Fd, Head, Type, Report);
         _ -> true
      end;
-write_report(Fd, What, {Time, {info_report, _GL, {Pid, Type,  
Report}}}) ->
+io_report(IO, Fd, What, {Time, {info_report, _GL, {Pid, Type,  
Report}}}) ->
      case is_my_info_report(What, Type) of
         true ->
             Head = write_head(Type, Time, Pid),
-           write_report2(Fd, Head, Type, Report);
+           write_report2(IO, Fd, Head, Type, Report);
         _ -> true
      end;
-write_report(_Fd, _, _) ->
+io_report(_IO, _Fd, _, _) ->
      false.

  is_my_error_report(all, Type)   ->  is_my_error_report(Type);
@@ -49,20 +55,26 @@
  is_my_info_report(progress)  -> true;
  is_my_info_report(_)                    -> false.

-write_report2(Fd, Head, supervisor_report, Report) ->
+write_report2(IO, Fd, Head, supervisor_report, Report) ->
      Name = sup_get(supervisor, Report),
      Context = sup_get(errorContext, Report),
      Reason = sup_get(reason, Report),
      Offender = sup_get(offender, Report),
-    io:format(Fd, Head ++ "     Supervisor: ~p~n     Context:     
~p~n     Reason:     "
-             "~80.18p~n     Offender:   ~80.18p~n~n",
-             [Name,Context,Reason,Offender]);
-write_report2(Fd, Head, progress, Report) ->
+    FmtString = "     Supervisor: ~p~n     Context:    ~p~n      
Reason:     "
+       "~80.18p~n     Offender:   ~80.18p~n~n",
+    write_report_action(IO, Fd, Head ++ FmtString,
+                       [Name,Context,Reason,Offender]);
+write_report2(IO, Fd, Head, progress, Report) ->
      Format = format_key_val(Report),
-    io:format(Fd, Head ++ "~s", [Format]);
-write_report2(Fd, Head, crash_report, Report) ->
+    write_report_action(IO, Fd, Head ++ "~s", [Format]);
+write_report2(IO, Fd, Head, crash_report, Report) ->
      Format = proc_lib:format(Report),
-    io:format(Fd, Head ++ "~s", [Format]).
+    write_report_action(IO, Fd, Head ++ "~s", [Format]).
+
+write_report_action(io, Fd, Format, Args) ->
+    io:format(Fd, Format, Args);
+write_report_action(io_lib, _Fd, Format, Args) ->
+    io_lib:format(Format, Args).

  format_key_val([{Tag,Data}|Rep]) ->
      io_lib:format("    ~16w: ~p~n",[Tag,Data]) ++ format_key_val(Rep);





More information about the erlang-questions mailing list