Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit a3199c3

Browse files
committed
#695: Adds logging output improvements.
1 parent 414a39b commit a3199c3

File tree

2 files changed

+114
-53
lines changed

2 files changed

+114
-53
lines changed

Diff for: src/etc/supervisord.d/00-reaper.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[program:reaper]
22
autorestart = false
33
autostart = %(ENV_ENABLE_REAPER)s
4-
command = /usr/sbin/reaper --monochrome --timeout %(ENV_REAPER_TIMEOUT)s --wall-timeout 30 --wall="Session expiring in 30 seconds."
4+
command = /usr/sbin/reaper --monochrome --verbose --timeout %(ENV_REAPER_TIMEOUT)s --wall-timeout 30 --wall="Session expiring in 30 seconds."
55
priority = 1
66
startsecs = 0
77
stderr_logfile = /dev/stderr

Diff for: src/usr/sbin/reaper

+113-52
Original file line numberDiff line numberDiff line change
@@ -39,73 +39,119 @@ function __print_message ()
3939
{
4040
local -r type="${1}"
4141

42-
local colour_negative='\033[1;31m'
43-
local colour_notice='\033[1;33m'
44-
local colour_positive='\033[1;32m'
42+
local colour_err='\033[1;31m'
43+
local colour_warning='\033[1;33m'
44+
local colour_notice='\033[1;32m'
45+
local colour_info='\033[1;37m'
46+
local colour_debug='\033[1;30m'
4547
local colour_reset='\033[0m'
4648
local exit_code="${3:-0}"
4749
local message="${2}"
50+
local output_debug="${output_debug:-false}"
51+
local output_verbose="${output_verbose:-false}"
52+
local output_quiet="${output_quiet:-false}"
53+
local output_silent="${output_silent:-false}"
4854
local prefix=""
49-
local quiet="${quiet:-false}"
50-
local silent="${silent:-false}"
55+
5156

5257
if [[ ${monochrome} == true ]]
5358
then
5459
unset \
55-
colour_negative \
60+
colour_err \
61+
colour_warning \
5662
colour_notice \
57-
colour_positive \
63+
colour_info \
64+
colour_debug \
5865
colour_reset
5966
fi
6067

6168
case "${type}" in
62-
error)
69+
err|error)
70+
prefix="$(
71+
printf -- \
72+
'%bERROR%b %s' \
73+
"${colour_err}" \
74+
"${colour_reset}" \
75+
"${0##*/}"
76+
)"
77+
;;
78+
warning|warn)
6379
prefix="$(
6480
printf -- \
65-
'%bERROR:%b ' \
66-
"${colour_negative}" \
67-
"${colour_reset}"
81+
'%bWARN%b %s' \
82+
"${colour_warning}" \
83+
"${colour_reset}" \
84+
"${0##*/}"
6885
)"
6986
;;
70-
warn)
87+
notice)
7188
prefix="$(
7289
printf -- \
73-
'%bWARN:%b ' \
90+
'%bNOTICE%b %s: ' \
7491
"${colour_notice}" \
75-
"${colour_reset}"
92+
"${colour_reset}" \
93+
"${0##*/}"
7694
)"
7795
;;
7896
info)
7997
prefix="$(
8098
printf -- \
81-
'%bINFO:%b ' \
82-
"${colour_positive}" \
83-
"${colour_reset}"
99+
'%bINFO%b %s: ' \
100+
"${colour_info}" \
101+
"${colour_reset}" \
102+
"${0##*/}"
103+
)"
104+
;;
105+
debug)
106+
prefix="$(
107+
printf -- \
108+
'%bDEBUG%b %s: ' \
109+
"${colour_debug}" \
110+
"${colour_reset}" \
111+
"${0##*/}"
84112
)"
85113
;;
86114
*)
87115
message="${type}"
88116
;;
89117
esac
90118

91-
if [[ ${quiet} == true ]] \
92-
&& [[ ! ${type} =~ ^(error|warn)$ ]]
119+
if [[ ${output_quiet} == true ]] \
120+
&& [[ ! ${type} =~ ^(err|error|warning|warn)$ ]]
93121
then
94122
return 0
95-
elif [[ ${silent} == true ]] \
96-
&& [[ ${type} != error ]]
123+
elif [[ ${output_silent} == true ]] \
124+
&& [[ ! ${type} =~ ^(err|error)$ ]]
97125
then
98126
return 0
99-
elif [[ ${silent} == true ]] \
100-
&& [[ ${type} == error ]]
127+
elif [[ ${output_silent} == true ]] \
128+
&& [[ ${type} =~ ^(err|error)$ ]]
101129
then
102130
return 1
103-
elif [[ ${type} == error ]]
131+
elif [[ ${type} =~ ^(err|error)$ ]]
104132
then
105-
>&2 printf -- \
106-
'%s%s\n' \
107-
"${prefix}" \
133+
logger \
134+
--priority "err" \
135+
--stderr \
136+
--tag "${prefix}" \
137+
-- \
108138
"${message}"
139+
elif [[ ${type} =~ ^(warning|warn)$ ]]
140+
then
141+
logger \
142+
--priority "warning" \
143+
--stderr \
144+
--tag "${prefix}" \
145+
-- \
146+
"${message}"
147+
elif [[ ${output_debug} != true ]] \
148+
&& [[ ${type} == debug ]]
149+
then
150+
return 0
151+
elif [[ ${output_verbose} != true ]] \
152+
&& [[ ${type} == info ]]
153+
then
154+
return 0
109155
else
110156
printf -- \
111157
'%s%s\n' \
@@ -167,39 +213,40 @@ function __reap ()
167213
function __usage ()
168214
{
169215
local help="${help:-false}"
170-
local quiet="${quiet:-false}"
171-
local silent="${silent:-false}"
216+
local output_quiet="${output_quiet:-false}"
217+
local output_silent="${output_silent:-false}"
172218

173-
if [[ ${silent} != true ]] \
219+
if [[ ${output_silent} != true ]] \
174220
|| [[ ${help} == true ]]
175221
then
176222
cat <<-USAGE
177223
178224
Usage: ${0##*/} [OPTIONS]
179-
${0##*/} [{-h|--help}]
180225
181226
Options:
182227
-g, --get KEY Used to get values from a running ${0##*/}
183228
process. The keys and values they return are:
184229
- start : returns session start timestamp.
185230
- end : returns session end timestamp.
186231
- ttl : returns remaining session time to live.
187-
-h, --help Show this help and exit.
232+
-h, --help Display help text and exit.
188233
--monochrome Output colour is suppressed.
189234
-p, --pid PID Send the termination signal to the process with
190235
the pid value PID.
191236
If not specified the default is pid 1.
192-
-q, --quiet Do not print information message output.
193-
-qq, --silent Do not print error message output.
237+
-q, --quiet Do not output notice, info or debug messages.
238+
-qq, --silent Do not output any messages.
194239
-s, --signal SIG Send the signal SIG to the process.
195240
If not specified the default is SIGTERM.
241+
-T, --wall-timeout SECONDS Set the time before session end to send the
242+
wall message. The default is 30 seconds.
243+
Set to 0 to disable sending a wall message.
196244
-t, --timeout SECONDS Time in seconds to wait before sending the
197245
signal to the process. The default is 0 seconds
198246
which indicates no delay.
247+
-v, --verbose Output info messages.
248+
-vv, --debug Output debug messages.
199249
-w, --wall MESSAGE Set a wall message to send before session end.
200-
-T, --wall-timeout SECONDS Set the time before session end to send the
201-
wall message. The default is 30 seconds.
202-
Set to 0 to disable sending a wall message.
203250
USAGE
204251
fi
205252

@@ -220,23 +267,24 @@ function main ()
220267
local get
221268
local help
222269
local monochrome="false"
270+
local output_debug="false"
271+
local output_verbose="false"
272+
local output_quiet="false"
273+
local output_silent="false"
223274
local pid="1"
224-
local quiet="false"
225275
local signal="TERM"
226-
local silent="false"
227276
local session_start
228277
local session_end
229278
local state_value
230279
local timeout="0"
231-
local verbose="false"
232280
local wall_message
233281
local wall_timeout="30"
234282

235283
if [[ ${EUID} -ne 0 ]]
236284
then
237285
__print_message \
238286
"error" \
239-
"${0##*/} must be run as root" \
287+
"must be run as root" \
240288
1
241289
fi
242290

@@ -268,12 +316,12 @@ function main ()
268316
shift 2 || break
269317
;;
270318
-q|--quiet)
271-
quiet="true"
319+
output_quiet="true"
272320
shift 1
273321
;;
274322
-qq|--silent)
275-
quiet="true"
276-
silent="true"
323+
output_quiet="true"
324+
output_silent="true"
277325
shift 1
278326
;;
279327
--signal=*)
@@ -293,7 +341,12 @@ function main ()
293341
shift 2 || break
294342
;;
295343
-v|--verbose)
296-
verbose="true"
344+
output_verbose="true"
345+
shift 1
346+
;;
347+
-vv|--debug)
348+
output_debug="true"
349+
output_verbose="true"
297350
shift 1
298351
;;
299352
--wall=*)
@@ -315,7 +368,7 @@ function main ()
315368
*)
316369
__print_message \
317370
"error" \
318-
"${0##*/} unknown option ${1}"
371+
"unknown option ${1}"
319372
__usage
320373
;;
321374
esac
@@ -352,7 +405,7 @@ function main ()
352405
*)
353406
__print_message \
354407
"error" \
355-
"${0##*/} unknown get value ${get}"
408+
"unknown get value ${get}"
356409
__usage
357410
;;
358411
esac
@@ -361,14 +414,14 @@ function main ()
361414
else
362415
__print_message \
363416
"error" \
364-
"${0##*/} lock detected - aborting" \
417+
"lock detected - aborting" \
365418
1
366419
fi
367420
elif [[ -n ${get} ]]
368421
then
369422
__print_message \
370423
"error" \
371-
"${0##*/} is not running"
424+
"is not running"
372425
__usage
373426
fi
374427

@@ -380,15 +433,15 @@ function main ()
380433
then
381434
__print_message \
382435
"error" \
383-
"${0##*/} invalid --timeout"
436+
"invalid --timeout"
384437
__usage
385438
fi
386439

387440
if ! __is_valid_wall_timeout "${wall_timeout}"
388441
then
389442
__print_message \
390443
"error" \
391-
"${0##*/} invalid --wall-timeout"
444+
"invalid --wall-timeout"
392445
__usage
393446
fi
394447

@@ -409,6 +462,10 @@ function main ()
409462

410463
if (( timeout > 0 ))
411464
then
465+
__print_message \
466+
"info" \
467+
"session will expire after ${timeout} seconds"
468+
412469
if coproc read -t "$(( ${timeout} - ${wall_timeout} ))"
413470
then
414471
wait "${!}" || :
@@ -425,9 +482,13 @@ function main ()
425482
fi
426483
fi
427484

485+
__print_message \
486+
"info" \
487+
"expiring session after ${timeout} seconds"
488+
428489
__print_message \
429490
"warn" \
430-
"${0##*/} expiring session"
491+
"expiring session"
431492

432493
exit 0
433494
}

0 commit comments

Comments
 (0)