Use functions for logging

To prevent duplicate logic for formatting and writing out
log messages, a standardized function is made for logging messages
and logging errors.
This commit is contained in:
M.B. Christiansen 2022-10-10 10:52:33 +02:00
parent 51fb3ec6d4
commit a293b54818
1 changed files with 22 additions and 16 deletions

38
kunst
View File

@ -13,6 +13,7 @@ MUSIC_DIR=~/Music
SIZE=250x250 SIZE=250x250
POSITION="+0+0" POSITION="+0+0"
ONLINE_ALBUM_ART=false ONLINE_ALBUM_ART=false
PROG_NAME=$(basename "$0")
show_help() { show_help() {
printf "%s" "\ printf "%s" "\
@ -34,6 +35,15 @@ optional arguments:
" "
} }
log() {
[ ! "$SILENT" ] && echo "$PROG_NAME: $1"
}
logError() {
echo "$PROG_NAME: $1" >&2
exit 1
}
# Parse the arguments # Parse the arguments
options=$(getopt -o h --long position:,size:,music_dir:,version,force-online,silent,help -- "$@") options=$(getopt -o h --long position:,size:,music_dir:,version,force-online,silent,help -- "$@")
@ -90,7 +100,7 @@ is_connected() {
if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then if ping -q -c 1 -W 1 api.deezer.com >/dev/null; then
connected=true connected=true
else else
[ ! "$SILENT" ] && echo "kunst: unable to check online for the album art" log "unable to check online for the album art"
connected=false connected=false
fi fi
} }
@ -120,10 +130,10 @@ get_cover_online() {
IMG_URL="${ALBUM:-$ARTIST}" IMG_URL="${ALBUM:-$ARTIST}"
if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then if [ "$IMG_URL" = '' ] || [ "$IMG_URL" = 'null' ];then
[ ! "$SILENT" ] && echo "error: cover not found online" log "cover not found online"
ARTLESS=true ARTLESS=true
else else
[ ! "$SILENT" ] && echo "kunst: cover found online" log "cover found online"
curl -o "$COVER" -s "$IMG_URL" curl -o "$COVER" -s "$IMG_URL"
ARTLESS=false ARTLESS=false
fi fi
@ -134,7 +144,7 @@ find_album_art(){
# Check if the user wants to get the album art from the internet, # Check if the user wants to get the album art from the internet,
# regardless if the curent song has an embedded album art or not # regardless if the curent song has an embedded album art or not
if [ "$ONLINE_ALBUM_ART" == true ];then if [ "$ONLINE_ALBUM_ART" == true ];then
[ ! "$SILENT" ] && echo "kunst: getting cover from internet" log "getting cover from internet"
get_cover_online get_cover_online
return return
fi fi
@ -148,11 +158,11 @@ find_album_art(){
# Check if the file has a embbeded album art # Check if the file has a embbeded album art
if [ "$STATUS" -eq 0 ];then if [ "$STATUS" -eq 0 ];then
[ ! "$SILENT" ] && echo "kunst: extracted album art" log "extracted album art"
ARTLESS=false ARTLESS=false
else else
DIR="$MUSIC_DIR$(dirname "$(mpc current -f %file%)")" DIR="$MUSIC_DIR$(dirname "$(mpc current -f %file%)")"
[ ! "$SILENT" ] && echo "kunst: inspecting $DIR" log "inspecting $DIR"
# Check if there is an album cover/art in the folder. # Check if there is an album cover/art in the folder.
# Look at issue #9 for more details # Look at issue #9 for more details
@ -161,13 +171,13 @@ find_album_art(){
STATUS=0 STATUS=0
ARTLESS=false ARTLESS=false
convert "$CANDIDATE" $COVER &> /dev/null convert "$CANDIDATE" $COVER &> /dev/null
[ ! "$SILENT" ] && echo "kunst: found cover.png" log "found cover.png"
fi fi
done done
fi fi
if [ "$STATUS" -ne 0 ];then if [ "$STATUS" -ne 0 ];then
[ ! "$SILENT" ] && echo "error: file does not have an album art" log "file does not have an album art"
get_cover_online get_cover_online
fi fi
} }
@ -178,7 +188,7 @@ update_cover() {
if [ "$ARTLESS" == false ]; then if [ "$ARTLESS" == false ]; then
convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null convert "$COVER" -resize "$SIZE" "$COVER" &> /dev/null
[ ! "$SILENT" ] && echo "kunst: resized album art to $SIZE" log "resized album art to $SIZE"
fi fi
} }
@ -198,8 +208,7 @@ main() {
dependencies=(sxiv convert bash ffmpeg mpc jq mpd) dependencies=(sxiv convert bash ffmpeg mpc jq mpd)
for dependency in "${dependencies[@]}"; do for dependency in "${dependencies[@]}"; do
type -p "$dependency" &>/dev/null || { type -p "$dependency" &>/dev/null || {
echo "error: Could not find '${dependency}', is it installed?" >&2 logError "could not find '${dependency}', is it installed?"
exit 1
} }
done done
@ -223,10 +232,7 @@ main() {
echo "$MUSIC_NOTE" | base64 --decode > "$COVER" echo "$MUSIC_NOTE" | base64 --decode > "$COVER"
fi fi
if [ ! "$SILENT" ];then log "swapped album art to $(mpc current)"
echo "kunst: swapped album art to $(mpc current)"
printf '%*s\n' "${COLUMNS:-$(tput cols)}" '' | tr ' ' -
fi
if [ "$FIRST_RUN" == true ]; then if [ "$FIRST_RUN" == true ]; then
FIRST_RUN=false FIRST_RUN=false
@ -244,7 +250,7 @@ main() {
while true; do while true; do
mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break mpc idle player &>/dev/null && (mpc status | grep "\[playing\]" &>/dev/null) && break
done done
[ ! "$SILENT" ] && echo "kunst: received event from mpd" log "received event from mpd"
done done
} }