Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Try to support printing arbitrary terms without format string

This was the accidental behaviour in 0.9, and apparently people depend
on it. Issue #41.
  • Loading branch information...
commit 3852d03e7279521e6a8493b1e8e157373e433233 1 parent b46e377
Andrew Thompson Vagabond authored
Showing with 39 additions and 12 deletions.
  1. +29 −12 src/lager_format.erl
  2. +10 −0 src/lager_trunc_io.erl
41 src/lager_format.erl
View
@@ -29,18 +29,35 @@
format(FmtStr, Args, MaxLen) ->
format(FmtStr, Args, MaxLen, []).
-format(FmtStr, Args, MaxLen, Opts) ->
- Options = make_options(Opts, #options{}),
- Cs = collect(FmtStr, Args),
- {Cs2, MaxLen2} = build(Cs, [], MaxLen, Options),
- %% count how many terms remain
- {Count, StrLen} = lists:foldl(
- fun({_C, _As, _F, _Adj, _P, _Pad, _Enc}, {Terms, Chars}) ->
- {Terms + 1, Chars};
- (_, {Terms, Chars}) ->
- {Terms, Chars + 1}
- end, {0, 0}, Cs2),
- build2(Cs2, Count, MaxLen2 - StrLen).
+format(FmtStr, Args, MaxLen, Opts) when is_atom(FmtStr) ->
+ format(erlang:atom_to_list(FmtStr), Args, MaxLen, Opts);
+format(FmtStr, Args, MaxLen, Opts) when is_binary(FmtStr) ->
+ format(erlang:binary_to_list(FmtStr), Args, MaxLen, Opts);
+format(FmtStr, [], MaxLen, Opts) when not is_list(FmtStr) ->
+ %% lager 0.9 behaviour let you print arbitrary terms without a format string.
+ %% This is emulation for that behaviour.
+ format("~p", [FmtStr], MaxLen, Opts);
+format(FmtStr, Args, MaxLen, Opts) when is_list(FmtStr) ->
+ case lager_stdlib:string_p(FmtStr) of
+ true ->
+ Options = make_options(Opts, #options{}),
+ Cs = collect(FmtStr, Args),
+ {Cs2, MaxLen2} = build(Cs, [], MaxLen, Options),
+ %% count how many terms remain
+ {Count, StrLen} = lists:foldl(
+ fun({_C, _As, _F, _Adj, _P, _Pad, _Enc}, {Terms, Chars}) ->
+ {Terms + 1, Chars};
+ (_, {Terms, Chars}) ->
+ {Terms, Chars + 1}
+ end, {0, 0}, Cs2),
+ build2(Cs2, Count, MaxLen2 - StrLen);
+ false when Args == [] ->
+ format("~p", [FmtStr], MaxLen, Opts);
+ _ ->
+ erlang:error(badarg)
+ end;
+format(_, _, _, _) ->
+ erlang:error(badarg).
collect([$~|Fmt0], Args0) ->
{C,Fmt1,Args1} = collect_cseq(Fmt0, Args0),
10 src/lager_trunc_io.erl
View
@@ -573,4 +573,14 @@ depth_limit_test() ->
?assertEqual("{\"a\",[\"b\",[\"c\",[\"d\"]]]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 9], 50))),
ok.
+print_terms_without_format_string_test() ->
+ ?assertEqual("{hello,world}", lists:flatten(format({hello, world}, [], 50))),
+ ?assertEqual("[{google,bomb}]", lists:flatten(format([{google, bomb}], [], 50))),
+ ?assertEqual("hello", lists:flatten(format(<<"hello">>, [], 50))),
+ ?assertEqual("hello", lists:flatten(format('hello', [], 50))),
+ ?assertEqual("<<1,2,3,1:7>>", lists:flatten(format(
+ <<1, 2, 3, 1:7>>, [], 100))),
+ ?assertEqual("65535", lists:flatten(format(65535, [], 50))),
+ ok.
+
-endif.
Please sign in to comment.
Something went wrong with that request. Please try again.