stop returning 1. use pipefail if needed
[log-quiet] / logq-function
index c2b99f21445e5e45d1079c29cf0df0dff61b1f55..d07cba078045114da6555a5d7c7e96146d00e9e5 100644 (file)
@@ -1,13 +1,23 @@
 #!/bin/bash
+# Copyright (C) 2014-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.
 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 +28,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,28 +37,32 @@ $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
+}