diff --git a/lib/compiler/src/compile.erl b/lib/compiler/src/compile.erl index f1b489c..5958c5d 100644 --- a/lib/compiler/src/compile.erl +++ b/lib/compiler/src/compile.erl @@ -191,6 +191,7 @@ format_error({bad_return,Pass,Reason}) -> dir="", base="", ifile="", + lfile="", ofile="", module=[], code=[], @@ -236,30 +237,62 @@ internal_comp(Passes, File, Suffix, St0) -> Base = filename:basename(File, Suffix), St1 = St0#compile{filename=File, dir=Dir, base=Base, ifile=erlfile(Dir, Base, Suffix), + lfile=logfile(Dir, Base), ofile=objfile(Base, St0)}, + Log = log_open(St1), Run = case member(time, St1#compile.options) of true -> io:format("Compiling ~p\n", [File]), fun run_tc/2; false -> - fun({Name,Fun}, St) -> - %% XXXfreza - St2 = begin catch Fun(St) end, - Show = case St2 of - {ok, Tree} -> - {pass, Name, Tree}; - Error -> - {pass, Name, Error} - end, - ok = io:fwrite("~n~p.~n", [Show]), - St2 - end + fun ({Name, Fun}, St) -> + comp_pass(Name, Fun, St, Log) + end end, - case fold_comp(Passes, Run, St1) of - {ok,St2} -> comp_ret_ok(St2); - {error,St2} -> comp_ret_err(St2) + Ret = case fold_comp(Passes, Run, St1) of + {ok, St2} -> + comp_ret_ok(St2); + {error, St2} -> + comp_ret_err(St2) + end, + log_close(Log), + Ret. + +comp_pass(_, Fun, St, nolog) -> + catch Fun(St); +comp_pass(Name, Fun, St, Log) -> + Pass = (catch Fun(St)), + case Pass of + {ok, Stx} -> + log_write(Log, "~n~p.~n", [{pass, Name, Stx}]); + Error -> %% Error = {error, _} | {'EXIT', _} + log_write(Log, "~n~p.~n", [{pass, Name, Error}]) + end, + Pass. + +log_open(St) -> + File = St#compile.lfile, + case member(log, St#compile.options) of + true -> + case file:open(File, [write, delayed_write]) of + {ok, Io} -> + Io; + {error, Reason} -> + io:format("Could not open log ~p: ~p~n", [File, Reason]), + nolog + end; + false -> + nolog end. +log_write(Io, Format, Args) when Io /= nolog -> + io:fwrite(Io, Format, Args). + +log_close(nolog) -> + ok; +log_close(Io) -> + ok = file:close(Io). + fold_comp([{delay,Ps0}|Passes], Run, #compile{options=Opts}=St) -> Ps = select_passes(Ps0, Opts) ++ Passes, fold_comp(Ps, Run, St); @@ -1159,6 +1193,9 @@ iofile(File) -> erlfile(Dir, Base, Suffix) -> filename:join(Dir, Base++Suffix). +logfile(Dir, Base) -> + filename:join(Dir, Base ++ ".log"). + outfile(Base, Ext, Opts) when is_atom(Ext) -> outfile(Base, atom_to_list(Ext), Opts); outfile(Base, Ext, Opts) ->