Skip to content

Commit a52de96

Browse files
Merge pull request #1695 from rabbitmq/lrb-error-text-formatting
Format errors and warnings with regard to config files in a manner si…
2 parents 7ef3b27 + 11915db commit a52de96

File tree

5 files changed

+97
-47
lines changed

5 files changed

+97
-47
lines changed

scripts/rabbitmq-env

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ if [ -z "$RABBITMQ_SCRIPTS_DIR" ]; then
4545
RABBITMQ_SCRIPTS_DIR=`dirname $SCRIPT_PATH`
4646
fi
4747

48+
_rmq_env_now()
49+
{
50+
date '+%Y-%m-%d %H:%M:%S'
51+
}
52+
53+
_rmq_env_print()
54+
{
55+
_rmq_env_tmp="$1"
56+
_rmq_env_tmp_len="${#_rmq_env_tmp}"
57+
shift
58+
printf '%s %s %s\n' "$(_rmq_env_now)" "$_rmq_env_tmp" "$1" 1>&2
59+
shift
60+
_rmq_env_print_line=''
61+
_rmq_env_indent="$((_rmq_env_tmp_len + 21))"
62+
for _rmq_env_print_line in "$@"
63+
do
64+
printf "%${_rmq_env_indent}s%s\n" ' ' "$_rmq_env_print_line" 1>&2
65+
done
66+
unset _rmq_env_print_line
67+
unset _rmq_env_indent
68+
unset _rmq_env_tmp_len
69+
unset _rmq_env_tmp
70+
}
71+
72+
_rmq_env_perr()
73+
{
74+
_rmq_env_print '[error]' "$@"
75+
}
76+
77+
_rmq_env_pwarn()
78+
{
79+
_rmq_env_print '[warning]' "$@"
80+
}
81+
4882
rmq_realpath() {
4983
local path=$1
5084

@@ -77,6 +111,11 @@ ESCRIPT_DIR="${RABBITMQ_HOME}/escript"
77111
## Set defaults
78112
. ${RABBITMQ_SCRIPTS_DIR}/rabbitmq-defaults
79113

114+
# We save the current value of $RABBITMQ_PID_FILE in case it was set by
115+
# an init script. If $CONF_ENV_FILE overrides it again, we must ignore
116+
# it and warn the user.
117+
saved_RABBITMQ_PID_FILE="$RABBITMQ_PID_FILE"
118+
80119
## Get configuration variables from the configure environment file
81120
[ "x" = "x$RABBITMQ_CONF_ENV_FILE" ] && RABBITMQ_CONF_ENV_FILE=${CONF_ENV_FILE}
82121
[ -f ${RABBITMQ_CONF_ENV_FILE} ] && . ${RABBITMQ_CONF_ENV_FILE} || true
@@ -100,21 +139,6 @@ DEFAULT_MAX_NUMBER_OF_ATOMS=5000000
100139
## Common server defaults
101140
SERVER_ERL_ARGS=" +P $RABBITMQ_MAX_NUMBER_OF_PROCESSES +t $RABBITMQ_MAX_NUMBER_OF_ATOMS +stbt $RABBITMQ_SCHEDULER_BIND_TYPE +zdbbl $RABBITMQ_DISTRIBUTION_BUFFER_SIZE "
102141

103-
# We save the current value of $RABBITMQ_PID_FILE in case it was set by
104-
# an init script. If $CONF_ENV_FILE overrides it again, we must ignore
105-
# it and warn the user.
106-
saved_RABBITMQ_PID_FILE=$RABBITMQ_PID_FILE
107-
108-
if [ "$saved_RABBITMQ_PID_FILE" -a \
109-
"$saved_RABBITMQ_PID_FILE" != "$RABBITMQ_PID_FILE" ]; then
110-
echo "WARNING: RABBITMQ_PID_FILE was already set by the init script to:" 1>&2
111-
echo " $saved_RABBITMQ_PID_FILE" 1>&2
112-
echo " The value set in rabbitmq-env.conf is ignored because it" 1>&2
113-
echo " would break the init script." 1>&2
114-
115-
RABBITMQ_PID_FILE="$saved_RABBITMQ_PID_FILE"
116-
fi
117-
118142
[ "x" = "x$RABBITMQ_USE_LONGNAME" ] && RABBITMQ_USE_LONGNAME=${USE_LONGNAME}
119143
if [ "xtrue" = "x$RABBITMQ_USE_LONGNAME" ] ; then
120144
RABBITMQ_NAME_TYPE=-name
@@ -230,8 +254,21 @@ rmq_normalize_path_var \
230254
RABBITMQ_MNESIA_BASE \
231255
RABBITMQ_MNESIA_DIR
232256

233-
[ "x" = "x$RABBITMQ_PID_FILE" ] && RABBITMQ_PID_FILE=${PID_FILE}
234-
[ "x" = "x$RABBITMQ_PID_FILE" ] && RABBITMQ_PID_FILE=${RABBITMQ_MNESIA_DIR}.pid
257+
[ "x" = "x$RABBITMQ_PID_FILE" ] && RABBITMQ_PID_FILE="$PID_FILE"
258+
259+
if [ -n "$saved_RABBITMQ_PID_FILE" ] && \
260+
[ "$saved_RABBITMQ_PID_FILE" != "$RABBITMQ_PID_FILE" ]
261+
then
262+
_rmq_env_pwarn 'RABBITMQ_PID_FILE was already set by the init script to:' \
263+
"$saved_RABBITMQ_PID_FILE" \
264+
'The value set in rabbitmq-env.conf is ignored because it would break the init script.'
265+
266+
RABBITMQ_PID_FILE="$saved_RABBITMQ_PID_FILE"
267+
fi
268+
269+
# Note: at this point, no RABBITMQ_PID_FILE is set so we use the mnesia dir value
270+
[ "x" = "x$RABBITMQ_PID_FILE" ] && RABBITMQ_PID_FILE="${RABBITMQ_MNESIA_DIR}.pid"
271+
235272
rmq_normalize_path_var RABBITMQ_PID_FILE
236273

237274
[ "x" = "x$RABBITMQ_BOOT_MODULE" ] && RABBITMQ_BOOT_MODULE=${BOOT_MODULE}

scripts/rabbitmq-server

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ RABBITMQ_CONFIG_FILE_NOEX=$(get_noex ${RABBITMQ_CONFIG_FILE})
9393
if [ "${RABBITMQ_CONFIG_FILE_NOEX}" = "${RABBITMQ_CONFIG_FILE}" ]; then
9494
if [ -f "${RABBITMQ_CONFIG_FILE_NOEX}.config" ]; then
9595
if [ -f "${RABBITMQ_CONFIG_FILE_NOEX}.conf" ]; then
96-
# Both files exist. Print a warning
97-
echo "WARNING: Both old (.config) and new (.conf) format config files exist."
98-
echo "WARNING: Using the old format config file: ${RABBITMQ_CONFIG_FILE_NOEX}.config"
99-
echo "WARNING: Please update your config files to the new format and remove the old file"
96+
# Both files exist. Print a warning.
97+
_rmq_env_pwarn 'Both old (.config) and new (.conf) format config files exist.' \
98+
"Using the old format config file: ${RABBITMQ_CONFIG_FILE_NOEX}.config" \
99+
'Please update your config files to the new format and remove the old file.'
100100
fi
101101
RABBITMQ_CONFIG_FILE="${RABBITMQ_CONFIG_FILE_NOEX}.config"
102102
elif [ -f "${RABBITMQ_CONFIG_FILE_NOEX}.conf" ]; then
103103
RABBITMQ_CONFIG_FILE="${RABBITMQ_CONFIG_FILE_NOEX}.conf"
104104
else
105105
if [ -f ${RABBITMQ_ADVANCED_CONFIG_FILE} ]; then
106-
echo "WARNING: Using RABBITMQ_ADVANCED_CONFIG_FILE: ${RABBITMQ_ADVANCED_CONFIG_FILE}"
106+
_rmq_env_pwarn "Using RABBITMQ_ADVANCED_CONFIG_FILE: ${RABBITMQ_ADVANCED_CONFIG_FILE}"
107107
fi
108108
# No config file exist. Use advanced config for -config arg.
109109
RABBITMQ_CONFIG_ARG_FILE="${RABBITMQ_ADVANCED_CONFIG_FILE}"
@@ -125,23 +125,23 @@ elif [ "${RABBITMQ_CONFIG_FILE_NOEX}.conf" = "${RABBITMQ_CONFIG_FILE}" ]; then
125125
elif [ "x" != "x${RABBITMQ_CONFIG_FILE}" \
126126
-a "${RABBITMQ_CONFIG_FILE_NOEX}" != "${RABBITMQ_CONFIG_FILE}" ]; then
127127
# Config file has an extension, but it's neither .conf or .config
128-
echo "ERROR: Wrong extension for RABBITMQ_CONFIG_FILE: ${RABBITMQ_CONFIG_FILE}"
129-
echo "ERROR: extension should be either .conf or .config"
130-
exit 1
128+
_rmq_env_perr "Wrong extension for RABBITMQ_CONFIG_FILE: ${RABBITMQ_CONFIG_FILE}" \
129+
'The extension should be either .conf or .config'
130+
exit 64 # EX_USAGE
131131
fi
132132

133133
RABBITMQ_CONFIG_ARG_FILE_NOEX=$(get_noex ${RABBITMQ_CONFIG_ARG_FILE})
134134

135135
if [ "${RABBITMQ_CONFIG_ARG_FILE_NOEX}.config" != "${RABBITMQ_CONFIG_ARG_FILE}" ]; then
136136
if [ "${RABBITMQ_CONFIG_ARG_FILE}" = "${RABBITMQ_ADVANCED_CONFIG_FILE}" ]; then
137-
echo "ERROR: Wrong extension for RABBITMQ_ADVANCED_CONFIG_FILE: ${RABBITMQ_ADVANCED_CONFIG_FILE}"
138-
echo "ERROR: extension should be .config"
139-
exit 1
137+
_rmq_env_perr "Wrong extension for RABBITMQ_ADVANCED_CONFIG_FILE: ${RABBITMQ_ADVANCED_CONFIG_FILE}" \
138+
'The extension should be .config'
139+
exit 64 # EX_USAGE
140140
else
141141
# We should never got here, but still there should be some explanation
142-
echo "ERROR: Wrong extension for ${RABBITMQ_CONFIG_ARG_FILE}"
143-
echo "ERROR: extension should be .config"
144-
exit 1
142+
_rmq_env_perr "Wrong extension for ${RABBITMQ_CONFIG_ARG_FILE}"
143+
'The extension should be .config'
144+
exit 64 # EX_USAGE
145145
fi
146146
fi
147147

src/rabbit.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ boot_error(Class, Reason) ->
880880

881881
log_boot_error_and_exit(Reason, Format, Args) ->
882882
rabbit_log:error(Format, Args),
883-
io:format("~nBOOT FAILED~n===========~n" ++ Format ++ "~n", Args),
883+
io:format(standard_error, "~nBOOT FAILED~n===========~n" ++ Format ++ "~n", Args),
884884
timer:sleep(1000),
885885
exit(Reason).
886886

src/rabbit_config.erl

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -255,38 +255,46 @@ assert_config(none, _) -> ok;
255255
assert_config(Filename, Env) ->
256256
assert_config(filename:extension(Filename), Filename, Env).
257257

258+
-define(ERRMSG_INDENT, " ").
259+
258260
assert_config(".config", Filename, Env) ->
259261
case filelib:is_regular(Filename) of
260262
true ->
261263
case file:consult(Filename) of
262264
{ok, []} -> {error,
263-
{"ERROR: Config file ~s should not be empty: ~s",
265+
{"Config file ~s should not be empty: ~s",
264266
[Env, Filename]}};
265267
{ok, [_]} -> ok;
266268
{ok, [_|_]} -> {error,
267-
{"ERROR: Config file ~s must contain ONE list ended by <dot>: ~s",
269+
{"Config file ~s must contain ONE list ended by <dot>: ~s",
268270
[Env, Filename]}};
269271
{error, {1, erl_parse, Err}} ->
270-
{error, {"ERROR: Unable to parse erlang terms from ~s file: ~s~n"
271-
"ERROR: Reason: ~p~n"
272-
"ERROR: Check that the file is in the erlang terms format. " ++
272+
% Note: the sequence of spaces is to indent from the [error] prefix, like this:
273+
%
274+
% 2018-09-06 07:05:40.225 [error] Unable to parse erlang terms from RABBITMQ_ADVANCED_CONFIG_FILE...
275+
% Reason: ["syntax error before: ",[]]
276+
{error, {"Unable to parse erlang terms from ~s file: ~s~n"
277+
?ERRMSG_INDENT
278+
"Reason: ~p~n"
279+
?ERRMSG_INDENT
280+
"Check that the file is in erlang term format. " ++
273281
case Env of
274282
"RABBITMQ_CONFIG_FILE" ->
275-
"If you are using the new ini-style format, the file "
276-
"extension should be '.conf'~n";
283+
"If you are using the new ini-style format, the file extension should be '.conf'~n";
277284
_ -> ""
278285
end,
279286
[Env, Filename, Err]}};
280287
{error, Err} ->
281-
{error, {"ERROR Unable to parse erlang terms from ~s file: ~s~n"
288+
{error, {"Unable to parse erlang terms from ~s file: ~s~n"
289+
?ERRMSG_INDENT
282290
"Error: ~p~n",
283291
[Env, Filename, Err]}}
284292
end;
285293
false ->
286294
ok
287295
end;
288296
assert_config(BadExt, Filename, Env) ->
289-
{error, {"ERROR: '~s': Expected extension '.config', got extension '~s' for file '~s'~n", [Env, BadExt, Filename]}}.
297+
{error, {"'~s': Expected extension '.config', got extension '~s' for file '~s'~n", [Env, BadExt, Filename]}}.
290298

291299
assert_conf("", _) -> ok;
292300
assert_conf(Filename, Env) ->
@@ -298,8 +306,9 @@ assert_conf(".conf", Filename, Env) ->
298306
case file:consult(Filename) of
299307
{ok, []} -> ok;
300308
{ok, _} ->
301-
{error, {"ERROR: Wrong format of the config file ~s: ~s~n"
302-
"ERROR: Check that the file is in the new ini-style config format "
309+
{error, {"Wrong format of the config file ~s: ~s~n"
310+
?ERRMSG_INDENT
311+
"Check that the file is in the new ini-style config format. "
303312
"If you are using the old format the file extension should "
304313
"be .config~n",
305314
[Env, Filename]}};
@@ -310,4 +319,4 @@ assert_conf(".conf", Filename, Env) ->
310319
ok
311320
end;
312321
assert_conf(BadExt, Filename, Env) ->
313-
{error, {"ERROR: '~s': Expected extension '.config', got extension '~s' for file '~s'~n", [Env, BadExt, Filename]}}.
322+
{error, {"'~s': Expected extension '.config', got extension '~s' for file '~s'~n", [Env, BadExt, Filename]}}.

src/rabbit_prelaunch.erl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
-define(SET_DIST_PORT, 0).
2727
-define(ERROR_CODE, 1).
2828
-define(DO_NOT_SET_DIST_PORT, 2).
29+
-define(EX_USAGE, 64).
2930

3031
%%----------------------------------------------------------------------------
3132
%% Specs
@@ -62,9 +63,12 @@ stop() ->
6263
config_file_check() ->
6364
case rabbit_config:validate_config_files() of
6465
ok -> ok;
65-
{error, {Err, Args}} ->
66-
io:format(Err, Args),
67-
rabbit_misc:quit(?ERROR_CODE)
66+
{error, {ErrFmt, ErrArgs}} ->
67+
ErrMsg = io_lib:format(ErrFmt, ErrArgs),
68+
{{Year, Month, Day}, {Hour, Minute, Second, Milli}} = lager_util:localtime_ms(),
69+
io:format(standard_error, "~b-~2..0b-~2..0b ~2..0b:~2..0b:~2..0b.~b [error] ~s",
70+
[Year, Month, Day, Hour, Minute, Second, Milli, ErrMsg]),
71+
rabbit_misc:quit(?EX_USAGE)
6872
end.
6973

7074
%% Check whether a node with the same name is already running

0 commit comments

Comments
 (0)