Skip to content

Commit

Permalink
Add conditional logging macros #1707
Browse files Browse the repository at this point in the history
New macros DEBUG_IF, INFO_MSG_IF, ERROR_MSG_IF, WARNING_MSG_IF
  • Loading branch information
kzemek authored and arcusfelis committed Feb 14, 2018
1 parent 4865c0c commit 86d836b
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 85 deletions.
18 changes: 18 additions & 0 deletions include/mongoose_logger.hrl
@@ -1,19 +1,37 @@
-ifndef(MONGOOSEIM_LOGGER_HRL).
-define(MONGOOSEIM_LOGGER_HRL, true).

-define(LOG_IF(Level, Condition, Format, Args),
(Condition) == true andalso lager:Level(Format, Args)).

-define(DEBUG(Format, Args),
lager:debug(Format, Args)).

-define(DEBUG_IF(Condition, Format, Args),
?LOG_IF(debug, Condition, Format, Args)).

-define(INFO_MSG(Format, Args),
lager:info(Format, Args)).

-define(INFO_MSG_IF(Condition, Format, Args),
?LOG_IF(info, Condition, Format, Args)).

-define(WARNING_MSG(Format, Args),
lager:warning(Format, Args)).

-define(WARNING_MSG_IF(Condition, Format, Args),
?LOG_IF(warning, Condition, Format, Args)).

-define(ERROR_MSG(Format, Args),
lager:error(Format, Args)).

-define(ERROR_MSG_IF(Condition, Format, Args),
?LOG_IF(error, Condition, Format, Args)).

-define(CRITICAL_MSG(Format, Args),
lager:critical(Format, Args)).

-define(CRITICAL_MSG_IF(Condition, Format, Args),
?LOG_IF(critical, Condition, Format, Args)).

-endif.
8 changes: 2 additions & 6 deletions src/ejabberd_commands.erl
Expand Up @@ -260,12 +260,8 @@ init() ->
register_commands(Commands) ->
lists:foreach(
fun(Command) ->
case ets:insert_new(ejabberd_commands, Command) of
true ->
ok;
false ->
?DEBUG("This command is already defined:~n~p", [Command])
end
Inserted = ets:insert_new(ejabberd_commands, Command),
?DEBUG_IF(not Inserted, "This command is already defined:~n~p", [Command])
end,
Commands).

Expand Down
44 changes: 8 additions & 36 deletions src/ejabberd_config.erl
Expand Up @@ -1037,12 +1037,7 @@ handle_local_config_add(#local_config{key = riak_server}) ->
handle_local_config_add(#local_config{key = cassandra_servers}) ->
mongoose_cassandra:start();
handle_local_config_add(#local_config{key = Key} = El) ->
case can_be_ignored(Key) of
true ->
ok;
false ->
?WARNING_MSG("local config add ~p option unhandled", [El])
end.
?WARNING_MSG_IF(not can_be_ignored(Key), "local config add ~p option unhandled", [El]).

handle_local_config_del(#local_config{key = riak_server}) ->
mongoose_riak:stop();
Expand All @@ -1052,12 +1047,7 @@ handle_local_config_del(#local_config{key = node_start}) ->
%% do nothing with it
ok;
handle_local_config_del(#local_config{key = Key} = El) ->
case can_be_ignored(Key) of
true ->
ok;
false ->
?WARNING_MSG("local config change: ~p unhandled", [El])
end.
?WARNING_MSG_IF(not can_be_ignored(Key), "local config change: ~p unhandled", [El]).

handle_local_config_change({listen, Old, New}) ->
reload_listeners(compare_listeners(Old, New));
Expand All @@ -1070,12 +1060,7 @@ handle_local_config_change({cassandra_servers, _Old, _New}) ->
mongoose_cassandra:start(),
ok;
handle_local_config_change({Key, _Old, _New} = El) ->
case can_be_ignored(Key) of
true ->
ok;
false ->
?WARNING_MSG("local config change: ~p unhandled", [El])
end.
?WARNING_MSG_IF(not can_be_ignored(Key), "local config change: ~p unhandled", [El]).

%% ----------------------------------------------------------------
%% LOCAL HOST CONFIG
Expand All @@ -1089,12 +1074,7 @@ handle_local_hosts_config_add({{ldap, _Host}, _}) ->
handle_local_hosts_config_add({{modules, Host}, Modules}) ->
gen_mod_deps:start_modules(Host, Modules);
handle_local_hosts_config_add({{Key, _Host}, _} = El) ->
case can_be_ignored(Key) of
true ->
ok;
false ->
?WARNING_MSG("local hosts config add option: ~p unhandled", [El])
end.
?WARNING_MSG_IF(not can_be_ignored(Key), "local hosts config add option: ~p unhandled", [El]).

handle_local_hosts_config_del({{auth, Host}, Opts}) ->
case lists:keyfind(auth_method, 1, Opts) of
Expand All @@ -1112,12 +1092,8 @@ handle_local_hosts_config_del({{ldap, _Host}, _I}) ->
handle_local_hosts_config_del({{modules, Host}, Modules}) ->
lists:foreach(fun({Mod, _}) -> gen_mod:stop_module(Host, Mod) end, Modules);
handle_local_hosts_config_del({{Key, _}, _} =El) ->
case can_be_ignored(Key) of
true ->
ok;
false ->
?WARNING_MSG("local hosts config delete option: ~p unhandled", [El])
end.
?WARNING_MSG_IF(not can_be_ignored(Key),
"local hosts config delete option: ~p unhandled", [El]).

handle_local_hosts_config_change({{auth, Host}, OldVals, _}) ->
case lists:keyfind(auth_method, 1, OldVals) of
Expand All @@ -1136,12 +1112,8 @@ handle_local_hosts_config_change({{ldap, Host}, _OldConfig, NewConfig}) ->
handle_local_hosts_config_change({{modules, Host}, OldModules, NewModules}) ->
gen_mod_deps:replace_modules(Host, OldModules, NewModules);
handle_local_hosts_config_change({{Key, _Host}, _Old, _New} = El) ->
case can_be_ignored(Key) of
true ->
ok;
false ->
?WARNING_MSG("local hosts config change option: ~p unhandled", [El])
end.
?WARNING_MSG_IF(not can_be_ignored(Key),
"local hosts config change option: ~p unhandled", [El]).

methods_to_auth_modules(L) when is_list(L) ->
[list_to_atom("ejabberd_auth_" ++ atom_to_list(M)) || M <- L];
Expand Down
12 changes: 5 additions & 7 deletions src/ejabberd_listener.erl
Expand Up @@ -83,13 +83,11 @@ start_listeners() ->
report_duplicated_portips(L) ->
LKeys = [Port || {Port, _, _} <- L],
LNoDupsKeys = proplists:get_keys(L),
case LKeys -- LNoDupsKeys of
[] -> ok;
Dups ->
?CRITICAL_MSG("In the ejabberd configuration there are duplicated "
"Port number + IP address:~n ~p",
[Dups])
end.
Dups = LKeys -- LNoDupsKeys,
?CRITICAL_MSG_IF(Dups /= [],
"In the ejabberd configuration there are duplicated "
"Port number + IP address:~n ~p",
[Dups]).

-spec start(Port :: _,
Module :: atom() | tuple(),
Expand Down
19 changes: 6 additions & 13 deletions src/global_distrib/mod_global_distrib_bounce.erl
Expand Up @@ -94,13 +94,9 @@ maybe_store_message({From, To, Acc0, Packet} = FPacket) ->
ToBin = jid:to_binary(To),
?DEBUG("Not storing global message id=~s from=~s to=~s as bounce_ttl=0",
[Id, FromBin, ToBin]),
case To#jid.luser of
<<>> -> % It's a component!
?ERROR_MSG("event=message_to_component_ttl_zero,id=~s,from=~s,to=~s",
[Id, FromBin, ToBin]);
_ ->
nothing_to_log
end,
?ERROR_MSG_IF(To#jid.luser == <<>>,
"event=message_to_component_ttl_zero,id=~s,from=~s,to=~s",
[Id, FromBin, ToBin]),
mongoose_metrics:update(global, ?GLOBAL_DISTRIB_STOP_TTL_ZERO, 1),
FPacket;
OldTTL ->
Expand Down Expand Up @@ -131,12 +127,9 @@ reroute_messages(Acc, From, To, TargetHost) ->
end
end,
ets:take(?MS_BY_TARGET, Key)),
case StoredMessages of
[] -> ok;
_ ->
?DEBUG("Routing ~B previously stored messages addressed from ~s to ~s",
[length(StoredMessages), jid:to_binary(From), jid:to_binary(To)])
end,
?DEBUG_IF(StoredMessages =/= [],
"Routing ~B previously stored messages addressed from ~s to ~s",
[length(StoredMessages), jid:to_binary(From), jid:to_binary(To)]),
lists:foreach(pa:bind(fun reroute_message/2, TargetHost), StoredMessages),
Acc.

Expand Down
9 changes: 2 additions & 7 deletions src/mod_bosh_socket.erl
Expand Up @@ -502,13 +502,8 @@ rid(#state{} = S, Rid) when is_integer(Rid), Rid > 0 ->
determine_report_action(undefined, false, _, _) ->
{noreport, undefined};
determine_report_action(undefined, true, Rid, LastProcessed) ->
case Rid+1 == LastProcessed of
true ->
{noreport, undefined};
false ->
?WARNING_MSG("expected 'ack' attribute on ~p~n", [Rid]),
{noreport, undefined}
end;
?WARNING_MSG_IF(Rid+1 /= LastProcessed, "expected 'ack' attribute on ~p~n", [Rid]),
{noreport, undefined};
determine_report_action(BinAck, _, _, LastProcessed) ->
Ack = binary_to_integer(BinAck),
case {LastProcessed, is_valid_ack(Ack, LastProcessed)} of
Expand Down
13 changes: 5 additions & 8 deletions src/mod_mam.erl
Expand Up @@ -178,14 +178,11 @@ archive_id(Server, User)
-spec start(Host :: jid:server(), Opts :: list()) -> any().
start(Host, Opts) ->
?DEBUG("mod_mam starting", []),
case gen_mod:get_opt(archive_groupchats, Opts, undefined) of
undefined ->
?WARNING_MSG("mod_mam is enabled without explicit archive_groupchats option value."
" It will default to `false` in one of future releases."
" Please check the mod_mam documentation for more details.", []);
_ ->
ok
end,
?WARNING_MSG_IF(
gen_mod:get_opt(archive_groupchats, Opts, undefined) == undefined,
"mod_mam is enabled without explicit archive_groupchats option value."
" It will default to `false` in one of future releases."
" Please check the mod_mam documentation for more details.", []),
case gen_mod:get_opt(add_archived_element, Opts, undefined) of
undefined -> ok;
_ ->
Expand Down
14 changes: 6 additions & 8 deletions src/node_pep.erl
Expand Up @@ -253,11 +253,9 @@ path_to_node(Path) ->
%% Check that the mod_caps module is enabled in that Jabber Host
%% If not, show a warning message in the ejabberd log file.
complain_if_modcaps_disabled(ServerHost) ->
case gen_mod:is_loaded(ServerHost, mod_caps) of
false ->
?WARNING_MSG("The PEP plugin is enabled in mod_pubsub "
"of host ~p. This plugin requires mod_caps "
"to be enabled, but it isn't.",
[ServerHost]);
true -> ok
end.
?WARNING_MSG_IF(
not gen_mod:is_loaded(ServerHost, mod_caps),
"The PEP plugin is enabled in mod_pubsub "
"of host ~p. This plugin requires mod_caps "
"to be enabled, but it isn't.",
[ServerHost]).

0 comments on commit 86d836b

Please sign in to comment.