2011-04-26 21:39:44 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# libmsgmng.lib -- @package@ library to manage debug/warning/error messages
|
2012-11-08 22:09:52 +01:00
|
|
|
# Copyright (C) 2005,2006,2008,2012 Davide Madrisan <davide.madrisan@gmail.com>
|
2011-04-26 21:39:44 +02:00
|
|
|
|
2012-11-25 15:54:34 +01:00
|
|
|
[ "$libmsgmng_is_loaded" = 1 ] || {
|
|
|
|
libmsgmng_is_loaded=1
|
|
|
|
|
2011-04-26 21:39:44 +02:00
|
|
|
[ -z "$BASH" ] || [ ${BASH_VERSION:0:1} -lt 2 ] &&
|
|
|
|
echo $"this script requires bash version 2 or better" >&2 && exit 1
|
|
|
|
|
2012-11-30 22:02:42 +01:00
|
|
|
[ -r @libdir@/libtranslate.lib ] ||
|
2012-12-01 17:40:21 +01:00
|
|
|
{ echo "\
|
|
|
|
libmsgmng.lib: "$"library not found"": @libdir@/libtranslate.lib" 1>&2
|
2012-11-30 22:02:42 +01:00
|
|
|
exit 1; }
|
|
|
|
. @libdir@/libtranslate.lib
|
2011-04-26 21:39:44 +02:00
|
|
|
|
|
|
|
# notify family of message handling functions:
|
2012-12-01 22:50:46 +01:00
|
|
|
# - notify.debug
|
2011-04-26 21:39:44 +02:00
|
|
|
# - notify.warning
|
|
|
|
# - notify.error
|
|
|
|
# - notify.note
|
|
|
|
# args:
|
|
|
|
# $1 : message to print
|
|
|
|
# note:
|
|
|
|
# 1. debug messages are only printed if 'verbose' is great then '0'
|
|
|
|
# 2. error notifications are followed by a call to 'EXIT_FUNC'
|
|
|
|
# or 'exit' if this variable is unset
|
|
|
|
# (can set to 'return' for instance)
|
|
|
|
|
|
|
|
notify_def_msg="<"$"no message"">"
|
|
|
|
|
2012-12-01 22:50:46 +01:00
|
|
|
function notify.debug() {
|
|
|
|
[ ${verbose:-0} -gt 1 ] || return
|
2012-12-09 00:12:57 +01:00
|
|
|
echo -e "${WARN}"$"DEBUG""${NORM}: ${1:-$notify_def_msg}" 1>&2
|
2012-12-01 22:50:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
notify.debug $"loading"": \`libmsgmng.lib'..."
|
|
|
|
|
2011-04-26 21:39:44 +02:00
|
|
|
function notify.warning() {
|
2012-12-09 00:12:57 +01:00
|
|
|
echo -e "${WARN}"$"WARNING""${NORM}: ${1:-$notify_def_msg}" 1>&2
|
2011-04-26 21:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function notify.error() {
|
2012-12-09 00:12:57 +01:00
|
|
|
echo -e "${EXIT}"$"ERROR""${NORM}: ${EXIT}${1:-$notify_def_msg}${NORM}" 1>&2
|
2011-04-26 21:39:44 +02:00
|
|
|
${EXIT_FUNC:-"exit"} 1
|
|
|
|
}
|
|
|
|
|
|
|
|
function notify.note() {
|
|
|
|
[ ${verbose:-0} -ge 0 ] || return
|
|
|
|
echo -e "${1:-$notify_def_msg}"
|
|
|
|
}
|
2012-11-10 11:52:20 +01:00
|
|
|
|
2012-11-10 15:47:12 +01:00
|
|
|
# function notify.enable_colors
|
|
|
|
# enable if possible the colorized output
|
|
|
|
# args:
|
|
|
|
# none
|
|
|
|
function notify.enable_colors() {
|
2012-11-11 19:33:38 +01:00
|
|
|
local color_scheme="${1:-default}"
|
|
|
|
if [ -r ~/.@package@.d/color-theme.${color_scheme} ]; then
|
|
|
|
. ~/.@package@.d/color-theme.${color_scheme}
|
|
|
|
notify.debug "loading ~/.@package@.d/color-theme.${color_scheme}"
|
|
|
|
elif [ -r /etc/@package@.d/color-theme.${color_scheme} ]; then
|
2012-11-11 18:56:52 +01:00
|
|
|
. /etc/@package@.d/color-theme.${color_scheme}
|
|
|
|
notify.debug "loading /etc/@package@.d/color-theme.${color_scheme}"
|
2012-11-10 21:01:37 +01:00
|
|
|
else
|
|
|
|
notify.warning \
|
2012-11-11 18:56:52 +01:00
|
|
|
$"unknown color theme \`$color_scheme': colors will be disabled"
|
2012-11-10 21:01:37 +01:00
|
|
|
ESC= CRIT= NOTE= WARN= EXIT= NORM=
|
|
|
|
fi
|
2012-11-10 15:47:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# function notify.disable_colors
|
|
|
|
# disable the colorized output
|
|
|
|
# args:
|
|
|
|
# none
|
|
|
|
function notify.disable_colors() {
|
|
|
|
ESC= CRIT= NOTE= WARN= EXIT= NORM=
|
|
|
|
}
|
2012-11-25 15:54:34 +01:00
|
|
|
|
|
|
|
} # endif $libmsgmng_is_loaded
|