minor fixes
[log-quiet] / log-once
index 5980b811423413ed01a33222112313b42b73ae94..4a2b3480e5c8dbc20771feb9df20bef5646ce008 100755 (executable)
--- a/log-once
+++ b/log-once
@@ -1,26 +1,15 @@
 #!/bin/bash
-# Copyright (C) 2016 Ian Kelling
-
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-
-#     http://www.apache.org/licenses/LICENSE-2.0
-
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
+# Copyright (C) 2019 Ian Kelling
+# SPDX-License-Identifier: AGPL-3.0-or-later
 
 append() {
   cat >> "$1"
 }
 log-once() {
-  local cbase c log x i out file
+  local cbase c log x i out file o tmp
   cbase=/var/local/cron-errors
   [[ $EUID == 0 ]] || cbase=$HOME/cron-errors
-  local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE]
+  local help="Usage: log-once [OPTION]... LOG_NAME [LOG_MESSAGE...]
 
 For cronjobs, email log on repeated failure and success after failure.
 
@@ -60,12 +49,16 @@ state in the same directory.
     # read stdin for anything which is not just a newline
   elif [[ ! -t 0 ]]; then
     while read -r x; do
-      output+=( $x )
+      output+=( "$x" )
       [[ $x ]] && log=true
     done
   fi
   glob="$c[0-9]*"
-  file=($glob); [[ $file != "$glob" ]] || file=
+  # file is error file indicating previous error
+  tmp=($glob); file="${tmp[0]}"
+  if [[ $file == "$glob" ]]; then
+    file=
+  fi
   if $log; then
     out=append
     if [[ $file ]]; then
@@ -80,12 +73,17 @@ state in the same directory.
       fi
     else
       file=${c}1
+      if (( errors == 1 )); then
+        out="tee -a"
+      fi
     fi
     $out $file <<<"log-once: $(date "+%A, %B %d, %r")"
     if [[ $2 ]]; then
-      $out $file <<<"$2"
+      $out $file <<<"$*"
     else
-      $out $file <<<"${output[@]}"
+      for o in "${output[@]}"; do
+        $out $file <<<"$o"
+      done
       $out $file
     fi
     return 0