@@ -39,73 +39,119 @@ function __print_message ()
39
39
{
40
40
local -r type=" ${1} "
41
41
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'
45
47
local colour_reset=' \033[0m'
46
48
local exit_code=" ${3:- 0} "
47
49
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} "
48
54
local prefix=" "
49
- local quiet=" ${quiet:- false} "
50
- local silent=" ${silent:- false} "
55
+
51
56
52
57
if [[ ${monochrome} == true ]]
53
58
then
54
59
unset \
55
- colour_negative \
60
+ colour_err \
61
+ colour_warning \
56
62
colour_notice \
57
- colour_positive \
63
+ colour_info \
64
+ colour_debug \
58
65
colour_reset
59
66
fi
60
67
61
68
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)
63
79
prefix=" $(
64
80
printf -- \
65
- ' %bERROR:%b ' \
66
- " ${colour_negative} " \
67
- " ${colour_reset} "
81
+ ' %bWARN%b %s' \
82
+ " ${colour_warning} " \
83
+ " ${colour_reset} " \
84
+ " ${0##*/ } "
68
85
) "
69
86
;;
70
- warn )
87
+ notice )
71
88
prefix=" $(
72
89
printf -- \
73
- ' %bWARN:%b ' \
90
+ ' %bNOTICE%b %s: ' \
74
91
" ${colour_notice} " \
75
- " ${colour_reset} "
92
+ " ${colour_reset} " \
93
+ " ${0##*/ } "
76
94
) "
77
95
;;
78
96
info)
79
97
prefix=" $(
80
98
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##*/ } "
84
112
) "
85
113
;;
86
114
* )
87
115
message=" ${type} "
88
116
;;
89
117
esac
90
118
91
- if [[ ${quiet } == true ]] \
92
- && [[ ! ${type} =~ ^(error| warn)$ ]]
119
+ if [[ ${output_quiet } == true ]] \
120
+ && [[ ! ${type} =~ ^(err | error| warning | warn)$ ]]
93
121
then
94
122
return 0
95
- elif [[ ${silent } == true ]] \
96
- && [[ ${type} != error ]]
123
+ elif [[ ${output_silent } == true ]] \
124
+ && [[ ! ${type} =~ ^(err | error)$ ]]
97
125
then
98
126
return 0
99
- elif [[ ${silent } == true ]] \
100
- && [[ ${type} == error ]]
127
+ elif [[ ${output_silent } == true ]] \
128
+ && [[ ${type} =~ ^(err | error)$ ]]
101
129
then
102
130
return 1
103
- elif [[ ${type} == error ]]
131
+ elif [[ ${type} =~ ^(err | error)$ ]]
104
132
then
105
- >&2 printf -- \
106
- ' %s%s\n' \
107
- " ${prefix} " \
133
+ logger \
134
+ --priority " err" \
135
+ --stderr \
136
+ --tag " ${prefix} " \
137
+ -- \
108
138
" ${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
109
155
else
110
156
printf -- \
111
157
' %s%s\n' \
@@ -167,39 +213,40 @@ function __reap ()
167
213
function __usage ()
168
214
{
169
215
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} "
172
218
173
- if [[ ${silent } != true ]] \
219
+ if [[ ${output_silent } != true ]] \
174
220
|| [[ ${help} == true ]]
175
221
then
176
222
cat << -USAGE
177
223
178
224
Usage: ${0##*/ } [OPTIONS]
179
- ${0##*/ } [{-h|--help}]
180
225
181
226
Options:
182
227
-g, --get KEY Used to get values from a running ${0##*/ }
183
228
process. The keys and values they return are:
184
229
- start : returns session start timestamp.
185
230
- end : returns session end timestamp.
186
231
- ttl : returns remaining session time to live.
187
- -h, --help Show this help and exit.
232
+ -h, --help Display help text and exit.
188
233
--monochrome Output colour is suppressed.
189
234
-p, --pid PID Send the termination signal to the process with
190
235
the pid value PID.
191
236
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 .
194
239
-s, --signal SIG Send the signal SIG to the process.
195
240
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.
196
244
-t, --timeout SECONDS Time in seconds to wait before sending the
197
245
signal to the process. The default is 0 seconds
198
246
which indicates no delay.
247
+ -v, --verbose Output info messages.
248
+ -vv, --debug Output debug messages.
199
249
-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.
203
250
USAGE
204
251
fi
205
252
@@ -220,23 +267,24 @@ function main ()
220
267
local get
221
268
local help
222
269
local monochrome=" false"
270
+ local output_debug=" false"
271
+ local output_verbose=" false"
272
+ local output_quiet=" false"
273
+ local output_silent=" false"
223
274
local pid=" 1"
224
- local quiet=" false"
225
275
local signal=" TERM"
226
- local silent=" false"
227
276
local session_start
228
277
local session_end
229
278
local state_value
230
279
local timeout=" 0"
231
- local verbose=" false"
232
280
local wall_message
233
281
local wall_timeout=" 30"
234
282
235
283
if [[ ${EUID} -ne 0 ]]
236
284
then
237
285
__print_message \
238
286
" error" \
239
- " ${0 ##*/ } must be run as root" \
287
+ " must be run as root" \
240
288
1
241
289
fi
242
290
@@ -268,12 +316,12 @@ function main ()
268
316
shift 2 || break
269
317
;;
270
318
-q|--quiet)
271
- quiet =" true"
319
+ output_quiet =" true"
272
320
shift 1
273
321
;;
274
322
-qq|--silent)
275
- quiet =" true"
276
- silent =" true"
323
+ output_quiet =" true"
324
+ output_silent =" true"
277
325
shift 1
278
326
;;
279
327
--signal=* )
@@ -293,7 +341,12 @@ function main ()
293
341
shift 2 || break
294
342
;;
295
343
-v|--verbose)
296
- verbose=" true"
344
+ output_verbose=" true"
345
+ shift 1
346
+ ;;
347
+ -vv|--debug)
348
+ output_debug=" true"
349
+ output_verbose=" true"
297
350
shift 1
298
351
;;
299
352
--wall=* )
@@ -315,7 +368,7 @@ function main ()
315
368
* )
316
369
__print_message \
317
370
" error" \
318
- " ${0 ##*/ } unknown option ${1} "
371
+ " unknown option ${1} "
319
372
__usage
320
373
;;
321
374
esac
@@ -352,7 +405,7 @@ function main ()
352
405
* )
353
406
__print_message \
354
407
" error" \
355
- " ${0 ##*/ } unknown get value ${get} "
408
+ " unknown get value ${get} "
356
409
__usage
357
410
;;
358
411
esac
@@ -361,14 +414,14 @@ function main ()
361
414
else
362
415
__print_message \
363
416
" error" \
364
- " ${0 ##*/ } lock detected - aborting" \
417
+ " lock detected - aborting" \
365
418
1
366
419
fi
367
420
elif [[ -n ${get} ]]
368
421
then
369
422
__print_message \
370
423
" error" \
371
- " ${0 ##*/ } is not running"
424
+ " is not running"
372
425
__usage
373
426
fi
374
427
@@ -380,15 +433,15 @@ function main ()
380
433
then
381
434
__print_message \
382
435
" error" \
383
- " ${0 ##*/ } invalid --timeout"
436
+ " invalid --timeout"
384
437
__usage
385
438
fi
386
439
387
440
if ! __is_valid_wall_timeout " ${wall_timeout} "
388
441
then
389
442
__print_message \
390
443
" error" \
391
- " ${0 ##*/ } invalid --wall-timeout"
444
+ " invalid --wall-timeout"
392
445
__usage
393
446
fi
394
447
@@ -409,6 +462,10 @@ function main ()
409
462
410
463
if (( timeout > 0 ))
411
464
then
465
+ __print_message \
466
+ " info" \
467
+ " session will expire after ${timeout} seconds"
468
+
412
469
if coproc read -t " $(( ${timeout} - ${wall_timeout} )) "
413
470
then
414
471
wait " ${! } " || :
@@ -425,9 +482,13 @@ function main ()
425
482
fi
426
483
fi
427
484
485
+ __print_message \
486
+ " info" \
487
+ " expiring session after ${timeout} seconds"
488
+
428
489
__print_message \
429
490
" warn" \
430
- " ${0 ##*/ } expiring session"
491
+ " expiring session"
431
492
432
493
exit 0
433
494
}
0 commit comments