Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: polyfill logger in edge cases for #541 #647

Merged
merged 1 commit into from
Jan 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is msg in this context?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$msg is a message that's read off of stdin piped into the "command" (the function at this point in the code). This is implemented here as you're able to pipe messages into (the real) logger and have it print out your messages with the same format for each read line:

logger -s -t INFO <<EOF
message 1 - hello, world!
message 2 - hi, there!
EOF
# <13>Jan 14 15:11:18 INFO: message 1 - hello, world!
# <13>Jan 14 15:11:18 INFO: message 2 - hi, there!

printf "$format" "${msg}" >&2
done

return 0
}