Better formatting for log messages
Simplify + add TTY codes in log messages Story: 2010226 Task: 46374 Signed-off-by: Davlet Panech <davlet.panech@windriver.com> Change-Id: Ie61c70bcdac199c530fd0a6e02fb00d63b9805b7
This commit is contained in:
parent
700a78477c
commit
838a7713b8
@ -36,28 +36,28 @@ COREUTILS_DOCKER_IMG="debian:bullseye-20220509"
|
||||
APT_UTILS_DOCKER_IMG="debian:bullseye-20220509"
|
||||
|
||||
notice() {
|
||||
( set +x ; print_log -i --loud "$@" ; )
|
||||
( set +x ; print_log -i --notice "$@" ; )
|
||||
}
|
||||
|
||||
info() {
|
||||
( set +x ; print_log -i --prefix ">>> " "$@" ; )
|
||||
( set +x ; print_log -i --info "$@" ; )
|
||||
}
|
||||
|
||||
error() {
|
||||
( set +x ; print_log -i --loud --dump-stack --location --prefix "ERROR: " "$@" ; )
|
||||
( set +x ; print_log -i --error --location --dump-stack "$@" ; )
|
||||
}
|
||||
|
||||
warn() {
|
||||
( set +x; print_log -i --prefix "WARNING: " --location "$@" ; )
|
||||
( set +x; print_log -i --warning --location --dump-stack "$@" ; )
|
||||
}
|
||||
|
||||
die() {
|
||||
( set +x ; print_log -i --loud --dump-stack --location --prefix "ERROR: " "$@" ; )
|
||||
( set +x ; print_log -i --error --location --dump-stack "$@" ; )
|
||||
exit 1
|
||||
}
|
||||
|
||||
bail() {
|
||||
( set +x ; print_log -i --prefix ">>> " "$@" ; )
|
||||
( set +x ; print_log -i --notice "$@" ; )
|
||||
exit 0
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,8 @@ dump_stack() {
|
||||
#
|
||||
# Usage: print_log [OPTIONS...] LINES...
|
||||
#
|
||||
# Print a log message -- LINES... followed by RAW_LINES...
|
||||
# Print a log message; each LINE is printed on a separate line and should
|
||||
# be quoted in scripts.
|
||||
#
|
||||
# --dump-stack include stack trace in output
|
||||
# --frame-offset=N frame offset for stack trace (default: 0)
|
||||
@ -36,6 +37,11 @@ dump_stack() {
|
||||
# -i,--increment-frame-offset
|
||||
# add one to frame-offset (additive)
|
||||
#
|
||||
# --error set --prefix --loud + TTY codes as necessary
|
||||
# --warning --warn
|
||||
# --notice
|
||||
# --info
|
||||
#
|
||||
__print_log_usage() {
|
||||
local func="${FUNCNAME[1]}"
|
||||
echo "
|
||||
@ -43,7 +49,7 @@ __print_log_usage() {
|
||||
ERROR: ${func}: invalid syntax
|
||||
$(dump_stack 2)
|
||||
|
||||
Usage: $func [OPTIONS...] LINES... RAW_LINES...
|
||||
Usage: $func [OPTIONS...] LINES...
|
||||
See ${BASH_SOURCE[0]} near line ${LINENO} for more info.
|
||||
################################################################################
|
||||
"
|
||||
@ -58,8 +64,20 @@ print_log() {
|
||||
local line_prefix
|
||||
local epilog
|
||||
local -i include_location=0
|
||||
local loud
|
||||
local loud_prefix loud_suffix
|
||||
local loud_line_prefix
|
||||
local preset_line_prefix
|
||||
|
||||
# color codes
|
||||
local tty_error tty_warning tty_notice tty_info
|
||||
local tty_on tty_off
|
||||
if [[ -t 1 ]] ; then
|
||||
tty_error=$'\033[1;31m'
|
||||
tty_warning=$'\033[0;33m'
|
||||
tty_notice=$'\033[0;32m'
|
||||
tty_off=$'\033[0m'
|
||||
fi
|
||||
|
||||
# parse command line
|
||||
local opts
|
||||
@ -73,6 +91,10 @@ print_log() {
|
||||
-l location\
|
||||
-l epilog: \
|
||||
-l loud \
|
||||
-l error \
|
||||
-l warning -l warn \
|
||||
-l notice \
|
||||
-l info \
|
||||
-- "$@"
|
||||
)"
|
||||
if [[ $? -ne 0 ]] ; then
|
||||
@ -107,10 +129,28 @@ print_log() {
|
||||
shift 2
|
||||
;;
|
||||
--loud)
|
||||
local nl=$'\n'
|
||||
loud_line_prefix=$'### '
|
||||
loud_prefix="${nl}${nl}${loud_line_prefix}${nl}"
|
||||
loud_suffix="${loud_line_prefix}${nl}${nl}"
|
||||
loud="yes"
|
||||
shift
|
||||
;;
|
||||
--error)
|
||||
loud="yes"
|
||||
preset_line_prefix='ERROR: '
|
||||
tty_on="$tty_error"
|
||||
shift
|
||||
;;
|
||||
--warning | --warn)
|
||||
loud="yes"
|
||||
preset_line_prefix='WARNING: '
|
||||
tty_on="$tty_warning"
|
||||
shift
|
||||
;;
|
||||
--notice)
|
||||
loud="yes"
|
||||
tty_on="$tty_notice"
|
||||
shift
|
||||
;;
|
||||
--info)
|
||||
preset_line_prefix='# '
|
||||
shift
|
||||
;;
|
||||
--)
|
||||
@ -131,23 +171,37 @@ print_log() {
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -z "$line_prefix" ]] ; then
|
||||
line_prefix="$preset_line_prefix"
|
||||
fi
|
||||
|
||||
if [[ "$loud" = "yes" ]] ; then
|
||||
local nl=$'\n'
|
||||
loud_line_prefix=$'### '
|
||||
loud_prefix="${nl}${nl}${loud_line_prefix}${nl}"
|
||||
loud_suffix="${loud_line_prefix}${nl}${nl}"
|
||||
fi
|
||||
|
||||
let frame_offset+=frame_offset_offset
|
||||
|
||||
local location
|
||||
if [[ $include_location -eq 1 ]] ; then
|
||||
local -i funcname_index=$frame_offset
|
||||
location="${FUNCNAME[$funcname_index]}: "
|
||||
local func="${FUNCNAME[$funcname_index]}"
|
||||
if [[ -n "$func" && "$func" != "main" ]] ; then
|
||||
location="$func: "
|
||||
fi
|
||||
fi
|
||||
|
||||
echo -n "$loud_prefix"
|
||||
while [[ "$#" -gt 0 ]] ; do
|
||||
local line="$1" ; shift || true
|
||||
echo "${location}${loud_line_prefix}${line_prefix}${line}"
|
||||
echo "${loud_line_prefix}${location}${tty_on}${line_prefix}${line}${tty_off}"
|
||||
done
|
||||
shift || true
|
||||
if [[ $dump_stack -eq 1 ]] ; then
|
||||
let dump_stack_frame_offset=frame_offset+1
|
||||
dump_stack $dump_stack_frame_offset
|
||||
dump_stack $dump_stack_frame_offset | awk -v PREFIX="${loud_line_prefix}" -v SUFFIX="" '{print PREFIX, $0, SUFFIX}'
|
||||
fi
|
||||
if [[ -n "$epilog" ]] ; then
|
||||
echo -n "$epilog"
|
||||
|
Loading…
x
Reference in New Issue
Block a user