Skip to content

Commit

Permalink
feat: define exception/failed depending on logger (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
chsukivra authored Oct 5, 2021
1 parent c4dd4a8 commit 6dfffb2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
47 changes: 31 additions & 16 deletions include/prelude.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,38 @@
-define(emergency(StringOrReport, ArgsOrMeta), ?LOG_EMERGENCY(StringOrReport, ArgsOrMeta)).
-define(emergency(FunOrFormat, Args, Meta), ?LOG_EMERGENCY(FunOrFormat, Args, Meta)).

-define(failed(Rsn, Extras)
, ?error( #{ reason => Rsn
, extras => s2_util:ensure_map(Extras)})).
-define(failed(Rsn)
, ?failed(Rsn, [])).

-define(exception(Class, Reason, Stacktrace, Extras)
, ?error(#{ class => Class
, reason => Reason
, stacktrace => Stacktrace
, extras => s2_util:ensure_map(Extras)})).

-define(exception(Class, Reason, Stacktrace)
, ?exception(Class, Reason, Stacktrace, [])).
-else.

-define(failed(Rsn, Extras), ?error( "Error: ~p"
, [ {failed, Rsn
, [ {function, ?FUNCTION_BIN}
, {line, ?LINE}
| Extras
]}])).
-define(failed(Rsn), ?failed(Rsn, [])).

-define(exception(Class, Reason, Stacktrace, Extras)
, ?error( "Exception: ~p\n"
"Extras: ~p"
, [{{Class, Reason}, Stacktrace}, Extras])).

-define(exception(Class, Reason, Stacktrace)
, ?exception(Class, Reason, Stacktrace, [])).

-ifdef(S2_USE_LAGER).

-compile([{parse_transform, lager_transform}]).
Expand Down Expand Up @@ -181,14 +212,6 @@

-define(FUNCTION_BIN, iolist_to_binary(?FUNCTION_STRING)).

-define(failed(Rsn, Extras), ?error( "Error: ~p"
, [ {failed, Rsn
, [ {function, ?FUNCTION_BIN}
, {line, ?LINE}
| Extras
]}])).
-define(failed(Rsn), ?failed(Rsn, [])).

%% Structured warning
%%
%% * Good to have when sending warnings to Sentry
Expand All @@ -207,14 +230,6 @@
-define(warn(Format, Args), ?warn(Format, Args, [])).
-define(warn(Format), ?warn(Format, [], [])).

-define( exception(Class, Reason, Stacktrace, Extras)
, ?error( "Exception: ~p\n"
"Extras: ~p"
, [{{Class, Reason}, Stacktrace}, Extras])).

-define( exception(Class, Reason, Stacktrace)
, ?exception(Class, Reason, Stacktrace, [])).

%%%_* Metrics ==========================================================
%% Luke Gorrie's favourite profiling macro.
-define(TIME(Tag, Expr),
Expand Down
9 changes: 9 additions & 0 deletions src/s2_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
%%%_* Exports ==========================================================
-export([ consult_string/1
, init_folsom/1
, ensure_map/1
]).

-ignore_xref([init_folsom/1]).
Expand Down Expand Up @@ -61,6 +62,14 @@ init_folsom(Metrics) ->
[folsom_metrics:tag_metric(A, {ret, Ret}) || Ret <- Rest]
end || {Type, [App, Mod, Func|Rest] = Name} <- Metrics]).

-spec ensure_map(map() | list({atom(),any()})) -> map().
%% @doc Ensure parameters to OTP logger is a map
ensure_map(Map) when is_map(Map) ->
Map;
ensure_map(List) ->
maps:from_list(List).


%%%_* Emacs ============================================================
%%% Local Variables:
%%% allout-layout: t
Expand Down

0 comments on commit 6dfffb2

Please sign in to comment.