swupdate-ab: improve log functions
authorHugo Villeneuve <hvilleneuve@dimonoff.com>
Thu, 18 Jul 2024 15:13:00 +0000 (11:13 -0400)
committerHugo Villeneuve <hugo@hugovil.com>
Tue, 24 Sep 2024 19:46:55 +0000 (15:46 -0400)
recipes-support/swupdate/swupdate-ab/update.footer.sh
recipes-support/swupdate/swupdate-ab/update.header.sh

index 6c8e394..53db14e 100644 (file)
@@ -21,15 +21,13 @@ get_destination_partition_device()
     disk="/dev/$(lsblk -ndo pkname $(findmnt -n -o SOURCE /))"
 
     if [ ! -b "${disk}" ]; then
-        swulog "Error: disk \"${disk}\" not found"
-        return 1
+        log_err "Error: disk \"${disk}\" not found"
     fi
 
     update_dev="${disk}${next_part}"
 
     if [ ! -b "${update_dev}" ]; then
-        swulog "Error: destination partition \"${update_dev}\" not found"
-        return 1
+        log_err "Error: destination partition \"${update_dev}\" not found"
     fi
 
     echo "${update_dev}"
@@ -43,8 +41,7 @@ mount_destination_partition()
     # Create temporary mount point:
     mnt_point=$(mktemp -q -d /tmp/swupdate-mount.XXXXXX)
     if [ ${?} -ne 0 ]; then
-        swulog "Error: cannot create temporary file"
-        exit 1
+        log_err "Error: cannot create temporary file"
     fi
 
     # Mount update partition:
@@ -65,7 +62,7 @@ unmount_destination_partition()
 do_preinst()
 {
     update_dev=$(get_destination_partition_device)
-    swulog "Reformat destination partition: ${update_dev}"
+    log_info "Reformat destination partition: ${update_dev}"
 
     label="$(blkid -s LABEL -o value ${update_dev})"
 
@@ -80,12 +77,12 @@ do_postinst()
     mount_destination_partition
 
     # Perform migration of selected files/folders...
-    swulog "migrating existing data..."
+    log_info "migrating existing data..."
 
     # Migrate files:
     for f in ${SWU_PRESERVE_FILES}; do
         if [ ! -f ${f} ]; then
-            swulog "warning: missing source file: ${f} (skipping)"
+            log_warn "warning: missing source file: ${f} (skipping)"
             continue
         fi
 
@@ -95,7 +92,7 @@ do_postinst()
         # The destination folder need to be created in your new SWU archive
         # with the proper ownership and permissions (can be empty)
         if [ ! -d ${dst_folder} ]; then
-            swulog "warning: missing destination folder for file: ${f} (skipping)"
+            log_warn "warning: missing destination folder for file: ${f} (skipping)"
             continue
         fi
 
@@ -106,7 +103,7 @@ do_postinst()
     # Migrate folders:
     for d in ${SWU_PRESERVE_FOLDERS}; do
         if [ ! -d ${d} ]; then
-            swulog "warning: missing source folder: ${d} (skipping)"
+            log_warn "warning: missing source folder: ${d} (skipping)"
             continue
         fi
 
@@ -134,7 +131,6 @@ case "$1" in
         do_postinst
         ;;
     *)
-        swulog "unsupported install mode: \"${1}\""
-        exit 1
+        log_err "unsupported install mode: \"${1}\""
         ;;
 esac
index 50af79c..53df653 100644 (file)
@@ -8,6 +8,20 @@ set -e
 
 trap 'catch $?' EXIT
 
+# Arg1: log message/string
+log_info() {
+    printf "$(basename ${0}): ${*}\n"
+}
+
+log_warn() {
+    printf >&2 "$(basename ${0}): ${*}\n"
+}
+
+log_err() {
+    printf >&2 "$(basename ${0}): ${*}\n"
+    exit 1
+}
+
 catch()
 {
     if [ "$1" != "0" ]; then
@@ -20,15 +34,9 @@ catch()
     fi
 }
 
-# Arg1: log message/string
-swulog()
-{
-    echo "$(basename ${0}): ${1}"
-}
-
 FSTYPE="ext4"
 
-swulog "arguments = \"${*}\""
+log_info "arguments = \"${*}\""
 
 if [ $# -lt 2 ]; then
     exit 1;