feat: enhance logging functions with color support and improved formatting

This commit is contained in:
Menno van Leeuwen 2025-03-11 10:59:55 +01:00
parent 59fc5e5e10
commit c58fa69e2e
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -9,27 +9,99 @@ readonly GIT_REPO="https://git.mvl.sh/vleeuwenmenno/dotfiles.git" # Dotfiles r
readonly DOTFILES_PATH="${HOME}/.dotfiles" # Dotfiles directory
readonly SETUP_MARKER="${HOME}/.dotfiles-setup" # Setup marker file indicates setup has been run
# Color constants
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[0;33m'
readonly NC='\033[0m' # No Color
#Color print function, usage: println "message" "color"
println() {
color=$2
printfe "%s\n" $color "$1"
}
# print colored with printf (args: format, color, message ...)
printfe() {
format=$1
color=$2
message=$3
show_time=true
# Check if $4 is explicitly set to false, otherwise default to true
if [ ! -z "$4" ] && [ "$4" == "false" ]; then
show_time=false
fi
red=$(tput setaf 1)
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 4)
magenta=$(tput setaf 5)
cyan=$(tput setaf 6)
normal=$(tput sgr0)
grey=$(tput setaf 8)
case $color in
"red")
color=$red
;;
"green")
color=$green
;;
"yellow")
color=$yellow
;;
"blue")
color=$blue
;;
"magenta")
color=$magenta
;;
"cyan")
color=$cyan
;;
"grey")
color=$grey
;;
*)
color=$normal
;;
esac
if [ "$show_time" == "false" ]; then
printf "$color$format$normal" "$message"
return
fi
printf $grey"%s" "$(date +'%H:%M:%S')"$normal
case $color in
$green | $cyan | $blue | $magenta | $normal)
printf "$green INF $normal"
;;
$yellow)
printf "$yellow WRN $normal"
;;
$red)
printf "$red ERR $normal"
;;
*)
printf "$normal"
;;
esac
printf "$color$format$normal" "$message"
}
# Helper functions
log_info() {
echo -e "${YELLOW}$1${NC}"
println "$1" "green"
}
log_success() {
echo -e "${GREEN}$1${NC}"
println "$1" "green"
}
log_error() {
echo -e "${RED}$1${NC}" >&2
println "$1" "red" >&2
}
log_warning() {
echo -e "${YELLOW}$1${NC}" >&2
println "$1" "yellow" >&2
}
die() {