From 78cb5e416ef73333a2726107aded9f5a47e0db9e Mon Sep 17 00:00:00 2001 From: Hary Zeldon Date: Fri, 3 Oct 2025 12:08:22 +0200 Subject: [PATCH 1/8] make test executable from any work dir Signed-off-by: Hary Zeldon --- tests/sloggi.sh | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) mode change 100644 => 100755 tests/sloggi.sh diff --git a/tests/sloggi.sh b/tests/sloggi.sh old mode 100644 new mode 100755 index 796451c..9fe38b2 --- a/tests/sloggi.sh +++ b/tests/sloggi.sh @@ -10,8 +10,11 @@ # XXX Output is not quite TAP, but should be. We could make a TAP mode for slog, or strip the # non-tap output in the log. -LOG_PATH="./my.log" -. ../slog.sh +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +readonly SCRIPT_DIR + +LOG_PATH="$SCRIPT_DIR/my.log" +. "$SCRIPT_DIR/../slog.sh" log "ok 1 - This is regular log message... " From 7675cf9d8780a54689a03bbc2a033d5bb45f3dc7 Mon Sep 17 00:00:00 2001 From: Hary Zeldon Date: Fri, 3 Oct 2025 12:08:54 +0200 Subject: [PATCH 2/8] make test fail because we really want to set e. Signed-off-by: Hary Zeldon --- tests/sloggi.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/sloggi.sh b/tests/sloggi.sh index 9fe38b2..37ba10f 100755 --- a/tests/sloggi.sh +++ b/tests/sloggi.sh @@ -10,6 +10,8 @@ # XXX Output is not quite TAP, but should be. We could make a TAP mode for slog, or strip the # non-tap output in the log. +set -euo pipefail + SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" readonly SCRIPT_DIR From aa932b82a1ac05a6d3f729ec8e304ac109f48d51 Mon Sep 17 00:00:00 2001 From: Hary Zeldon Date: Fri, 3 Oct 2025 12:10:14 +0200 Subject: [PATCH 3/8] make use of vars Signed-off-by: Hary Zeldon --- tests/sloggi.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/sloggi.sh b/tests/sloggi.sh index 37ba10f..1035773 100755 --- a/tests/sloggi.sh +++ b/tests/sloggi.sh @@ -32,7 +32,7 @@ log_error "ok 5 - Whoops! I made a booboo" LOG_LEVEL_LOG="ERROR" log_info "ok 6 - This should only appear on the console and not in the log file" -if [ -e ./my.log ]; then +if [ -e "$LOG_PATH" ]; then # Did the log get created with actual data? grep -q Whoops "$LOG_PATH" if [ $? -eq 0 ]; then @@ -58,3 +58,4 @@ log_debug "Uh oh. This should never show up in the log or the console." # Invalid log level...should default to info LOG_LEVEL_STDOUT="DOOTDOOT" log_info "ok 9 - This should show up, even though LOG_LEVEL_STDOUT set to $LOG_LEVEL_STDOUT." + From aa1e21812619e9c70caf6323981e99caa116d56d Mon Sep 17 00:00:00 2001 From: Hary Zeldon Date: Fri, 3 Oct 2025 12:11:01 +0200 Subject: [PATCH 4/8] fix issue with unset var Signed-off-by: Hary Zeldon --- slog.sh | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/slog.sh b/slog.sh index 4bbf267..bf81b13 100644 --- a/slog.sh +++ b/slog.sh @@ -62,8 +62,9 @@ prepare_log_for_nonterminal() { log() { local log_text="$1" - local log_level="$2" - local log_color="$3" + # Default level to "info" + local log_level="${2:-INFO}" + local log_color="${3:-${LOG_INFO_COLOR}}" # Levels for comparing against LOG_LEVEL_STDOUT and LOG_LEVEL_LOG local LOG_LEVEL_DEBUG=0 @@ -72,10 +73,6 @@ log() { local LOG_LEVEL_WARNING=3 local LOG_LEVEL_ERROR=4 - # Default level to "info" - [ -z ${log_level} ] && log_level="INFO"; - [ -z ${log_color} ] && log_color="${LOG_INFO_COLOR}"; - # Validate LOG_LEVEL_STDOUT and LOG_LEVEL_LOG since they'll be eval-ed. case $LOG_LEVEL_STDOUT in DEBUG|INFO|SUCCESS|WARNING|ERROR) From 4f0ec9be4ebad88c3fd440582bbbd205d2bc1a60 Mon Sep 17 00:00:00 2001 From: Hary Zeldon Date: Mon, 6 Oct 2025 12:12:28 +0200 Subject: [PATCH 5/8] remove 'set -e' from slog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit setting 'set -e' in a script that is sourced by other scripts can cause unexpected exits in the caller’s environment. dependencies should not dictate error-handling behavior for scripts that source them. this change ensures that the calling scripts remain in control of whether they exit on errors, while still allowing internal logging commands to handle errors safely if needed. Signed-off-by: Hary Zeldon --- slog.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/slog.sh b/slog.sh index bf81b13..c65035d 100644 --- a/slog.sh +++ b/slog.sh @@ -6,7 +6,6 @@ # Licensed under the MIT license # http://github.com/swelljoe/slog #-------------------------------------------------------------------------------------------------- -set -e # Fail on first error # LOG_PATH - Define $LOG_PATH in your script to log to a file, otherwise # just writes to STDOUT. From 6e5d189f1df0c53fc74e5b506129cc5fcd35e11c Mon Sep 17 00:00:00 2001 From: Hary Zeldon Date: Mon, 6 Oct 2025 12:30:43 +0200 Subject: [PATCH 6/8] fix terminal detection logic for interactive output the previous implementation used '$(tty -s)' inside a test expression, which always evaluated to false because 'tty -s' produces no output. it also spawned an unnecessary subprocess. the logic now uses '[ -t 1 ] && [ -n "$TERM" ]' to check whether stdout is a terminal and a valid terminal type is defined. this approach is faster, simpler, and fully POSIX-compliant. Signed-off-by: Hary Zeldon --- slog.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/slog.sh b/slog.sh index c65035d..2117d7c 100644 --- a/slog.sh +++ b/slog.sh @@ -25,10 +25,10 @@ SCRIPT_NAME="${SCRIPT_NAME#\./}" SCRIPT_NAME="${SCRIPT_NAME##/*/}" # Determines if we print colors or not -if [ $(tty -s) ]; then - readonly INTERACTIVE_MODE="off" -else +if [ -t 1 ] && [ -n "$TERM" ]; then readonly INTERACTIVE_MODE="on" +else + readonly INTERACTIVE_MODE="off" fi #-------------------------------------------------------------------------------------------------- From ccb63e4dafe1ba9dbe11f550373c6fd827b7893e Mon Sep 17 00:00:00 2001 From: Hary Zeldon Date: Mon, 6 Oct 2025 12:35:02 +0200 Subject: [PATCH 7/8] remove non-POSIX-compliant `local` Signed-off-by: Hary Zeldon --- slog.sh | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/slog.sh b/slog.sh index 2117d7c..71f9ced 100644 --- a/slog.sh +++ b/slog.sh @@ -60,17 +60,17 @@ prepare_log_for_nonterminal() { } log() { - local log_text="$1" + log_text="$1" # Default level to "info" - local log_level="${2:-INFO}" - local log_color="${3:-${LOG_INFO_COLOR}}" + log_level="${2:-INFO}" + log_color="${3:-${LOG_INFO_COLOR}}" # Levels for comparing against LOG_LEVEL_STDOUT and LOG_LEVEL_LOG - local LOG_LEVEL_DEBUG=0 - local LOG_LEVEL_INFO=1 - local LOG_LEVEL_SUCCESS=2 - local LOG_LEVEL_WARNING=3 - local LOG_LEVEL_ERROR=4 + LOG_LEVEL_DEBUG=0 + LOG_LEVEL_INFO=1 + LOG_LEVEL_SUCCESS=2 + LOG_LEVEL_WARNING=3 + LOG_LEVEL_ERROR=4 # Validate LOG_LEVEL_STDOUT and LOG_LEVEL_LOG since they'll be eval-ed. case $LOG_LEVEL_STDOUT in From 3a20eb9925dcc1971be2f6bbd1dbe0d8d1ae8242 Mon Sep 17 00:00:00 2001 From: Hary Zeldon Date: Mon, 27 Oct 2025 15:24:56 +0100 Subject: [PATCH 8/8] fix another issue with unset var Signed-off-by: Hary Zeldon --- slog.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/slog.sh b/slog.sh index 71f9ced..d06a339 100644 --- a/slog.sh +++ b/slog.sh @@ -9,6 +9,7 @@ # LOG_PATH - Define $LOG_PATH in your script to log to a file, otherwise # just writes to STDOUT. +LOG_PATH=${LOG_PATH:-""} # LOG_LEVEL_STDOUT - Define to determine above which level goes to STDOUT. # By default, all log levels will be written to STDOUT.