Skip to content

Commit 187715e

Browse files
authored
Merge pull request #24 from mbland/printf-esc-seqs
log: Replace `echo -e` with `printf`
2 parents 6814878 + 4bc05c8 commit 187715e

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

lib/log

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ declare __GO_CRITICAL_SECTION=0
137137
local level_fd=1
138138
local exit_status=0
139139
local close_code='\e[0m'
140-
local echo_mode='-e'
141140

142141
unset 'args[0]'
143142
_@go.log_init
@@ -164,14 +163,13 @@ declare __GO_CRITICAL_SECTION=0
164163
fi
165164

166165
if [[ ! -t "$level_fd" && -z "$_GO_LOG_FORMATTING" ]]; then
167-
echo_mode='-E'
168166
args=("${args[@]//\\e\[[0-9]m}")
169167
args=("${args[@]//\\e\[[0-9][0-9]m}")
170168
args=("${args[@]//\\e\[[0-9][0-9][0-9]m}")
171169
close_code=''
172170
fi
173171

174-
echo "$echo_mode" "$formatted_log_level ${args[*]}$close_code" >&"$level_fd"
172+
printf "$formatted_log_level ${args[*]}$close_code\n" >&"$level_fd"
175173

176174
if [[ "$log_level" == FATAL ]]; then
177175
exit "$exit_status"

tests/log/add-update.bats

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ teardown() {
4949
'@go.log FOOBAR Hello, World!'
5050
assert_success ''
5151

52-
local expected="$(echo -e "${INFO_FORMAT}FOOBAR\e[0m Hello, World!\e[0m")"
52+
local expected="$(printf "${INFO_FORMAT}FOOBAR\e[0m Hello, World!\e[0m\n")"
5353
assert_equal "$expected" "$(< "$TEST_GO_ROOTDIR/logfile")" 'log file output'
5454
}
5555

5656
@test "$SUITE: add new log level defaulting to standard output" {
5757
run_log_script "@go.add_or_update_log_level FOOBAR '$INFO_FORMAT'" \
5858
'_GO_LOG_FORMATTING=true' \
5959
'@go.log FOOBAR Hello, World!'
60-
assert_success "$(echo -e "${INFO_FORMAT}FOOBAR\e[0m Hello, World!\e[0m")"
60+
assert_success "$(printf "${INFO_FORMAT}FOOBAR\e[0m Hello, World!\e[0m\n")"
6161
}
6262

6363
@test "$SUITE: update format of existing log level" {
6464
run_log_script "@go.add_or_update_log_level INFO '$START_FORMAT' keep" \
6565
'_GO_LOG_FORMATTING=true' \
6666
'@go.log INFO Hello, World!'
67-
assert_success "$(echo -e "${START_FORMAT}INFO\e[0m Hello, World!\e[0m")"
67+
assert_success "$(printf "${START_FORMAT}INFO\e[0m Hello, World!\e[0m\n")"
6868
}
6969

7070
@test "$SUITE: update file descriptor of existing log level" {
@@ -74,7 +74,7 @@ teardown() {
7474
'@go.log INFO Hello, World!'
7575
assert_success ''
7676

77-
local expected="$(echo -e "${INFO_FORMAT}INFO\e[0m Hello, World!\e[0m")"
77+
local expected="$(printf "${INFO_FORMAT}INFO\e[0m Hello, World!\e[0m\n")"
7878
assert_equal "$expected" "$(< "$TEST_GO_ROOTDIR/logfile")" 'log file output'
7979
}
8080

@@ -85,6 +85,6 @@ teardown() {
8585
'@go.log INFO Hello, World!'
8686
assert_success ''
8787

88-
local expected="$(echo -e "${START_FORMAT}INFO\e[0m Hello, World!\e[0m")"
88+
local expected="$(printf "${START_FORMAT}INFO\e[0m Hello, World!\e[0m\n")"
8989
assert_equal "$expected" "$(< "$TEST_GO_ROOTDIR/logfile")" 'log file output'
9090
}

tests/log/helpers.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ __expected_log_line() {
3939
stripped_level="${stripped_level//\\e\[[0-9][0-9]m}"
4040
stripped_level="${stripped_level//\\e\[[0-9][0-9][0-9]m}"
4141
level="${level}${padding:0:$((${#padding} - ${#stripped_level}))}"
42-
echo -e "$level $message\e[0m"
42+
printf "$level $message\e[0m\n"
4343
else
4444
level="${level}${padding:0:$((${#padding} - ${#level}))}"
4545
echo "$level $message"

0 commit comments

Comments
 (0)