X-Git-Url: https://iankelling.org/git/?p=log-quiet;a=blobdiff_plain;f=log-once;h=5980b811423413ed01a33222112313b42b73ae94;hp=179dacfd8c670eea5035a2c6255d7a9e60a727c5;hb=ea9d543d812b5ec13d4d3c6b424626f3afaaa5dc;hpb=457661424027a87b9ea600cfeb218574e95470d4 diff --git a/log-once b/log-once index 179dacf..5980b81 100755 --- a/log-once +++ b/log-once @@ -14,13 +14,13 @@ # limitations under the License. append() { - cat >> "$1" + cat >> "$1" } log-once() { - local cbase c log x i out file - cbase=/var/local/cron-errors - [[ $EUID == 0 ]] || cbase=$HOME/cron-errors - local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE] + local cbase c log x i out file + cbase=/var/local/cron-errors + [[ $EUID == 0 ]] || cbase=$HOME/cron-errors + local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE] For cronjobs, email log on repeated failure and success after failure. @@ -34,68 +34,68 @@ or $HOME/cron-errors if not root, and keeps state in the same directory. -ERRORS: ERRORS is the number of errors to accumulate before outputing the error" - errors=3 - while true; do - if [[ $1 == --help ]]; then - echo "$help" - return - elif [[ $1 == -[0-9]* ]]; then - errors=${1#-} - shift - elif [[ $1 == -- ]]; then - shift - break - else - break - fi - done - log_name=$1 - # todo, make option & make them overridable based on command line or env variable - [[ -d $cbase ]] || mkdir -p $cbase - c=$cbase/$log_name - # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script - log=false - if [[ $2 ]]; then - log=true - # read stdin for anything which is not just a newline - elif [[ ! -t 0 ]]; then - while read -r x; do - output+=( $x ) - [[ $x ]] && log=true - done - fi - glob="$c[0-9]*" - file=($glob); [[ $file != "$glob" ]] || file= - if $log; then - out=append - if [[ $file ]]; then - i="${file#$c}" - if (( i < errors )); then - new_file=$c$((i+1)) - mv $file $new_file - file=$new_file - if [[ $file == $c$errors ]]; then - out="tee -a" - fi - fi - else - file=${c}1 - fi - $out $file <<<"log-once: $(date "+%A, %B %d, %r")" - if [[ $2 ]]; then - $out $file <<<"$2" - else - $out $file <<<"${output[@]}" - $out $file - fi - return 0 + errors=3 + while true; do + if [[ $1 == --help ]]; then + echo "$help" + return + elif [[ $1 == -[0-9]* ]]; then + errors=${1#-} + shift + elif [[ $1 == -- ]]; then + shift + break + else + break fi + done + log_name=$1 + # todo, make option & make them overridable based on command line or env variable + [[ -d $cbase ]] || mkdir -p $cbase + c=$cbase/$log_name + # http://stackoverflow.com/questions/2456750/detect-presence-of-stdin-contents-in-shell-script + log=false + if [[ $2 ]]; then + log=true + # read stdin for anything which is not just a newline + elif [[ ! -t 0 ]]; then + while read -r x; do + output+=( $x ) + [[ $x ]] && log=true + done + fi + glob="$c[0-9]*" + file=($glob); [[ $file != "$glob" ]] || file= + if $log; then + out=append if [[ $file ]]; then - rm -f $file + i="${file#$c}" + if (( i < errors )); then + new_file=$c$((i+1)) + mv $file $new_file + file=$new_file if [[ $file == $c$errors ]]; then - echo "log-once success after failure for $c" + out="tee -a" fi + fi + else + file=${c}1 + fi + $out $file <<<"log-once: $(date "+%A, %B %d, %r")" + if [[ $2 ]]; then + $out $file <<<"$2" + else + $out $file <<<"${output[@]}" + $out $file fi return 0 + fi + if [[ $file ]]; then + rm -f $file + if [[ $file == $c$errors ]]; then + echo "log-once success after failure for $c" + fi + fi + return 0 } log-once "$@"