Skip to content

Commit

Permalink
Steamroll
Browse files Browse the repository at this point in the history
  • Loading branch information
reachfh committed Jul 21, 2024
1 parent a5611f1 commit c5be13a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
24 changes: 9 additions & 15 deletions src/logger_formatter_json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,15 @@ to_string(X, Config) when is_binary(X) ->

to_string(X, Config) -> io_lib:format(p(Config), [X]).


-spec process_io_list(List, Config) -> binary() when List :: list(), Config :: config().
% process_io_list(List, Config) when is_list(List) ->
% Strings = lists:map(fun (X) -> iolist_to_string(X, Config) end, List),
% iolist_to_binary(Strings).

process_io_list([], _Config) -> [];
process_io_list(List, Config) ->
process_io_list(List, [], Config).
process_io_list(List, Config) -> process_io_list(List, [], Config).

process_io_list([], Acc, _Config) ->
iolist_to_binary(lists:reverse(Acc));
process_io_list([], Acc, _Config) -> iolist_to_binary(lists:reverse(Acc));

process_io_list([H | T], Acc, Config) when is_list(H) ->
process_io_list(T, [process_io_list(H, [], Config) | Acc], Config);
Expand Down Expand Up @@ -217,8 +215,8 @@ iolist_to_string(X, _) when is_atom(X) -> atom_to_list(X);
iolist_to_string(X, _) when is_integer(X) -> X;
iolist_to_string(X, _) when is_pid(X) -> pid_to_list(X);
iolist_to_string(X, _) when is_reference(X) -> ref_to_list(X);

iolist_to_string(<<>>, _) -> <<>>;

iolist_to_string(X, Config) when is_binary(X) ->
case is_printable(X) of
true -> X;
Expand All @@ -229,8 +227,7 @@ iolist_to_string(X, Config) when is_list(X) ->
% case printable_list(lists:flatten(X)) of
case printable_list(X) of
true -> X;
_ ->
io_lib:format(p(Config), [X])
_ -> io_lib:format(p(Config), [X])
end;

iolist_to_string(X, Config) -> io_lib:format(p(Config), [X]).
Expand Down Expand Up @@ -273,16 +270,13 @@ print_string(X, Config) when is_list(X) ->
format_msg({string, Chardata}, Meta, Config) when is_binary(Chardata) ->
case is_printable(Chardata) of
true -> format_msg({"~ts", [Chardata]}, Meta, Config);
false ->format_msg({"~tp", [Chardata]}, Meta, Config)
false -> format_msg({"~tp", [Chardata]}, Meta, Config)
end;

format_msg({string, Chardata}, Meta, Config) ->
case io_lib:printable_unicode_list(Chardata) of
true -> format_msg({"~ts", [Chardata]}, Meta, Config);
false ->
try
format_msg({"~ts", [process_io_list(Chardata, Config)]}, Meta, Config)
catch
false -> try format_msg({"~ts", [process_io_list(Chardata, Config)]}, Meta, Config) catch
% _:_ ->
% format_msg({"~tp", [Chardata]}, Meta, Config)
C:R : S ->
Expand All @@ -295,8 +289,7 @@ format_msg({string, Chardata}, Meta, Config) ->
},
Meta,
Config
)
end
) end
end;

format_msg({report, Report}, _Meta, Config) when is_map(Report) ->
Expand Down Expand Up @@ -604,6 +597,7 @@ get_utc_config() ->
end
end.


-spec check_config(Config) -> ok | {error, term()} when Config :: config().
check_config(Config) when is_map(Config) -> do_check_config(maps:to_list(Config));
check_config(Config) -> {error, {invalid_formatter_config, ?MODULE, Config}}.
Expand Down
2 changes: 1 addition & 1 deletion test/logger_formatter_json_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ to_string(_) ->
<<"793µs"/utf8>>,
iolist_to_binary(logger_formatter_json:to_string(<<"793µs"/utf8>>, Config))
),
?assertEqual("<0.250.0>", logger_formatter_json:to_string(c:pid(0,250,0), Config)).
?assertEqual("<0.250.0>", logger_formatter_json:to_string(c:pid(0, 250, 0), Config)).


printable_list(_) -> ?assertEqual(true, logger_formatter_json:printable_list("foo")).
Expand Down

0 comments on commit c5be13a

Please sign in to comment.