Skip to content

Commit 8054419

Browse files
authored
Use parent STDOUT for logging output (#172)
1 parent b1ae1ce commit 8054419

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

lib/log.sh

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
# to be included in add-on scripts to reduce code duplication across add-ons.
88
# ==============================================================================
99

10+
# Unless $LOG_FD is already set to a valid fd
11+
if ! [[ "$LOG_FD" =~ ^[0-9]+$ ]] || ! { true >&"$LOG_FD" ; } 2>/dev/null ; then
12+
# Preserve the original STDOUT on a free fd (stored in $LOG_FD) so that we can
13+
# log to it without interfering with the STDOUT of subshells whose output we
14+
# want to capture for other purposes.
15+
exec {LOG_FD}>&1
16+
fi
17+
1018
# ------------------------------------------------------------------------------
1119
# Log a message to output.
1220
#
@@ -15,7 +23,7 @@
1523
# ------------------------------------------------------------------------------
1624
bashio::log() {
1725
local message=$*
18-
echo -e "${message}"
26+
printf '%b\n' "${message}" >&"$LOG_FD"
1927
return "${__BASHIO_EXIT_OK}"
2028
}
2129

@@ -27,7 +35,7 @@ bashio::log() {
2735
# ------------------------------------------------------------------------------
2836
bashio::log.red() {
2937
local message=$*
30-
echo -e "${__BASHIO_COLORS_RED}${message}${__BASHIO_COLORS_RESET}"
38+
printf '%b\n' "${__BASHIO_COLORS_RED}${message}${__BASHIO_COLORS_RESET}" >&"$LOG_FD"
3139
return "${__BASHIO_EXIT_OK}"
3240
}
3341

@@ -39,7 +47,7 @@ bashio::log.red() {
3947
# ------------------------------------------------------------------------------
4048
bashio::log.green() {
4149
local message=$*
42-
echo -e "${__BASHIO_COLORS_GREEN}${message}${__BASHIO_COLORS_RESET}"
50+
printf '%b\n' "${__BASHIO_COLORS_GREEN}${message}${__BASHIO_COLORS_RESET}" >&"$LOG_FD"
4351
return "${__BASHIO_EXIT_OK}"
4452
}
4553

@@ -51,7 +59,7 @@ bashio::log.green() {
5159
# ------------------------------------------------------------------------------
5260
bashio::log.yellow() {
5361
local message=$*
54-
echo -e "${__BASHIO_COLORS_YELLOW}${message}${__BASHIO_COLORS_RESET}"
62+
printf '%b\n' "${__BASHIO_COLORS_YELLOW}${message}${__BASHIO_COLORS_RESET}" >&"$LOG_FD"
5563
return "${__BASHIO_EXIT_OK}"
5664
}
5765

@@ -63,7 +71,7 @@ bashio::log.yellow() {
6371
# ------------------------------------------------------------------------------
6472
bashio::log.blue() {
6573
local message=$*
66-
echo -e "${__BASHIO_COLORS_BLUE}${message}${__BASHIO_COLORS_RESET}"
74+
printf '%b\n' "${__BASHIO_COLORS_BLUE}${message}${__BASHIO_COLORS_RESET}" >&"$LOG_FD"
6775
return "${__BASHIO_EXIT_OK}"
6876
}
6977

@@ -75,7 +83,7 @@ bashio::log.blue() {
7583
# ------------------------------------------------------------------------------
7684
bashio::log.magenta() {
7785
local message=$*
78-
echo -e "${__BASHIO_COLORS_MAGENTA}${message}${__BASHIO_COLORS_RESET}"
86+
printf '%b\n' "${__BASHIO_COLORS_MAGENTA}${message}${__BASHIO_COLORS_RESET}" >&"$LOG_FD"
7987
return "${__BASHIO_EXIT_OK}"
8088
}
8189

@@ -87,7 +95,7 @@ bashio::log.magenta() {
8795
# ------------------------------------------------------------------------------
8896
bashio::log.cyan() {
8997
local message=$*
90-
echo -e "${__BASHIO_COLORS_CYAN}${message}${__BASHIO_COLORS_RESET}"
98+
printf '%b\n' "${__BASHIO_COLORS_CYAN}${message}${__BASHIO_COLORS_RESET}" >&"$LOG_FD"
9199
return "${__BASHIO_EXIT_OK}"
92100
}
93101

@@ -115,7 +123,7 @@ function bashio::log.log() {
115123
output="${output//\{MESSAGE\}/"${message}"}"
116124
output="${output//\{LEVEL\}/"${__BASHIO_LOG_LEVELS[$level]}"}"
117125

118-
echo -e "${output}"
126+
printf '%b\n' "${output}" >&"$LOG_FD"
119127

120128
return "${__BASHIO_EXIT_OK}"
121129
}

0 commit comments

Comments
 (0)