Skip to content

Commit 3d72978

Browse files
blambiJosé Valim
authored andcommitted
Avoid calling truncate_n/2 if truncate is :infinity (#7701)
Signed-off-by: José Valim <[email protected]>
1 parent 51d61cb commit 3d72978

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

lib/logger/lib/logger/utils.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ defmodule Logger.Utils do
139139
# arguments according to the truncate limit.
140140
{args, _} =
141141
Enum.map_reduce(args, truncate, fn arg, acc ->
142-
if is_binary(arg) do
142+
if is_binary(arg) and acc != :infinity do
143143
truncate_n(arg, acc)
144144
else
145145
{arg, acc}

lib/logger/test/logger/utils_test.exs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ defmodule Logger.UtilsTest do
55

66
import Kernel, except: [inspect: 2]
77

8-
defp inspect(format, args) do
8+
defp inspect(format, args, truncate \\ 10) do
99
format
10-
|> Logger.Utils.scan_inspect(args, 10)
10+
|> Logger.Utils.scan_inspect(args, truncate)
1111
|> :io_lib.unscan_format()
1212
end
1313

@@ -81,52 +81,57 @@ defmodule Logger.UtilsTest do
8181
assert truncate(long_string, :infinity) == long_string
8282
end
8383

84-
test "inspect/2 formats" do
84+
test "scan_inspect/3 formats" do
8585
assert inspect('~p', [1]) == {'~ts', [["1"]]}
8686
assert inspect("~p", [1]) == {'~ts', [["1"]]}
8787
assert inspect(:"~p", [1]) == {'~ts', [["1"]]}
8888
end
8989

90-
test "inspect/2 sigils" do
90+
test "scan_inspect/3 sigils" do
9191
assert inspect('~10.10tp', [1]) == {'~ts', [["1"]]}
9292
assert inspect('~-10.10tp', [1]) == {'~ts', [["1"]]}
9393

9494
assert inspect('~10.10lp', [1]) == {'~ts', [["1"]]}
9595
assert inspect('~10.10x~p~n', [1, 2, 3]) == {'~10.10x~ts~n', [1, 2, ["3"]]}
9696
end
9797

98-
test "inspect/2 with modifier t has no effect (as it is the default)" do
98+
test "scan_inspect/3 with modifier t has no effect (as it is the default)" do
9999
assert inspect('~tp', [1]) == {'~ts', [["1"]]}
100100
assert inspect('~tw', [1]) == {'~ts', [["1"]]}
101101
end
102102

103-
test "inspect/2 with modifier l always prints lists" do
103+
test "scan_inspect/3 with modifier l always prints lists" do
104104
assert inspect('~lp', ['abc']) == {'~ts', [["[", "97", ",", " ", "98", ",", " ", "99", "]"]]}
105105
assert inspect('~lw', ['abc']) == {'~ts', [["[", "97", ",", " ", "98", ",", " ", "99", "]"]]}
106106
end
107107

108-
test "inspect/2 with modifier for width" do
108+
test "scan_inspect/3 with modifier for width" do
109109
assert inspect('~5lp', ['abc']) ==
110110
{'~ts', [["[", "97", ",", "\n ", "98", ",", "\n ", "99", "]"]]}
111111

112112
assert inspect('~5lw', ['abc']) == {'~ts', [["[", "97", ",", " ", "98", ",", " ", "99", "]"]]}
113113
end
114114

115-
test "inspect/2 with modifier for limit" do
115+
test "scan_inspect/3 with modifier for limit" do
116116
assert inspect('~5lP', ['abc', 2]) ==
117117
{'~ts', [["[", "97", ",", "\n ", "98", ",", "\n ", "...", "]"]]}
118118

119119
assert inspect('~5lW', ['abc', 2]) ==
120120
{'~ts', [["[", "97", ",", " ", "98", ",", " ", "...", "]"]]}
121121
end
122122

123-
test "inspect/2 truncates binaries" do
123+
test "scan_inspect/3 truncates binaries" do
124124
assert inspect('~ts', ["abcdeabcdeabcdeabcde"]) == {'~ts', ["abcdeabcde"]}
125125

126126
assert inspect('~ts~ts~ts', ["abcdeabcde", "abcde", "abcde"]) ==
127127
{'~ts~ts~ts', ["abcdeabcde", "", ""]}
128128
end
129129

130+
test "scan_inspect/3 with :infinity truncate" do
131+
long_string = String.duplicate("foo", 10000)
132+
assert inspect('~ts', [long_string], :infinity) == {'~ts', [long_string]}
133+
end
134+
130135
test "timestamp/1" do
131136
assert {{_, _, _}, {_, _, _, _}} = timestamp(true)
132137
end

0 commit comments

Comments
 (0)