138
141
Dir = get_report_dir(Options),
139
142
Max = get_option(Options, max, all),
140
143
Type = get_option(Options, type, all),
144
Abort = get_option(Options, abort_on_error, false),
141
145
Data = scan_files(Dir ++ "/", Max, Type),
142
146
{ok, #state{dir = Dir ++ "/", data = Data, device = Device,
143
max = Max, type = Type}}.
147
max = Max, type = Type, abort = Abort, log = Log}}.
145
%% (All those 'catch' are probably unnecessary now that we catch the
146
%% formatting in read_rep/4.)
147
149
handle_call({rescan, Options}, _From, State) ->
149
151
case get_option(Options, start_log, {undefined}) of
150
{undefined} -> State#state.device;
153
{State#state.device,State#state.log};
152
155
close_device(State#state.device),
156
{open_log_file(Log),Log}
155
158
Max = get_option(Options, max, State#state.max),
156
159
Type = get_option(Options, type, State#state.type),
160
Abort = get_option(Options, abort_on_error, false),
157
161
Data = scan_files(State#state.dir, Max, Type),
158
162
NewState = State#state{data = Data, max = Max, type = Type,
163
device = Device, abort = Abort, log = Log1},
160
164
{reply, ok, NewState};
161
165
handle_call(stop, _From, State) ->
162
166
{stop, normal, stopped, State};
172
176
close_device(State#state.device),
173
177
{reply, ok, State#state{device = standard_io}};
174
178
handle_call({show_number, Number}, _From, State) ->
175
#state{dir = Dir, data = Data, device = Device} = State,
176
catch print_report(Dir, Data, Number, Device),
179
#state{dir = Dir, data = Data, device = Device, abort = Abort, log = Log} = State,
180
NewDevice = print_report_by_num(Dir, Data, Number, Device, Abort, Log),
181
{reply, ok, State#state{device = NewDevice}};
178
182
handle_call({show_type, Type}, _From, State) ->
179
#state{dir = Dir, data = Data, device = Device} = State,
180
catch print_typed_reports(Dir, Data, Type, Device),
183
#state{dir = Dir, data = Data, device = Device, abort = Abort, log = Log} = State,
184
NewDevice = print_typed_reports(Dir, Data, Type, Device, Abort, Log),
185
{reply, ok, State#state{device = NewDevice}};
182
186
handle_call(show, _From, State) ->
183
#state{dir = Dir, data = Data, device = Device} = State,
184
catch print_all_reports(Dir, Data, Device),
187
#state{dir = Dir, data = Data, device = Device, abort = Abort, log = Log} = State,
188
NewDevice = print_all_reports(Dir, Data, Device, Abort, Log),
189
{reply, ok, State#state{device = NewDevice}};
186
190
handle_call({grep, RegExp}, _From, State) ->
187
#state{dir = Dir, data = Data, device = Device} = State,
188
catch print_grep_reports(Dir, Data, RegExp, Device),
191
#state{dir = Dir, data = Data, device = Device, abort = Abort, log = Log} = State,
192
NewDevice = print_grep_reports(Dir, Data, RegExp, Device, Abort, Log),
193
{reply, ok, State#state{device = NewDevice}}.
191
195
terminate(_Reason, #state{device = Device}) ->
192
196
close_device(Device).
529
541
io_lib:format("~s", [ShortDescr]),
532
print_typed_reports(Dir, [], Type, Device) ->
534
print_typed_reports(Dir, Data, Type, Device) ->
535
case element(2, hd(Data)) of
536
Type -> print_report(Dir, Data, element(1, hd(Data)), Device);
539
print_typed_reports(Dir, tl(Data), Type, Device).
541
print_all_reports(Dir, [], Device) ->
543
print_all_reports(Dir, Data, Device) ->
544
print_report(Dir, Data, element(1, hd(Data)), Device),
545
print_all_reports(Dir, tl(Data), Device).
547
print_report(Dir, Data, Number, Device) ->
548
{Fname, FilePosition, Type} = find_report(Data, Number),
549
FileName = lists:concat([Dir, Fname]),
550
case file:open(FileName, read) of
551
{ok, Fd} -> read_rep(Fd, FilePosition, Type, Device);
552
_ -> io:format("rb: can't open file ~p~n", [Fname])
555
find_report([{No, Type, ShortDescr, Date, Fname, FilePosition}|T], No) ->
556
{Fname, FilePosition, Type};
557
find_report([H|T], No) -> find_report(T, No);
544
print_report_by_num(Dir, Data, Number, Device, Abort, Log) ->
545
{_,Device1} = print_report(Dir, Data, Number, Device, Abort, Log),
548
print_typed_reports(_Dir, [], _Type, Device, _Abort, _Log) ->
550
print_typed_reports(Dir, Data, Type, Device, Abort, Log) ->
552
case element(2, hd(Data)) of
554
print_report(Dir, Data, element(1, hd(Data)), Device, Abort, Log);
561
print_typed_reports(Dir, tl(Data), Type, Device1, Abort, Log)
564
print_all_reports(_Dir, [], Device, _Abort, _Log) ->
566
print_all_reports(Dir, Data, Device, Abort, Log) ->
567
{Next,Device1} = print_report(Dir, Data, element(1, hd(Data)),
572
print_all_reports(Dir, tl(Data), Device1, Abort, Log)
575
print_report(Dir, Data, Number, Device, Abort, Log) ->
576
case find_report(Data, Number) of
577
{Fname, FilePosition} ->
578
FileName = lists:concat([Dir, Fname]),
579
case file:open(FileName, read) of
581
read_rep(Fd, FilePosition, Device, Abort, Log);
583
io:format("rb: can't open file ~p~n", [Fname]),
590
find_report([{No, _Type, _Descr, _Date, Fname, FilePosition}|_T], No) ->
591
{Fname, FilePosition};
592
find_report([_H|T], No) ->
558
594
find_report([], No) ->
559
io:format("There is no report with number ~p.~n", [No]).
595
io:format("There is no report with number ~p.~n", [No]),
561
print_grep_reports(Dir, [], RegExp, Device) ->
563
print_grep_reports(Dir, Data, RegExp, Device) ->
564
print_grep_report(Dir, Data, element(1, hd(Data)), Device, RegExp),
565
print_grep_reports(Dir, tl(Data), RegExp, Device).
598
print_grep_reports(_Dir, [], _RegExp, Device, _Abort, _Log) ->
600
print_grep_reports(Dir, Data, RegExp, Device, Abort, Log) ->
601
{Next,Device1} = print_grep_report(Dir, Data, element(1, hd(Data)),
602
Device, RegExp, Abort, Log),
606
print_grep_reports(Dir, tl(Data), RegExp, Device1, Abort, Log)
567
print_grep_report(Dir, Data, Number, Device, RegExp) ->
568
{Fname, FilePosition, Type} = find_report(Data, Number),
609
print_grep_report(Dir, Data, Number, Device, RegExp, Abort, Log) ->
610
{Fname, FilePosition} = find_report(Data, Number),
569
611
FileName = lists:concat([Dir, Fname]),
570
612
case file:open(FileName, read) of
571
613
{ok, Fd} when pid(Fd) ->
572
check_rep(Fd, FilePosition, Type, Device, RegExp, Number);
614
check_rep(Fd, FilePosition, Device, RegExp, Number, Abort, Log);
574
io:format("rb: can't open file ~p~n", [Fname])
616
io:format("rb: can't open file ~p~n", [Fname]),
577
check_rep(Fd, FilePosition, Type, Device, RegExp, Number) ->
578
case read_rep_msg(Fd, FilePosition, Type) of
620
check_rep(Fd, FilePosition, Device, RegExp, Number, Abort, Log) ->
621
case read_rep_msg(Fd, FilePosition) of
580
623
MsgStr = lists:flatten(io_lib:format("~p",[Msg])),
581
624
case regexp:match(MsgStr, RegExp) of
583
626
io:format("Found match in report number ~w~n", [Number]),
584
rb_format_supp:print(Date, Msg, Device);
627
case catch rb_format_supp:print(Date, Msg, Device) of
629
handle_bad_form(Date, Msg, Device, Abort, Log);
588
io:format("rb: Cannot read from file~n")
637
io:format("rb: Cannot read from file~n"),
591
read_rep(Fd, FilePosition, Type, Device) ->
592
case read_rep_msg(Fd, FilePosition, Type) of
641
read_rep(Fd, FilePosition, Device, Abort, Log) ->
642
case read_rep_msg(Fd, FilePosition) of
594
644
case catch rb_format_supp:print(Date, Msg, Device) of
596
io:format(Device, "ERROR: ~p ~p~n", [Date, Msg]);
646
handle_bad_form(Date, Msg, Device, Abort, Log);
601
io:format("rb: Cannot read from file~n")
651
io:format("rb: Cannot read from file~n"),
604
read_rep_msg(Fd, FilePosition, Type) ->
655
handle_bad_form(Date, Msg, Device, Abort, Log) ->
656
io:format("rb: ERROR! A report on bad form was encountered. " ++
657
"It can not be printed to the log.~n~n"),
658
io:format("Details:~n~p ~p~n~n", [Date,Msg]),
659
case {Abort,Device,open_log_file(Log)} of
660
{true,standard_io,standard_io} ->
661
io:format("rb: Logging aborted.~n"),
663
{false,standard_io,standard_io} ->
664
io:format("rb: Logging resumed...~n~n"),
667
io:format("rb: Can not reopen ~p. Logging aborted.~n", [Log]),
669
{true,_,NewDevice} ->
671
"~n~n************************* RB ERROR ************************~n" ++
672
"A report on bad form was encountered here and the logging~n" ++
673
"process was aborted. Note that there may well be remaining~n" ++
674
"reports that haven't yet been logged. Please see the rb~n" ++
675
"manual for more info.~n" ++
676
"***********************************************************~n", []),
677
io:format("rb: Logging aborted.~n"),
679
{false,_,NewDevice} ->
681
"~n ********* RB: UNPRINTABLE REPORT ********~n~n", []),
682
io:format("rb: Logging resumed...~n~n"),
686
read_rep_msg(Fd, FilePosition) ->
605
687
file:position(Fd, {bof, FilePosition}),
607
689
case catch read_report(Fd) of