From 95fdf935b9279948c67b74a898952833e101cb55 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Thu, 13 Dec 2012 01:38:38 -0500 Subject: [PATCH] Support variables as arguments to lager:info and friends As long as *one* of the arguments is a literal, lager can figure out what you're trying to do and (re)arrange the arguments as necessary. --- src/lager_transform.erl | 16 ++++++++++++++++ test/lager_test_backend.erl | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/lager_transform.erl b/src/lager_transform.erl index 387aa9e..6a45040 100644 --- a/src/lager_transform.erl +++ b/src/lager_transform.erl @@ -111,6 +111,18 @@ transform_statement({call, Line, {remote, _Line1, {atom, _Line2, lager}, {cons, _, {tuple, _, _}, _} -> {concat_lists(Arg1, DefaultAttrs), Arg2, {atom, Line, none}}; + {var, _, _} -> + %% crap, its a variable. look at the second + %% argument to see if it is a string + case Arg2 of + {string, _, _} -> + {concat_lists(Arg1, DefaultAttrs), + Arg2, {atom, Line, none}}; + _ -> + %% not a string, going to have to guess + %% its the argument list + {DefaultAttrs, Arg1, Arg2} + end; _ -> {DefaultAttrs, Arg1, Arg2} end; @@ -146,6 +158,10 @@ transform_statement(Stmt) -> Stmt. %% concat 2 list ASTs by replacing the terminating [] in A with the contents of B +concat_lists({var, Line, Name}, B) -> + %% concatenating a var with a cons + {call, Line, {remote, Line, {atom, Line, lists},{atom, Line, flatten}}, + [{cons, Line, {var, Line, Name}, B}]}; concat_lists({nil, _Line}, B) -> B; concat_lists({cons, Line, Element, Tail}, B) -> diff --git a/test/lager_test_backend.erl b/test/lager_test_backend.erl index 9e30182..9da5be3 100644 --- a/test/lager_test_backend.erl +++ b/test/lager_test_backend.erl @@ -180,6 +180,36 @@ lager_test_() -> ok end }, + {"variables inplace of literals in logging statements work", + fun() -> + ?assertEqual(0, count()), + Attr = [{a, alpha}, {b, beta}], + Fmt = "format ~p", + Args = [world], + lager:info(Attr, "hello"), + lager:info(Attr, "hello ~p", [world]), + lager:info(Fmt, [world]), + lager:info("hello ~p", Args), + lager:info(Attr, "hello ~p", Args), + lager:info([{d, delta}, {g, gamma}], Fmt, Args), + ?assertEqual(6, count()), + {_Level, _Time, Message, Metadata} = pop(), + ?assertMatch([{a, alpha}, {b, beta}|_], Metadata), + ?assertEqual("hello", lists:flatten(Message)), + {_Level, _Time2, Message2, _Metadata2} = pop(), + ?assertEqual("hello world", lists:flatten(Message2)), + {_Level, _Time3, Message3, _Metadata3} = pop(), + ?assertEqual("format world", lists:flatten(Message3)), + {_Level, _Time4, Message4, _Metadata4} = pop(), + ?assertEqual("hello world", lists:flatten(Message4)), + {_Level, _Time5, Message5, _Metadata5} = pop(), + ?assertEqual("hello world", lists:flatten(Message5)), + {_Level, _Time6, Message6, Metadata6} = pop(), + ?assertMatch([{d, delta}, {g, gamma}|_], Metadata6), + ?assertEqual("format world", lists:flatten(Message6)), + ok + end + }, {"log messages below the threshold are ignored", fun() -> ?assertEqual(0, count()),