add verbose option, license for small program
[log-quiet] / log-once
index 3aa3c766642b531f812d4d8d08486079497d936a..5ada71b2ab5b2ba412d809c01a0a97a70e41223e 100755 (executable)
--- a/log-once
+++ b/log-once
@@ -1,12 +1,27 @@
 #!/bin/bash
 # Copyright (C) 2019 Ian Kelling
-# SPDX-License-Identifier: AGPL-3.0-or-later
+# Log errors once or so instead of many times.
+
+    # This program is free software: you can redistribute it and/or modify
+    # it under the terms of the GNU General Public License as published by
+    # the Free Software Foundation, either version 3 of the License, or
+    # (at your option) any later version.
+
+    # This program is distributed in the hope that it will be useful,
+    # but WITHOUT ANY WARRANTY; without even the implied warranty of
+    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    # GNU General Public License for more details.
+
+    # You should have received a copy of the GNU General Public License
+    # along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# SPDX-License-Identifier: GPL-3.0-or-later
 
 append() {
   cat >> "$1"
 }
 log-once() {
-  local cbase c log line i out file o tmp
+  local cbase c log line i out file o tmp mvout mverr verbose
   cbase=/var/local/cron-errors
   [[ $EUID == 0 ]] || cbase=$HOME/cron-errors
   local help="Usage: some-command-that-outputs-on-error |& log-once [OPTION]... LOG_NAME
@@ -20,6 +35,7 @@ Logs STDIN to /var/local/cron-errors/LOG_NAME\$error_count 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
+-v:       Output the errors along the way to ERRORS
 
 
 You can emulate how cronjobs work by doing this for example:
@@ -30,10 +46,16 @@ I could imagine a similar command that considers its non-option args to
 be an error, but I haven't written it yet.
 "
   errors=3
+  mverr=0
+  verbose=false
+
   while true; do
     if [[ $1 == --help ]]; then
       echo "$help"
       return
+    elif [[ $1 == -v ]]; then
+      verbose=true
+      shift
     elif [[ $1 == -[0-9]* ]]; then
       errors=${1#-}
       shift
@@ -69,9 +91,19 @@ be an error, but I haven't written it yet.
       i="${file#$c}"
       if (( i < errors )); then
         new_file=$c$((i+1))
-        mv $file $new_file
+        mvout=$(mv $file $new_file 2>&1) || mverr=$?
+        if (( mverr )); then
+          if [[ $mvout == *": No such file or directory" ]]; then
+            # We've very likely hit a race condition, where another
+            # log-once did the mv before us.
+            return 0
+          else
+            echo "log-once: error on mv $file $new_file"
+            return $mverr
+          fi
+        fi
         file=$new_file
-        if [[ $file == $c$errors ]]; then
+        if [[ $file == $c$errors ]] || $verbose; then
           out="tee -a"
         fi
       fi