Skip to content

Commit

Permalink
Merge pull request #55 from tsloughter/otp-21
Browse files Browse the repository at this point in the history
logging and stacktrace OTP-21 support
  • Loading branch information
tsloughter authored May 6, 2018
2 parents ce0049f + 713904d commit 10752ec
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
5 changes: 3 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
{coveralls, "1.4.0"},
{rebar3_lint, "0.1.9"}
]}.

{provider_hooks, [{pre, [{eunit, lint}]}]}.
{plugins, [{rebar_erl_vsn, "0.1.7"}]}.
{provider_hooks, [{pre, [{compile, erl_vsn},
{eunit, lint}]}]}.

{cover_enabled, true}.
{cover_export_enabled, true}.
Expand Down
4 changes: 2 additions & 2 deletions src/elli.erl
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ handle_cast(_Msg, State) ->
Result :: {stop, emfile, State0}
| {noreply, State1 :: state()}.
handle_info({'EXIT', _Pid, {error, emfile}}, State) ->
?ERROR("No more file descriptors, shutting down~n"),
?LOG_ERROR("No more file descriptors, shutting down~n"),
{stop, emfile, State};

handle_info({'EXIT', Pid, normal}, State) ->
{noreply, remove_acceptor(State, Pid)};

handle_info({'EXIT', Pid, Reason}, State) ->
?ERROR("Elli request (pid ~p) unexpectedly crashed:~n~p~n", [Pid, Reason]),
?LOG_ERROR("Elli request (pid ~p) unexpectedly crashed:~n~p~n", [Pid, Reason]),
{noreply, remove_acceptor(State, Pid)}.


Expand Down
2 changes: 1 addition & 1 deletion src/elli_example_callback.erl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ chunk_loop(Ref, N) ->

case elli_request:send_chunk(Ref, [<<"chunk">>, ?I2L(N)]) of
ok -> ok;
{error, Reason} -> ?ERROR("error in sending chunk: ~p~n", [Reason])
{error, Reason} -> ?LOG_ERROR("error in sending chunk: ~p~n", [Reason])
end,

chunk_loop(Ref, N-1).
Expand Down
18 changes: 9 additions & 9 deletions src/elli_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,19 @@ execute_callback(#req{callback = {Mod, Args}} = Req) ->
catch
throw:{ResponseCode, Headers, Body} when is_integer(ResponseCode) ->
{response, ResponseCode, Headers, Body};
throw:Exc ->
?WITH_STACKTRACE(throw, Exc, Stacktrace)
handle_event(Mod, request_throw,
[Req, Exc, erlang:get_stacktrace()],
[Req, Exc, Stacktrace],
Args),
{response, 500, [], <<"Internal server error">>};
error:Error ->
?WITH_STACKTRACE(error, Error, Stacktrace)
handle_event(Mod, request_error,
[Req, Error, erlang:get_stacktrace()],
[Req, Error, Stacktrace],
Args),
{response, 500, [], <<"Internal server error">>};
exit:Exit ->
?WITH_STACKTRACE(exit, Exit, Stacktrace)
handle_event(Mod, request_exit,
[Req, Exit, erlang:get_stacktrace()],
[Req, Exit, Stacktrace],
Args),
{response, 500, [], <<"Internal server error">>}
end.
Expand Down Expand Up @@ -735,9 +735,9 @@ handle_event(Mod, Name, EventArgs, ElliArgs) ->
try
Mod:handle_event(Name, EventArgs, ElliArgs)
catch
EvClass:EvError ->
?ERROR("~p:handle_event/3 crashed ~p:~p~n~p",
[Mod, EvClass, EvError, erlang:get_stacktrace()])
?WITH_STACKTRACE(EvClass, EvError, Stacktrace)
?LOG_ERROR("~p:handle_event/3 crashed ~p:~p~n~p",
[Mod, EvClass, EvError, Stacktrace])
end.

%%
Expand Down
20 changes: 18 additions & 2 deletions src/elli_util.hrl
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
-define(I2L(I), integer_to_list(I)).

-ifdef('16.0').
-define(B2I(I), binary_to_integer(I)).
-else.
-define(B2I(I), list_to_integer(binary_to_list(I))).
-endif.

-ifdef('21.0').
-include_lib("kernel/include/logger.hrl").
-else.
-define(LOG_ERROR(Str), error_logger:error_msg(Str)).
-define(LOG_ERROR(Format,Data), error_logger:error_msg(Format, Data)).
-define(LOG_INFO(Format,Data), error_logger:info_msg(Format, Data)).
-endif.

-define(ERROR(Str), error_logger:error_msg(Str)).
-define(ERROR(Format,Data), error_logger:error_msg(Format, Data)).
-ifdef('21.0').
-define(WITH_STACKTRACE(T, R, S), T:R:S ->).
-else.
-define(WITH_STACKTRACE(T, R, S), T:R -> S = erlang:get_stacktrace(),).
-endif.

%% Bloody useful
-define(IF(Test,True,False), case Test of true -> True; false -> False end).
13 changes: 10 additions & 3 deletions test/elli_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
-define(VTB(T1, T2, LB, UB),
time_diff_to_micro_seconds(T1, T2) >= LB andalso
time_diff_to_micro_seconds(T1, T2) =< UB).
-ifdef('21.0').
-include_lib("kernel/include/logger.hrl").
-else.
-define(LOG_ERROR(Str), error_logger:error_msg(Str)).
-define(LOG_ERROR(Format,Data), error_logger:error_msg(Format, Data)).
-define(LOG_INFO(Format,Data), error_logger:info_msg(Format, Data)).
-endif.

time_diff_to_micro_seconds(T1, T2) ->
erlang:convert_time_unit(
Expand Down Expand Up @@ -541,8 +548,8 @@ get_pipeline() ->
true ->
ok;
false ->
error_logger:info_msg("Expected: ~p~nResult: ~p~n",
[binary:copy(ExpectedResponse, 2), Res])
?LOG_INFO("Expected: ~p~nResult: ~p~n",
[binary:copy(ExpectedResponse, 2), Res])
end,

?assertEqual(binary:copy(ExpectedResponse, 2),
Expand Down Expand Up @@ -606,7 +613,7 @@ send(Socket, B, ChunkSize) ->
<<P:ChunkSize/binary, R/binary>> -> {P, R};
P -> {P, <<>>}
end,
%%error_logger:info_msg("~p~n", [Part]),
%%?LOG_INFO("~p~n", [Part]),
gen_tcp:send(Socket, Part),
timer:sleep(1),
send(Socket, Rest, ChunkSize).
Expand Down

0 comments on commit 10752ec

Please sign in to comment.