Skip to content

Allow os::log functions to print multi-line messages #12701

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

Merged
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
31 changes: 25 additions & 6 deletions hack/lib/log/output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# Arguments:
# - all: message to write
function os::log::info() {
echo "[INFO] $*"
os::log::internal::prefix_lines "[INFO]" "$*"
}
readonly -f os::log::info

Expand All @@ -19,7 +19,7 @@ readonly -f os::log::info
# Arguments:
# - all: message to write
function os::log::warn() {
os::text::print_yellow "[WARNING] $*" 1>&2
os::text::print_yellow "$( os::log::internal::prefix_lines "[WARNING]" "$*" )" 1>&2
}
readonly -f os::log::warn

Expand All @@ -30,7 +30,7 @@ readonly -f os::log::warn
# Arguments:
# - all: message to write
function os::log::error() {
os::text::print_red "[ERROR] $*" 1>&2
os::text::print_red "$( os::log::internal::prefix_lines "[ERROR]" "$*" )" 1>&2
}
readonly -f os::log::error

Expand All @@ -42,7 +42,7 @@ readonly -f os::log::error
# Arguments:
# - all: message to write
function os::log::fatal() {
os::text::print_red "[FATAL] $*" 1>&2
os::text::print_red "$( os::log::internal::prefix_lines "[FATAL]" "$*" )" 1>&2
return 1
}
readonly -f os::log::fatal
Expand All @@ -54,7 +54,26 @@ readonly -f os::log::fatal
# - all: message to write
function os::log::debug() {
if [[ -n "${OS_DEBUG:-}" ]]; then
os::text::print_blue "[DEBUG] $*" 1>&2
os::text::print_blue "$( os::log::internal::prefix_lines "[DEBUG]" "$*" )" 1>&2
fi
}
readonly -f os::log::debug
readonly -f os::log::debug

# os::log::internal::prefix_lines prints out the
# original content with the given prefix at the
# start of every line.
#
# Arguments:
# - 1: prefix for lines
# - 2: content to prefix
function os::log::internal::prefix_lines() {
local prefix="$1"
local content="$2"

local old_ifs="${IFS}"
IFS=$'\n'
for line in ${content}; do
echo "${prefix} ${line}"
done
IFS="${old_ifs}"
}