Skip to content

Commit 9a44775

Browse files
author
José Valim
committed
Make prune part of the public API in the formatter
Signed-off-by: José Valim <[email protected]>
1 parent 07c6356 commit 9a44775

File tree

5 files changed

+29
-28
lines changed

5 files changed

+29
-28
lines changed

lib/logger/lib/logger/backends/console.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ defmodule Logger.Backends.Console do
8282
IO.write(device, output)
8383
rescue
8484
ArgumentError ->
85-
IO.write(device, Logger.Utils.prune(output))
85+
IO.write(device, Logger.Formatter.prune(output))
8686
end
8787
end
8888

lib/logger/lib/logger/formatter.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ defmodule Logger.Formatter do
4444
@type pattern :: :date | :level | :levelpad | :message | :metadata | :node | :time
4545
@valid_patterns [:time, :date, :message, :level, :node, :metadata, :levelpad]
4646
@default_pattern "\n$time $metadata[$level] $levelpad$message\n"
47+
@replacement "�"
48+
49+
@doc """
50+
Prune non-valid UTF-8 codepoints.
51+
52+
Typically called after formatting when the data cannot be printed.
53+
"""
54+
@spec prune(IO.chardata) :: IO.chardata
55+
def prune(binary) when is_binary(binary), do: prune_binary(binary, "")
56+
def prune([h|t]) when h in 0..1114111, do: [h|prune(t)]
57+
def prune([h|t]), do: [prune(h)|prune(t)]
58+
def prune([]), do: []
59+
def prune(_), do: @replacement
60+
61+
defp prune_binary(<<h::utf8, t::binary>>, acc),
62+
do: prune_binary(t, <<acc::binary, h::utf8>>)
63+
defp prune_binary(<<_, t::binary>>, acc),
64+
do: prune_binary(t, <<acc::binary, @replacement>>)
65+
defp prune_binary(<<>>, acc),
66+
do: acc
4767

4868
@doc ~S"""
4969
Compiles a format string into an array that the `format/5` can handle.

lib/logger/lib/logger/utils.ex

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
defmodule Logger.Utils do
22
@moduledoc false
33

4-
@replacement "�"
5-
6-
@doc """
7-
Prune non-valid UTF-8 codepoints.
8-
"""
9-
@spec prune(IO.chardata) :: IO.chardata
10-
def prune(binary) when is_binary(binary), do: prune_binary(binary, "")
11-
def prune([h|t]) when h in 0..1114111, do: [h|prune(t)]
12-
def prune([h|t]), do: [prune(h)|prune(t)]
13-
def prune([]), do: []
14-
def prune(_), do: @replacement
15-
16-
defp prune_binary(<<h::utf8, t::binary>>, acc),
17-
do: prune_binary(t, <<acc::binary, h::utf8>>)
18-
defp prune_binary(<<_, t::binary>>, acc),
19-
do: prune_binary(t, <<acc::binary, @replacement>>)
20-
defp prune_binary(<<>>, acc),
21-
do: acc
22-
234
@doc """
245
Truncates a char data into n bytes.
256

lib/logger/test/logger/formatter_test.exs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ defmodule Logger.FormatterTest do
44

55
import Logger.Formatter
66

7+
test "prune/1" do
8+
assert prune(1) == "�"
9+
assert prune(<<"hí", 233>>) == "hí�"
10+
assert prune(["hi"|233]) == ["hi"|"�"]
11+
assert prune([233|"hi"]) == [233|"hi"]
12+
assert prune([[]|[]]) == [[]]
13+
end
14+
715
defmodule CompileMod do
816
def format(_level, _msg, _ts, _md) do
917
true

lib/logger/test/logger/utils_test.exs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,6 @@ defmodule Logger.UtilsTest do
66
import Kernel, except: [inspect: 2]
77
defp inspect(format, args), do: Logger.Utils.inspect(format, args, 10)
88

9-
test "prune/1" do
10-
assert prune(1) == "�"
11-
assert prune(<<"hí", 233>>) == "hí�"
12-
assert prune(["hi"|233]) == ["hi"|"�"]
13-
assert prune([233|"hi"]) == [233|"hi"]
14-
assert prune([[]|[]]) == [[]]
15-
end
16-
179
test "truncate/2" do
1810
# ASCII binaries
1911
assert truncate("foo", 4) == "foo"

0 commit comments

Comments
 (0)