]> git.corax.cc Git - toolbox/commitdiff
include/log: Log messages to the logfile AND stderr
authorMatthias Kruk <m@m10k.eu>
Tue, 23 Mar 2021 23:56:27 +0000 (08:56 +0900)
committerMatthias Kruk <m@m10k.eu>
Tue, 23 Mar 2021 23:56:27 +0000 (08:56 +0900)
The log module only writes messages to the log file, which is not
enough for scripts that are running in the foreground. This commit
changes the log_write() function to log messages to the logfile as
well as to stderr.

include/log.sh

index d4e3760d20b736e6d04eedb52f6c480567b4c34e..41d2c1c4771ce901811090e2a15514ad8eb0b871 100644 (file)
@@ -64,10 +64,18 @@ log_write() {
 
        if (( $# > 2 )); then
                for line in "${@:3}"; do
-                       if ! date +"%F %T %z $prefix $line" >> "$__log_file"; then
-                               echo "Could not write to $__log_file" 1>&2
+                       local msg
+
+                       if ! msg=$(date +"%F %T %z $prefix $line"); then
+                               echo "Could not get timestamp" 1>&2
                                return 1
                        fi
+
+                       if ! echo "$msg" >> "$__log_file"; then
+                               echo "Could not write to $__log_file" 1>&2
+                       fi
+
+                       echo "$msg" 1>&2
                done
        else
                while read -r line; do