X-Git-Url: https://iankelling.org/git/?a=blobdiff_plain;f=logq;h=ac49a8fa9fa933e8a7e62d19f375552e1d65109c;hb=19694da937d2e8a8cf0d9eaa8cf6aa5e5abbf1f7;hp=b88e35eed47ddbee97ffee2a7211fadcddfa53dd;hpb=704248ca7f3cb8208aad63453f9bae5c613fdc10;p=log-quiet diff --git a/logq b/logq index b88e35e..ac49a8f 100755 --- a/logq +++ b/logq @@ -3,11 +3,8 @@ logq () { local help="Usage: logq [-h|--help] COMMAND [ARG...] Log Quietly. Run COMMAND with ARGs, log to temp file. Output return code, -command, and log path. - -Minor details: Use logsave if available, which adds informational header/footer -to log file. Logfile is put in random temp dir, with filename made from the -alphanumeric characters of COMMAND + ARGs." +command, and log path. Tail log if there is a failure. +Follows output format of logsave." if [[ $1 == --help || $1 == -h ]]; then echo "$help" @@ -18,7 +15,8 @@ alphanumeric characters of COMMAND + ARGs." $help" return 1 fi - + + # deliniate arguments, so spaces aren't ambiguous local index=0 local x prettycommand for x in "$@"; do @@ -26,29 +24,33 @@ $help" index=$(( index+1 )) done - local file="$*" - file="$(mktemp -d)/${file//[^[:alnum:]]/}" + file="$(mktemp -d)/${file//[[:space:]\/]/_}" + # give us ~20 char filename max + file="${file:0:40}" + + printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file" + + if [[ $- != *x* ]]; then + echo "log $file = $@" + fi - echo "log of $prettycommand" >"$file" - date >>"$file" - echo >>"$file" - # we will propagate any errors - local e=$- - [[ $e == *e* ]] && set +e - "$@" &>> "$file" - local ret=$? - [[ $e == *e* ]] && set -e - - echo >>"$file" - echo "----------------" >>"$file" - date >>"$file" - echo "end of log" >>"$file" - - echo -n "\$?=$ret " - echo -n "$prettycommand" - echo "[log] $file" - return $ret -} + local logq_ret=$( + set +e + trap ERR + "$@" &>> "$file" + echo $? + ) + printf "\n%s\n%s\n" "$(date)" "----------------" >> "$file" + + if [[ $logq_ret != 0 ]]; then + x="tail -n 100 $file" + if [[ $- != *x* ]]; then + echo "logq failure. $x :" + fi + $x + fi + return $logq_ret +} logq "$@"