Skip to content

Commit 10752ec

Browse files
authored
Merge pull request #55 from tsloughter/otp-21
logging and stacktrace OTP-21 support
2 parents ce0049f + 713904d commit 10752ec

6 files changed

Lines changed: 43 additions & 19 deletions

File tree

rebar.config

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
{coveralls, "1.4.0"},
2424
{rebar3_lint, "0.1.9"}
2525
]}.
26-
27-
{provider_hooks, [{pre, [{eunit, lint}]}]}.
26+
{plugins, [{rebar_erl_vsn, "0.1.7"}]}.
27+
{provider_hooks, [{pre, [{compile, erl_vsn},
28+
{eunit, lint}]}]}.
2829

2930
{cover_enabled, true}.
3031
{cover_export_enabled, true}.

src/elli.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@ handle_cast(_Msg, State) ->
218218
Result :: {stop, emfile, State0}
219219
| {noreply, State1 :: state()}.
220220
handle_info({'EXIT', _Pid, {error, emfile}}, State) ->
221-
?ERROR("No more file descriptors, shutting down~n"),
221+
?LOG_ERROR("No more file descriptors, shutting down~n"),
222222
{stop, emfile, State};
223223

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

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

231231

src/elli_example_callback.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ chunk_loop(Ref, N) ->
226226

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

232232
chunk_loop(Ref, N-1).

src/elli_http.erl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,19 @@ execute_callback(#req{callback = {Mod, Args}} = Req) ->
311311
catch
312312
throw:{ResponseCode, Headers, Body} when is_integer(ResponseCode) ->
313313
{response, ResponseCode, Headers, Body};
314-
throw:Exc ->
314+
?WITH_STACKTRACE(throw, Exc, Stacktrace)
315315
handle_event(Mod, request_throw,
316-
[Req, Exc, erlang:get_stacktrace()],
316+
[Req, Exc, Stacktrace],
317317
Args),
318318
{response, 500, [], <<"Internal server error">>};
319-
error:Error ->
319+
?WITH_STACKTRACE(error, Error, Stacktrace)
320320
handle_event(Mod, request_error,
321-
[Req, Error, erlang:get_stacktrace()],
321+
[Req, Error, Stacktrace],
322322
Args),
323323
{response, 500, [], <<"Internal server error">>};
324-
exit:Exit ->
324+
?WITH_STACKTRACE(exit, Exit, Stacktrace)
325325
handle_event(Mod, request_exit,
326-
[Req, Exit, erlang:get_stacktrace()],
326+
[Req, Exit, Stacktrace],
327327
Args),
328328
{response, 500, [], <<"Internal server error">>}
329329
end.
@@ -735,9 +735,9 @@ handle_event(Mod, Name, EventArgs, ElliArgs) ->
735735
try
736736
Mod:handle_event(Name, EventArgs, ElliArgs)
737737
catch
738-
EvClass:EvError ->
739-
?ERROR("~p:handle_event/3 crashed ~p:~p~n~p",
740-
[Mod, EvClass, EvError, erlang:get_stacktrace()])
738+
?WITH_STACKTRACE(EvClass, EvError, Stacktrace)
739+
?LOG_ERROR("~p:handle_event/3 crashed ~p:~p~n~p",
740+
[Mod, EvClass, EvError, Stacktrace])
741741
end.
742742

743743
%%

src/elli_util.hrl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
-define(I2L(I), integer_to_list(I)).
2+
3+
-ifdef('16.0').
4+
-define(B2I(I), binary_to_integer(I)).
5+
-else.
26
-define(B2I(I), list_to_integer(binary_to_list(I))).
7+
-endif.
8+
9+
-ifdef('21.0').
10+
-include_lib("kernel/include/logger.hrl").
11+
-else.
12+
-define(LOG_ERROR(Str), error_logger:error_msg(Str)).
13+
-define(LOG_ERROR(Format,Data), error_logger:error_msg(Format, Data)).
14+
-define(LOG_INFO(Format,Data), error_logger:info_msg(Format, Data)).
15+
-endif.
316

4-
-define(ERROR(Str), error_logger:error_msg(Str)).
5-
-define(ERROR(Format,Data), error_logger:error_msg(Format, Data)).
17+
-ifdef('21.0').
18+
-define(WITH_STACKTRACE(T, R, S), T:R:S ->).
19+
-else.
20+
-define(WITH_STACKTRACE(T, R, S), T:R -> S = erlang:get_stacktrace(),).
21+
-endif.
622

723
%% Bloody useful
824
-define(IF(Test,True,False), case Test of true -> True; false -> False end).

test/elli_tests.erl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
-define(VTB(T1, T2, LB, UB),
99
time_diff_to_micro_seconds(T1, T2) >= LB andalso
1010
time_diff_to_micro_seconds(T1, T2) =< UB).
11+
-ifdef('21.0').
12+
-include_lib("kernel/include/logger.hrl").
13+
-else.
14+
-define(LOG_ERROR(Str), error_logger:error_msg(Str)).
15+
-define(LOG_ERROR(Format,Data), error_logger:error_msg(Format, Data)).
16+
-define(LOG_INFO(Format,Data), error_logger:info_msg(Format, Data)).
17+
-endif.
1118

1219
time_diff_to_micro_seconds(T1, T2) ->
1320
erlang:convert_time_unit(
@@ -541,8 +548,8 @@ get_pipeline() ->
541548
true ->
542549
ok;
543550
false ->
544-
error_logger:info_msg("Expected: ~p~nResult: ~p~n",
545-
[binary:copy(ExpectedResponse, 2), Res])
551+
?LOG_INFO("Expected: ~p~nResult: ~p~n",
552+
[binary:copy(ExpectedResponse, 2), Res])
546553
end,
547554

548555
?assertEqual(binary:copy(ExpectedResponse, 2),
@@ -606,7 +613,7 @@ send(Socket, B, ChunkSize) ->
606613
<<P:ChunkSize/binary, R/binary>> -> {P, R};
607614
P -> {P, <<>>}
608615
end,
609-
%%error_logger:info_msg("~p~n", [Part]),
616+
%%?LOG_INFO("~p~n", [Part]),
610617
gen_tcp:send(Socket, Part),
611618
timer:sleep(1),
612619
send(Socket, Rest, ChunkSize).

0 commit comments

Comments
 (0)