From 8c796ed027bea695991a7e8974ab6806f0298c35 Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Sun, 8 Jul 2018 20:42:53 +0300 Subject: [PATCH] Better format invalid values when logging them --- src/ejabberd_config.erl | 13 +++++++------ src/gen_mod.erl | 10 ++++++---- src/misc.erl | 15 ++++++++++++++- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/ejabberd_config.erl b/src/ejabberd_config.erl index ca4b427d4..d47dc2892 100644 --- a/src/ejabberd_config.erl +++ b/src/ejabberd_config.erl @@ -1078,14 +1078,15 @@ validate_opts(#state{opts = Opts} = State, ModOpts) -> NewVal -> In#local_config{value = NewVal} catch {invalid_syntax, Error} -> - ?ERROR_MSG("Invalid value '~p' for " - "option '~s': ~s", - [Val, Opt, Error]), + ?ERROR_MSG("Invalid value for " + "option '~s' (~s): ~s", + [Opt, Error, + misc:format_val(Val)]), erlang:error(invalid_option); _:_ -> - ?ERROR_MSG("Invalid value '~p' for " - "option '~s'", - [Val, Opt]), + ?ERROR_MSG("Invalid value for " + "option '~s': ~s", + [Opt, misc:format_val(Val)]), erlang:error(invalid_option) end; _ -> diff --git a/src/gen_mod.erl b/src/gen_mod.erl index b003986a8..040bcb7b8 100644 --- a/src/gen_mod.erl +++ b/src/gen_mod.erl @@ -546,12 +546,14 @@ validate_opts(Host, Module, Opts0) -> [Module, Opt]), module_error(ErrTxt); _:{invalid_option, Opt, Val} -> - ErrTxt = io_lib:format("Invalid value '~p' for option '~s' of " - "module '~s'", [Val, Opt, Module]), + ErrTxt = io_lib:format("Invalid value for option '~s' of " + "module ~s: ~s", + [Opt, Module, misc:format_val(Val)]), module_error(ErrTxt); _:{invalid_option, Opt, Val, Reason} -> - ErrTxt = io_lib:format("Invalid value '~p' for option '~s' of " - "module '~s': ~s", [Val, Opt, Module, Reason]), + ErrTxt = io_lib:format("Invalid value for option '~s' of " + "module ~s (~s): ~s", + [Opt, Module, Reason, misc:format_val(Val)]), module_error(ErrTxt); _:{unknown_option, Opt, []} -> ErrTxt = io_lib:format("Unknown option '~s' of module '~s': " diff --git a/src/misc.erl b/src/misc.erl index 69534ef33..af2933ef6 100644 --- a/src/misc.erl +++ b/src/misc.erl @@ -35,7 +35,8 @@ now_to_usec/1, usec_to_now/1, encode_pid/1, decode_pid/2, compile_exprs/2, join_atoms/2, try_read_file/1, get_descr/2, css_dir/0, img_dir/0, js_dir/0, msgs_dir/0, sql_dir/0, - read_css/1, read_img/1, read_js/1, try_url/1, intersection/2]). + read_css/1, read_img/1, read_js/1, try_url/1, intersection/2, + format_val/1]). %% Deprecated functions -export([decode_base64/1, encode_base64/1]). @@ -287,6 +288,18 @@ intersection(L1, L2) -> lists:member(E, L2) end, L1). +-spec format_val(any()) -> iodata(). +format_val(I) when is_integer(I) -> + integer_to_list(I); +format_val(S) when is_binary(S) -> + S; +format_val(B) when is_atom(B) -> + erlang:atom_to_binary(B, utf8); +format_val(YAML) -> + try [io_lib:nl(), fast_yaml:encode(YAML)] + catch _:_ -> io_lib:format("~p", [YAML]) + end. + %%%=================================================================== %%% Internal functions %%%===================================================================