Skip to content

Commit

Permalink
Merge pull request #647 from amazonlinux/ci-container-logger
Browse files Browse the repository at this point in the history
ci: polyfill logger in edge cases for #541
  • Loading branch information
jahkeup authored Jan 14, 2020
2 parents b47dad9 + 9ec444b commit 9245014
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion tools/infra/container/runtime/lib/lib.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,50 @@

# Logger provides the corrected interface to log to stderr.
logger() {
command logger --no-act -s "$@"
# Use logger if its usable
if test -S /dev/log; then
command logger --no-act -s "$@"
return 0
fi

# Otherwise, use a simple polyfill implementation that provides a similar
# enough interface to be used across scripts.
local tag
local message
local format

# polyfill in a logger that writes to stderr
while [ "$#" -ne 0 ]; do
case "$1" in
-t )
tag="$2"
shift 1
;;
-*|-- )
# drop options
;;
* )
# message
message=( "$@" )
break
;;
esac
shift 1
done

# Message printer format
format="${tag:+"$tag: "}%s\n"

# Single message in function call
if [[ "${#message[@]}" -ne 0 ]]; then
printf "$format" "${message[*]}" >&2
return 0
fi

# Stream of messages sent to function as input
while read msg; do
printf "$format" "${msg}" >&2
done

return 0
}

0 comments on commit 9245014

Please sign in to comment.