From: Ian Kelling Date: Fri, 17 Jun 2016 08:14:58 +0000 (-0700) Subject: improve output and error handling, use 1 script X-Git-Url: https://iankelling.org/git/?a=commitdiff_plain;h=634a9faf0514608dc9559800292025a573fbe780;p=log-quiet improve output and error handling, use 1 script --- diff --git a/README b/README index b3829e2..4c5077f 100644 --- a/README +++ b/README @@ -1,12 +1,8 @@ -The main documentation is availiable via --help and near the top of any bash -script files which sit next to this file. +The main documentation is availiable via --help and near the top of the bash +script file which sit next to this file. -Files ending in -function are for sourcing then calling as a function. Files -without -function are exactly the same except they are for calling as a script. +The script file can be stripped of the last line and used as a function +instead of a script. -Patches, bugs, and any feedback is very welcome via gitorious or email to +Patches, bugs, and any feedback is very welcome via email to Ian Kelling . - -This program is also part of a collection of programs, -https://gitorious.org/bash-programs-by-ian, which are unrelated except -having the same author and being being bash programs. diff --git a/logq b/logq index bb98743..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" @@ -19,6 +16,7 @@ $help" return 1 fi + # deliniate arguments, so spaces aren't ambiguous local index=0 local x prettycommand for x in "$@"; do @@ -26,24 +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" + printf "%s\n%s\n\n" "Log of $prettycommand" "$(date)" >"$file" + + if [[ $- != *x* ]]; then + echo "log $file = $@" + fi # we will propagate any errors - local e=$- - [[ $e == *e* ]] && set +e - "$@" &>> "$file" - local ret=$? - [[ $e == *e* ]] && set -e - - printf "\n%s\n%s\n%s" "----------------" "$(date)" "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 "$@" diff --git a/logq-function b/logq-function deleted file mode 100644 index afe722f..0000000 --- a/logq-function +++ /dev/null @@ -1,48 +0,0 @@ -#!/bin/bash -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." - - if [[ $1 == --help || $1 == -h ]]; then - echo "$help" - return - fi - if [[ $# == 0 ]]; then - echo "error: need 1 or more arguments -$help" - return 1 - fi - - local index=0 - local x prettycommand - for x in "$@"; do - prettycommand+="[$index]$x " - index=$(( index+1 )) - done - - - local file="$*" - file="$(mktemp -d)/${file//[^[:alnum:]]/}" - - printf "%s\n%s\n\n" "log of $prettycommand" "$(date)" >"$file" - - # we will propagate any errors - local e=$- - [[ $e == *e* ]] && set +e - "$@" &>> "$file" - local ret=$? - [[ $e == *e* ]] && set -e - - printf "\n%s\n%s\n%s" "----------------" "$(date)" "end of log" >>"$file" - - echo -n "\$?=$ret " - echo -n "$prettycommand" - echo "[log] $file" - return $ret -}