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"
|
APT_UTILS_DOCKER_IMG="debian:bullseye-20220509"
|
||||||
|
|
||||||
notice() {
|
notice() {
|
||||||
( set +x ; print_log -i --loud "$@" ; )
|
( set +x ; print_log -i --notice "$@" ; )
|
||||||
}
|
}
|
||||||
|
|
||||||
info() {
|
info() {
|
||||||
( set +x ; print_log -i --prefix ">>> " "$@" ; )
|
( set +x ; print_log -i --info "$@" ; )
|
||||||
}
|
}
|
||||||
|
|
||||||
error() {
|
error() {
|
||||||
( set +x ; print_log -i --loud --dump-stack --location --prefix "ERROR: " "$@" ; )
|
( set +x ; print_log -i --error --location --dump-stack "$@" ; )
|
||||||
}
|
}
|
||||||
|
|
||||||
warn() {
|
warn() {
|
||||||
( set +x; print_log -i --prefix "WARNING: " --location "$@" ; )
|
( set +x; print_log -i --warning --location --dump-stack "$@" ; )
|
||||||
}
|
}
|
||||||
|
|
||||||
die() {
|
die() {
|
||||||
( set +x ; print_log -i --loud --dump-stack --location --prefix "ERROR: " "$@" ; )
|
( set +x ; print_log -i --error --location --dump-stack "$@" ; )
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
bail() {
|
bail() {
|
||||||
( set +x ; print_log -i --prefix ">>> " "$@" ; )
|
( set +x ; print_log -i --notice "$@" ; )
|
||||||
exit 0
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,7 +25,8 @@ dump_stack() {
|
|||||||
#
|
#
|
||||||
# Usage: print_log [OPTIONS...] LINES...
|
# 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
|
# --dump-stack include stack trace in output
|
||||||
# --frame-offset=N frame offset for stack trace (default: 0)
|
# --frame-offset=N frame offset for stack trace (default: 0)
|
||||||
@ -36,6 +37,11 @@ dump_stack() {
|
|||||||
# -i,--increment-frame-offset
|
# -i,--increment-frame-offset
|
||||||
# add one to frame-offset (additive)
|
# add one to frame-offset (additive)
|
||||||
#
|
#
|
||||||
|
# --error set --prefix --loud + TTY codes as necessary
|
||||||
|
# --warning --warn
|
||||||
|
# --notice
|
||||||
|
# --info
|
||||||
|
#
|
||||||
__print_log_usage() {
|
__print_log_usage() {
|
||||||
local func="${FUNCNAME[1]}"
|
local func="${FUNCNAME[1]}"
|
||||||
echo "
|
echo "
|
||||||
@ -43,7 +49,7 @@ __print_log_usage() {
|
|||||||
ERROR: ${func}: invalid syntax
|
ERROR: ${func}: invalid syntax
|
||||||
$(dump_stack 2)
|
$(dump_stack 2)
|
||||||
|
|
||||||
Usage: $func [OPTIONS...] LINES... RAW_LINES...
|
Usage: $func [OPTIONS...] LINES...
|
||||||
See ${BASH_SOURCE[0]} near line ${LINENO} for more info.
|
See ${BASH_SOURCE[0]} near line ${LINENO} for more info.
|
||||||
################################################################################
|
################################################################################
|
||||||
"
|
"
|
||||||
@ -58,8 +64,20 @@ print_log() {
|
|||||||
local line_prefix
|
local line_prefix
|
||||||
local epilog
|
local epilog
|
||||||
local -i include_location=0
|
local -i include_location=0
|
||||||
|
local loud
|
||||||
local loud_prefix loud_suffix
|
local loud_prefix loud_suffix
|
||||||
local loud_line_prefix
|
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
|
# parse command line
|
||||||
local opts
|
local opts
|
||||||
@ -73,6 +91,10 @@ print_log() {
|
|||||||
-l location\
|
-l location\
|
||||||
-l epilog: \
|
-l epilog: \
|
||||||
-l loud \
|
-l loud \
|
||||||
|
-l error \
|
||||||
|
-l warning -l warn \
|
||||||
|
-l notice \
|
||||||
|
-l info \
|
||||||
-- "$@"
|
-- "$@"
|
||||||
)"
|
)"
|
||||||
if [[ $? -ne 0 ]] ; then
|
if [[ $? -ne 0 ]] ; then
|
||||||
@ -107,10 +129,28 @@ print_log() {
|
|||||||
shift 2
|
shift 2
|
||||||
;;
|
;;
|
||||||
--loud)
|
--loud)
|
||||||
local nl=$'\n'
|
loud="yes"
|
||||||
loud_line_prefix=$'### '
|
shift
|
||||||
loud_prefix="${nl}${nl}${loud_line_prefix}${nl}"
|
;;
|
||||||
loud_suffix="${loud_line_prefix}${nl}${nl}"
|
--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
|
shift
|
||||||
;;
|
;;
|
||||||
--)
|
--)
|
||||||
@ -131,23 +171,37 @@ print_log() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
let frame_offset+=frame_offset_offset
|
||||||
|
|
||||||
local location
|
local location
|
||||||
if [[ $include_location -eq 1 ]] ; then
|
if [[ $include_location -eq 1 ]] ; then
|
||||||
local -i funcname_index=$frame_offset
|
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
|
fi
|
||||||
|
|
||||||
echo -n "$loud_prefix"
|
echo -n "$loud_prefix"
|
||||||
while [[ "$#" -gt 0 ]] ; do
|
while [[ "$#" -gt 0 ]] ; do
|
||||||
local line="$1" ; shift || true
|
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
|
done
|
||||||
shift || true
|
shift || true
|
||||||
if [[ $dump_stack -eq 1 ]] ; then
|
if [[ $dump_stack -eq 1 ]] ; then
|
||||||
let dump_stack_frame_offset=frame_offset+1
|
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
|
fi
|
||||||
if [[ -n "$epilog" ]] ; then
|
if [[ -n "$epilog" ]] ; then
|
||||||
echo -n "$epilog"
|
echo -n "$epilog"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user