From a0440a9d608f205df6d8319c18cec3a74b194cb4 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Mon, 29 Aug 2011 18:42:56 -0400 Subject: [PATCH 1/3] Smarter doublequote stripping when printing strings with ~s --- src/lager_trunc_io.erl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lager_trunc_io.erl b/src/lager_trunc_io.erl index 9d78e52..bc693d3 100644 --- a/src/lager_trunc_io.erl +++ b/src/lager_trunc_io.erl @@ -101,8 +101,14 @@ format([[$~|H]| T], [AH | AT], Max, Acc, ArgAcc) when length(H) == 1 -> {String, Length} -> {Value, RealLen} = case H of "s" -> - % strip off the doublequotes - {string:substr(String, 2, length(String) -2), Length -2}; + % strip off the doublequotes, if applicable + Trimmed = case {hd(String), lists:last(String)} == {$", $"} of + true -> + string:strip(String, both, $"); + _ -> + String + end, + {Trimmed, length(Trimmed)}; _ -> {String, Length} end, @@ -428,4 +434,10 @@ sane_float_printing_test() -> ?assertEqual("0.1234567", lists:flatten(format("~p", [0.1234567], 50))), ok. +quote_strip_test() -> + ?assertEqual("\"hello\"", lists:flatten(format("~p", ["hello"], 50))), + ?assertEqual("hello", lists:flatten(format("~s", ["hello"], 50))), + ?assertEqual("hello", lists:flatten(format("~s", [hello], 50))), + ?assertEqual("hello", lists:flatten(format("~p", [hello], 50))), + ok. -endif. From af4ea7f31d2a51e45faf9924bb5c2cd3d3a11fbe Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Mon, 29 Aug 2011 18:43:36 -0400 Subject: [PATCH 2/3] Don't treat floats in a list as printable characters Reported by @bryanhughes The test if an element in a list was in the printable range didn't handle the case of a float in the list that compared to be in the printable range. --- src/lager_trunc_io.erl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/lager_trunc_io.erl b/src/lager_trunc_io.erl index bc693d3..e0a6073 100644 --- a/src/lager_trunc_io.erl +++ b/src/lager_trunc_io.erl @@ -309,10 +309,10 @@ list_bodyc(X,Max) -> %% improper list %% alist_start([], _) -> {"[]", 2}; alist_start(_, Max) when Max < 4 -> {"...", 3}; -alist_start([H|T], Max) when H >= 16#20, H =< 16#7e -> % definitely printable +alist_start([H|T], Max) when is_integer(H), H >= 16#20, H =< 16#7e -> % definitely printable {L, Len} = alist([H|T], Max-1), {[$"|L], Len + 1}; -alist_start([H|T], Max) when H == 9; H == 10; H == 13 -> % show as space +alist_start([H|T], Max) when H =:= 9; H =:= 10; H =:= 13 -> % show as space {L, Len} = alist(T, Max-1), {[$ |L], Len + 1}; alist_start(L, Max) -> @@ -321,10 +321,10 @@ alist_start(L, Max) -> alist([], _) -> {"\"", 1}; alist(_, Max) when Max < 5 -> {"...\"", 4}; -alist([H|T], Max) when H >= 16#20, H =< 16#7e -> % definitely printable +alist([H|T], Max) when is_integer(H), H >= 16#20, H =< 16#7e -> % definitely printable {L, Len} = alist(T, Max-1), {[H|L], Len + 1}; -alist([H|T], Max) when H == 9; H == 10; H == 13 -> % show as space +alist([H|T], Max) when H =:= 9; H =:= 10; H =:= 13 -> % show as space {L, Len} = alist(T, Max-1), {[$ |L], Len + 1}; alist(L, Max) -> @@ -434,6 +434,11 @@ sane_float_printing_test() -> ?assertEqual("0.1234567", lists:flatten(format("~p", [0.1234567], 50))), ok. +float_inside_list_test() -> + ?assertEqual("\"a\"[38.233913133184835,99]", lists:flatten(format("~p", [[$a, 38.233913133184835, $c]], 50))), + ?assertEqual("\"a\"[38.233913133184835,99]", lists:flatten(format("~s", [[$a, 38.233913133184835, $c]], 50))), + ok. + quote_strip_test() -> ?assertEqual("\"hello\"", lists:flatten(format("~p", ["hello"], 50))), ?assertEqual("hello", lists:flatten(format("~s", ["hello"], 50))), From 0cc9e252248f475246eb950914ec573ea4a2c2d9 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Mon, 29 Aug 2011 18:55:09 -0400 Subject: [PATCH 3/3] Remove incorrect test case --- test/lager_test_backend.erl | 1 - 1 file changed, 1 deletion(-) diff --git a/test/lager_test_backend.erl b/test/lager_test_backend.erl index 35b860a..01c1ec1 100644 --- a/test/lager_test_backend.erl +++ b/test/lager_test_backend.erl @@ -647,7 +647,6 @@ error_logger_redirect_test_() -> safe_format_test() -> ?assertEqual("foo bar", lists:flatten(lager:safe_format("~p ~p", [foo, bar], 1024))), ?assertEqual("FORMAT ERROR: \"~p ~p ~p\" [foo,bar]", lists:flatten(lager:safe_format("~p ~p ~p", [foo, bar], 1024))), - ?assertEqual("FORMAT ERROR: \"~s\" [1]", lists:flatten(lager:safe_format("~s", [1], 1024))), ok. -endif.