From: Gustav Eek Date: Thu, 4 Apr 2024 06:54:15 +0000 (+0200) Subject: [unoconv-display] Collect logging and output in dedicated functions X-Git-Url: https://git.g-eek.se/?a=commitdiff_plain;h=78d848bc1cbf7fbbf222f6149124ab5ec60162f5;p=users%2Fgustav%2Fscripts.git [unoconv-display] Collect logging and output in dedicated functions --- diff --git a/unoconv-display b/unoconv-display index 46cd12d..09559da 100644 --- a/unoconv-display +++ b/unoconv-display @@ -16,9 +16,27 @@ DEBUG=1 cache="$HOME/.cache/unoconv-display" tmp="$XDG_RUNTIME_DIR/${0##*/}" mkdir -p "$cache" "$tmp" -test -n "$DEBUG" &&\ - echo "[$(date -Is)] ${0##*/} D: Start." >> $tmp/log if [ "$1" == "-e" ]; then fmt=emacs; shift; else fmt=pdf; fi + +debug () { + test -n "$DEBUG" &&\ + echo "[$(date -Is)] ${0##*/} D: $1" >> $tmp/log +} + +info () { + echo "[$(date -Is)] ${0##*/} I: $1" >> $tmp/log +} + +error () { + debug "Error: $1" + echo -n "${0##*/}: Error: $1" 1>&2 + test -n "$DEBUG" && echo " > Refer to debug log output '$tmp/log'" 1>&2 + test -z "$DEBUG" && echo " > Rerun with DEBUG environment variable" 1>&2 + exit 1 +} + +debug "Start" + if [ -n "$1" ]; then file=$1 else file="$tmp/$$"; cat > "$file"; fi sha=$(sha1sum "$file") @@ -27,13 +45,11 @@ dir="$cache/${sha:0:16}" pdf="$dir/$base.pdf" txt="$dir/$base.txt" -test -n "$DEBUG" &&\ - echo "[$(date -Is)] ${0##*/} D: Continue for '$fmt', '$tmp', '$file', '$pdf', '$txt'." >> $tmp/log +debug "Continue for '$fmt', '$tmp', '$file', '$pdf', '$txt'." if [ -d "$dir" ]; then # Start text viewer rm -f "$tmp/$$" - test -n "$DEBUG" &&\ - echo "[$(date -Is)] ${0##*/} D: Found '$dir'. Start '$fmt' viewer." >> $tmp/log + debug "Found '$dir'. Start '$fmt' viewer." case $fmt in emacs) exec emacsclient -e "(view-file \"$txt\")" > /dev/null @@ -52,10 +68,10 @@ if [ -d "$dir" ]; then # Start text viewer esac fi -test -n "$DEBUG" &&\ - echo "[$(date -Is)] ${0##*/} D: Convert '$file'." >> $tmp/log +# Convert to PDF and text, since not exited already + +debug "Convert '$file'." -# Convert to PDF mkdir "$dir" unoconv -f pdf -o "$pdf" "$file" || \ unoconv -l && \ @@ -63,8 +79,8 @@ unoconv -f pdf -o "$pdf" "$file" || \ unoconv -f txt -o "$txt" "$file" # Recursively open -test -n "$DEBUG" &&\ - echo "[$(date -Is)] [$(date -Is)] ${0##*/} D: Recursively for '$file'." >> $tmp/log + +debug "Recursively wrap for '$file'." if [ $fmt == "emacs" ]; then args=-e; fi bash ${0} ${args} "$file" exit $?