Skip to content

Commit

Permalink
feat(LOGGING): options to manage newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
niall-byrne committed Jul 24, 2024
1 parent 53e6074 commit 4ec3db9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
Binary file modified cicd-tools/boxes/0.1.0.tar.gz
Binary file not shown.
4 changes: 4 additions & 0 deletions cicd-tools/boxes/0.1.0/libraries/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ environment() {
}

_environment_args() {
local OPTARG
local OPTIND
local OPTION

while getopts "m:o:d:" OPTION; do
case "$OPTION" in
m)
Expand Down
60 changes: 50 additions & 10 deletions cicd-tools/boxes/0.1.0/libraries/logging.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ source "$(dirname -- "${BASH_SOURCE[0]}")/colours.sh"
LOGGING_LEVEL=${LOGGING_LEVEL-"DEBUG"}

function log() {

# USAGE:
# colour bg [CRITICAL|ERROR|WARNING|INFO|DEBUG] [MESSAGE CONTENTS]
# log (-b) (-n) [CRITICAL|ERROR|WARNING|INFO|DEBUG] [MESSAGE CONTENTS]

local LOGGING_MESSAGE_CONTENT
local LOGGING_MESSAGE_LEVEL
local LOGGING_NEW_LINE="\n"
local LOGGING_SEVERITY_LEVELS=("DEBUG" "INFO" "WARNING" "ERROR" "CRITICAL")

# shellcheck disable=SC2034
Expand All @@ -28,21 +32,50 @@ function log() {
local DEBUG="CYAN"
# shellcheck disable=SC2034

local LOGGING_MESSAGE_LEVEL="${1}"
local MESSAGE_CONTENT="${2}"

if [[ -z "${!LOGGING_LEVEL}" ]] ||
[[ -z "${MESSAGE_CONTENT}" ]]; then
log "ERROR" "Invalid logging statement!"
return 127
fi
_log_args "$@"

if [[ "$(_log_get_severity_level "${LOGGING_MESSAGE_LEVEL}")" -ge "$(_log_get_severity_level "${LOGGING_LEVEL}")" ]]; then
echo "[$(date -u)] [$(colour fg "${!LOGGING_MESSAGE_LEVEL}")${LOGGING_MESSAGE_LEVEL}$(colour clear)] ${MESSAGE_CONTENT}" >> /dev/stderr
echo \
-e \
-n \
"[$(date -u)] [$(colour fg "${!LOGGING_MESSAGE_LEVEL}")${LOGGING_MESSAGE_LEVEL}$(colour clear)] ${LOGGING_MESSAGE_CONTENT}${LOGGING_NEW_LINE}" \
>> /dev/stderr
fi

}

function _log_args() {
local OPTIND
local OPTION

while getopts "bn" OPTION; do
case "$OPTION" in
b)
LOGGING_NEW_LINE="\n\n"
;;
n)
LOGGING_NEW_LINE=""
;;
\?)
_logging_usage
;;
*)
continue
;;
esac
done
shift $((OPTIND - 1))

LOGGING_MESSAGE_LEVEL="${1}"
LOGGING_MESSAGE_CONTENT="${2}"

if [[ -z "${!LOGGING_LEVEL}" ]] ||
[[ -z "${LOGGING_MESSAGE_CONTENT}" ]]; then
echo "ERROR" "Invalid logging statement!"
return 127
fi
}

function _log_get_severity_level() {
#1: The severity type as a string.
local LOGGIN_SEVERITY_LEVEL
Expand All @@ -52,3 +85,10 @@ function _log_get_severity_level() {
fi
done
}

function _logging_usage() {
echo "ERROR" "USAGE: log (-b) (-n) [CRITICAL|ERROR|WARNING|INFO|DEBUG] [MESSAGE CONTENTS]"
echo "ERROR" " -b Optionally add an additional newline after logging message."
echo "ERROR" " -n Optionally disable adding a newline after logging message."
return 127
}

0 comments on commit 4ec3db9

Please sign in to comment.