diff options
author | Fredrik Gustafsson <fredrik@erlang.org> | 2013-08-23 18:13:19 +0200 |
---|---|---|
committer | Fredrik Gustafsson <fredrik@erlang.org> | 2013-08-23 18:13:19 +0200 |
commit | 3a10fa99f6744b0a4600cf0b966af5a0a678da8f (patch) | |
tree | a0ade9bd1b83ac38c0f665f53670cdb06dceb0da | |
parent | 076dff38d5aa6fd303b5c2a3aa809115c0545164 (diff) | |
parent | 14f1d3037fff02118b8a3d4bb8a86c3df2b24077 (diff) | |
download | otp-3a10fa99f6744b0a4600cf0b966af5a0a678da8f.tar.gz otp-3a10fa99f6744b0a4600cf0b966af5a0a678da8f.tar.bz2 otp-3a10fa99f6744b0a4600cf0b966af5a0a678da8f.zip |
Merge branch 'epa/add_fd_usage_in_rb/OTP-11252' into maint
* epa/add_fd_usage_in_rb/OTP-11252:
kernel: sasl: fixed rb.xml docs
Add Fd usage in rb logging
-rw-r--r-- | lib/sasl/doc/src/rb.xml | 34 | ||||
-rw-r--r-- | lib/sasl/src/rb.erl | 56 | ||||
-rw-r--r-- | lib/sasl/test/rb_SUITE.erl | 32 |
3 files changed, 96 insertions, 26 deletions
diff --git a/lib/sasl/doc/src/rb.xml b/lib/sasl/doc/src/rb.xml index 3da825878e..b94914d8f9 100644 --- a/lib/sasl/doc/src/rb.xml +++ b/lib/sasl/doc/src/rb.xml @@ -138,6 +138,24 @@ </desc> </func> <func> + <name>log_list()</name> + <name>log_list(Type)</name> + <fsummary>Log reports list</fsummary> + <type> + <v>Type = type()</v> + <v>type() = error | error_report | info_msg | info_report | + warning_msg | warning_report | crash_report | + supervisor_report | progress</v> + </type> + <desc> + <p>Same as <c>list/0</c> or <c>list/1</c> functions + but result is printed to logfile, if set, otherwise to standard_io. + </p> + <p>If no <c>Type</c> is given, all reports are listed. + </p> + </desc> + </func> + <func> <name>rescan()</name> <name>rescan(Options)</name> <fsummary>Rescan the report directory</fsummary> @@ -172,7 +190,7 @@ <type> <v>Options = [opt()]</v> <v>opt() = {start_log, FileName} | {max, MaxNoOfReports} | {report_dir, DirString} | {type, ReportType} | {abort_on_error, Bool}</v> - <v>FileName = string() | standard_io</v> + <v>FileName = string() | atom() | pid()</v> <v>MaxNoOfReports = int() | all</v> <v>DirString = string()</v> <v>ReportType = type() | [type()] | all</v> @@ -185,11 +203,13 @@ reports can be browsed. When the <c>rb_server</c> is started, the files in the specified directory are scanned. The other functions assume that the server has - started. + started. </p> - <p><c>{start_log, FileName}</c> starts logging to file. All - reports will be printed to the named file. The default is - <c>standard_io</c>. + <p><c>{start_log, FileName}</c> starts logging to file, + registered name or io_device. All reports will be printed + to the named file. The default is <c>standard_io</c>. + The option {start_log, standard_error} is not allowed and + will be replaced by default standard_io. </p> <p><c>{max, MaxNoOfReports}</c>. Controls how many reports the <c>rb_server</c> should read on start-up. This option is @@ -226,11 +246,11 @@ <name>start_log(FileName)</name> <fsummary>Redirect all output to <c>FileName</c></fsummary> <type> - <v>FileName = string()</v> + <v>FileName = string() | atom() | pid()</v> </type> <desc> <p>Redirects all report output from the RB tool to the - specified file. + specified file, registered name or io_device. </p> </desc> </func> diff --git a/lib/sasl/src/rb.erl b/lib/sasl/src/rb.erl index 8004ef2c5a..767932e659 100644 --- a/lib/sasl/src/rb.erl +++ b/lib/sasl/src/rb.erl @@ -22,7 +22,7 @@ %% External exports -export([start/0, start/1, stop/0, rescan/0, rescan/1]). --export([list/0, list/1, show/0, show/1, grep/1, filter/1, filter/2, start_log/1, stop_log/0]). +-export([list/0, list/1, log_list/0, log_list/1, show/0, show/1, grep/1, filter/1, filter/2, start_log/1, stop_log/0]). -export([h/0, help/0]). %% Internal exports @@ -62,6 +62,9 @@ rescan(Options) -> list() -> list(all). list(Type) -> call({list, Type}). +log_list() -> log_list(all). +log_list(Type) -> call({log_list, Type}). + show() -> call(show). @@ -93,6 +96,8 @@ help() -> io:format("rb:help() - print this help~n"), io:format("rb:list() - list all reports~n"), io:format("rb:list(Type) - list all reports of type Type~n"), + io:format("rb:log_list() - log list of all reports~n"), + io:format("rb:log_list(Type) - log list of all reports of type Type~n"), io:format(" currently supported types are:~n"), print_types(), io:format("rb:grep(RegExp) - print reports containing RegExp.~n"), @@ -113,7 +118,7 @@ help() -> io:format("rb:show(Number) - print report no Number~n"), io:format("rb:show(Type) - print all reports of type Type~n"), io:format("rb:show() - print all reports~n"), - io:format("rb:start_log(File) - redirect all reports to file~n"), + io:format("rb:start_log(File) - redirect all reports to file or io_device~n"), io:format("rb:stop_log() - close the log file and redirect to~n"), io:format(" standard_io~n"), io:format("rb:stop - stop the rb_server~n"). @@ -207,7 +212,10 @@ handle_call({rescan, Options}, _From, State) -> handle_call(_, _From, #state{data = undefined}) -> {reply, {error, no_data}, #state{}}; handle_call({list, Type}, _From, State) -> - print_list(State#state.data, Type), + print_list(standard_io, State#state.data, Type), + {reply, ok, State}; +handle_call({log_list, Type}, _From, State) -> + print_list(State#state.device, State#state.data, Type), {reply, ok, State}; handle_call({start_log, FileName}, _From, State) -> NewDevice = open_log_file(FileName), @@ -262,7 +270,16 @@ code_change(_OldVsn, State, _Extra) -> %% Returns: A Device for later use in call to io:format %%----------------------------------------------------------------- open_log_file(standard_io) -> standard_io; -open_log_file(FileName) -> +open_log_file(Fd) when is_atom(Fd),Fd=/=standard_error -> + case whereis(Fd) of + undefined -> io:format("rb: Registered name not found '~s'.~n", + [Fd]), + io:format("rb: Using standard_io~n"), + open_log_file(standard_io); + Pid -> open_log_file(Pid) + end; +open_log_file(Fd) when is_pid(Fd)-> Fd; +open_log_file(FileName) when is_list(FileName) -> case file:open(FileName, [write,append]) of {ok, Fd} -> Fd; Error -> @@ -270,7 +287,10 @@ open_log_file(FileName) -> [FileName, Error]), io:format("rb: Using standard_io~n"), standard_io - end. + end; +open_log_file(standard_error) -> + io:format("rb: Using standard_io~n"), + standard_io. close_device(Fd) when is_pid(Fd) -> catch file:close(Fd); @@ -550,18 +570,18 @@ local_time_to_universal_time({Date,Time}) -> end. -print_list(Data, Type) -> +print_list(Fd, Data, Type) -> Header = {"No", "Type", "Process", "Date Time"}, Width = find_width([Header | Data], 0)+1, DateWidth = find_date_width([Header | Data], 0) +1, Format = lists:concat(["~4s~20s ~", Width, "s~20s~n"]), - io:format(Format, tuple_to_list(Header)), - io:format(Format, ["==", "====", "=======", "==== ===="]), - print_list(Data, Type, Width, DateWidth). -print_list([], _, _, _) -> true; -print_list([H|T], Type, Width, DateWidth) -> - print_one_report(H, Type, Width, DateWidth), - print_list(T, Type, Width, DateWidth). + io:format(Fd, Format, tuple_to_list(Header)), + io:format(Fd, Format, ["==", "====", "=======", "==== ===="]), + print_list(Fd, Data, Type, Width, DateWidth). +print_list(_, [], _, _, _) -> true; +print_list(Fd, [H|T], Type, Width, DateWidth) -> + print_one_report(Fd, H, Type, Width, DateWidth), + print_list(Fd, T, Type, Width, DateWidth). find_width([], Width) -> Width; find_width([H|T], Width) -> @@ -578,22 +598,22 @@ find_date_width([H|T], Width) -> true -> find_date_width(T, Width) end. -print_one_report({No, RealType, ShortDescr, Date, _Fname, _FilePos}, +print_one_report(Fd, {No, RealType, ShortDescr, Date, _Fname, _FilePos}, WantedType, Width, DateWidth) -> if WantedType == all -> - print_short_descr(No, RealType, ShortDescr, Date, Width, + print_short_descr(Fd, No, RealType, ShortDescr, Date, Width, DateWidth); WantedType == RealType -> - print_short_descr(No, RealType, ShortDescr, Date, Width, + print_short_descr(Fd, No, RealType, ShortDescr, Date, Width, DateWidth); true -> ok end. -print_short_descr(No, Type, ShortDescr, Date, Width, DateWidth) -> +print_short_descr(Fd, No, Type, ShortDescr, Date, Width, DateWidth) -> Format = lists:concat(["~4w~20w ~", Width, "s~", DateWidth,"s~n"]), - io:format(Format, [No, + io:format(Fd, Format, [No, Type, io_lib:format("~s", [ShortDescr]), Date]). diff --git a/lib/sasl/test/rb_SUITE.erl b/lib/sasl/test/rb_SUITE.erl index b0e43be3a2..453f992850 100644 --- a/lib/sasl/test/rb_SUITE.erl +++ b/lib/sasl/test/rb_SUITE.erl @@ -362,18 +362,48 @@ start_stop_log(Config) -> StdioResult = [_|_] = capture(fun() -> rb:show(1) end), {ok,<<>>} = file:read_file(OutFile), - %% Start log and check that show is printed to log and not to standad_io + %% Start log and check that show is printed to log and not to standard_io ok = rb:start_log(OutFile), [] = capture(fun() -> rb:show(1) end), {ok,Bin} = file:read_file(OutFile), true = (Bin =/= <<>>), + %% Start log with atom standard_io and check that show is printed to standard_io + ok = rb:stop_log(), + ok = file:write_file(OutFile,[]), + ok = rb:start_log(standard_io), + StdioResult = [_|_] = capture(fun() -> rb:show(1) end), + {ok,<<>>} = file:read_file(OutFile), + + %% Start log and check that show is printed to iodevice log and not to standard_io + ok = rb:stop_log(), + ok = file:write_file(OutFile,[]), + {ok, IoOutFile} = file:open(OutFile,[write]), + ok = rb:start_log(IoOutFile), + [] = capture(fun() -> rb:show(1) end), + {ok,Bin} = file:read_file(OutFile), + true = (Bin =/= <<>>), + ok = file:close(IoOutFile), + %% Stop log and check that show is printed to standard_io and not to log ok = rb:stop_log(), ok = file:write_file(OutFile,[]), StdioResult = capture(fun() -> rb:show(1) end), {ok,<<>>} = file:read_file(OutFile), + %% Start log and check that list is printed to log and not to standard_io + ok = file:write_file(OutFile,[]), + ok = rb:start_log(OutFile), + [] = capture(fun() -> rb:log_list() end), + {ok,Bin2} = file:read_file(OutFile), + true = (Bin2 =/= <<>>), + + %% Stop log and check that list is printed to standard_io and not to log + ok = rb:stop_log(), + ok = file:write_file(OutFile,[]), + StdioResult2 = capture(fun() -> rb:log_list() end), + {ok,<<>>} = file:read_file(OutFile), + %% Test that standard_io is used if log file can not be opened ok = rb:start_log(filename:join(nonexistingdir,"newfile.txt")), StdioResult = capture(fun() -> rb:show(1) end), |