Browse Source

Merge branch 'feature/otp-17' into intg

pull/277/head
Mark Allen 10 years ago
parent
commit
d40ddd01ce
4 changed files with 158 additions and 42 deletions
  1. +46
    -4
      rebar.config
  2. +4
    -4
      src/error_logger_lager_h.erl
  3. +19
    -19
      src/lager_trunc_io.erl
  4. +89
    -15
      test/lager_test_backend.erl

+ 46
- 4
rebar.config View File

@ -1,9 +1,51 @@
{erl_opts, [{lager_extra_sinks, ['__lager_test_sink']}, debug_info, warn_untyped_record]}.
%% -*- erlang -*-
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2011-2015 Basho Technologies, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
{erl_opts, [
{lager_extra_sinks, ['__lager_test_sink']}
debug_info,
report,
verbose,
warn_deprecated_function,
warn_deprecated_type,
warn_export_all,
warn_export_vars,
warn_obsolete_guard,
warn_untyped_record,
warn_unused_import
% do NOT include warnings_as_errors, as rebar includes these options
% when compiling for eunit, and at least one test module has code that
% is deliberatly broken and will generate an un-maskable warning
]}.
{erl_first_files, ["src/lager_util.erl"]}.
{eunit_opts, [verbose]}.
{eunit_compile_opts, [
nowarn_untyped_record,
nowarn_export_all
]}.
{deps, [
{goldrush, "0\.1\.6",
{git, "git://github.com/DeadZen/goldrush.git", {tag, "0.1.6"}}}
]}.
{goldrush, ".*", {git, "git://github.com/DeadZen/goldrush.git", {branch, "master"}}}
]}.
{xref_checks, []}.
{xref_queries, [{"(XC - UC) || (XU - X - B - lager_default_tracer : Mod - erlang:\"(is_map|map_size)\"/1 - maps:to_list/1)", []}]}.

+ 4
- 4
src/error_logger_lager_h.erl View File

@ -1,4 +1,4 @@
%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
%% Copyright (c) 2011-2015 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
@ -197,7 +197,7 @@ log_event(Event, State) ->
?LOGFMT(error, Pid, "Webmachine error at path ~p : ~s", [Path, format_reason(StackTrace)]);
_ ->
?CRASH_LOG(Event),
?LOGMSG(error, Pid, lager:safe_format(Fmt, Args, ?DEFAULT_TRUNCATION))
?LOGFMT(error, Pid, Fmt, Args)
end;
{error_report, _GL, {Pid, std_error, D}} ->
?CRASH_LOG(Event),
@ -217,11 +217,11 @@ log_event(Event, State) ->
?CRASH_LOG(Event),
?LOGMSG(error, Pid, "CRASH REPORT " ++ format_crash_report(Self, Neighbours));
{warning_msg, _GL, {Pid, Fmt, Args}} ->
?LOGMSG(warning, Pid, lager:safe_format(Fmt, Args, ?DEFAULT_TRUNCATION));
?LOGFMT(warning, Pid, Fmt, Args);
{warning_report, _GL, {Pid, std_warning, Report}} ->
?LOGMSG(warning, Pid, print_silly_list(Report));
{info_msg, _GL, {Pid, Fmt, Args}} ->
?LOGMSG(info, Pid, lager:safe_format(Fmt, Args, ?DEFAULT_TRUNCATION));
?LOGFMT(info, Pid, Fmt, Args);
{info_report, _GL, {Pid, std_info, D}} when is_list(D) ->
Details = lists:sort(D),
case Details of

+ 19
- 19
src/lager_trunc_io.erl View File

@ -3,12 +3,12 @@
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with your Erlang distribution. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%%
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%%
%% The Initial Developer of the Original Code is Corelatus AB.
%% Portions created by Corelatus are Copyright 2003, Corelatus
%% AB. All Rights Reserved.''
@ -32,7 +32,7 @@
-module(lager_trunc_io).
-author('matthias@corelatus.se').
%% And thanks to Chris Newcombe for a bug fix
%% And thanks to Chris Newcombe for a bug fix
-export([format/3, format/4, print/2, print/3, fprint/2, fprint/3, safe/2]). % interface functions
-version("$Id: trunc_io.erl,v 1.11 2009-02-23 12:01:06 matthias Exp $").
@ -76,11 +76,11 @@ fprint(Term, Max) ->
%% @doc Returns an flattened list containing the ASCII representation of the given
%% term.
-spec fprint(term(), pos_integer(), options()) -> string().
fprint(T, Max, Options) ->
fprint(T, Max, Options) ->
{L, _} = print(T, Max, prepare_options(Options, #print_options{})),
lists:flatten(L).
%% @doc Same as print, but never crashes.
%% @doc Same as print, but never crashes.
%%
%% This is a tradeoff. Print might conceivably crash if it's asked to
%% print something it doesn't understand, for example some new data
@ -88,7 +88,7 @@ fprint(T, Max, Options) ->
%% to io_lib to format the term, but then the formatting is
%% depth-limited instead of length limited, so you might run out
%% memory printing it. Out of the frying pan and into the fire.
%%
%%
-spec safe(term(), pos_integer()) -> {string(), pos_integer()} | {string()}.
safe(What, Len) ->
case catch print(What, Len) of
@ -114,8 +114,8 @@ print(_, Max, _Options) when Max < 0 -> {"...", 3};
print(_, _, #print_options{depth=0}) -> {"...", 3};
%% @doc We assume atoms, floats, funs, integers, PIDs, ports and refs never need
%% to be truncated. This isn't strictly true, someone could make an
%% @doc We assume atoms, floats, funs, integers, PIDs, ports and refs never need
%% to be truncated. This isn't strictly true, someone could make an
%% arbitrarily long bignum. Let's assume that won't happen unless someone
%% is being malicious.
%%
@ -214,15 +214,15 @@ print({inline_bitstring, B}, _Max, _Options) when is_bitstring(B) ->
SizeStr = integer_to_list(Size),
{[ValueStr, $:, SizeStr], length(ValueStr) + length(SizeStr) +1};
print(BitString, Max, Options) when is_bitstring(BitString) ->
case byte_size(BitString) > Max of
BL = case byte_size(BitString) > Max of
true ->
BL = binary_to_list(BitString, 1, Max);
binary_to_list(BitString, 1, Max);
_ ->
R = erlang:bitstring_to_list(BitString),
{Bytes, [Bits]} = lists:splitwith(fun erlang:is_integer/1, R),
%% tag the trailing bits with a special tuple we catch when
%% list_body calls print again
BL = Bytes ++ [{inline_bitstring, Bits}]
Bytes ++ [{inline_bitstring, Bits}]
end,
{X, Len0} = list_body(BL, Max - 4, dec_depth(Options), true),
{["<<", X, ">>"], Len0 + 4};
@ -265,7 +265,7 @@ print({'$lager_record', Name, Fields}, Max, Options) ->
{RC, Len} = record_fields(Fields, Max - length(Leader) + 1, dec_depth(Options)),
{[Leader, RC, "}"], Len + length(Leader) + 1};
print(Tuple, Max, Options) when is_tuple(Tuple) ->
print(Tuple, Max, Options) when is_tuple(Tuple) ->
{TC, Len} = tuple_contents(Tuple, Max-2, Options),
{[${, TC, $}], Len + 2};
@ -307,7 +307,7 @@ list_body([H|_], Max, Options=#print_options{depth=1}, Tuple) ->
false -> $|
end,
{[List ++ [Sep | "..."]], Len + 4};
list_body([H|T], Max, Options, Tuple) ->
list_body([H|T], Max, Options, Tuple) ->
{List, Len} = print(H, Max, Options),
{Final, FLen} = list_bodyc(T, Max - Len, Options, Tuple),
{[List|Final], FLen + Len};
@ -319,7 +319,7 @@ list_bodyc([], _Max, _Options, _Tuple) -> {[], 0};
list_bodyc(_, Max, _Options, _Tuple) when Max < 5 -> {",...", 4};
list_bodyc(_, _Max, #print_options{depth=1}, true) -> {",...", 4};
list_bodyc(_, _Max, #print_options{depth=1}, false) -> {"|...", 4};
list_bodyc([H|T], Max, #print_options{depth=Depth} = Options, Tuple) ->
list_bodyc([H|T], Max, #print_options{depth=Depth} = Options, Tuple) ->
{List, Len} = print(H, Max, dec_depth(Options)),
{Final, FLen} = list_bodyc(T, Max - Len - 1, dec_depth(Options), Tuple),
Sep = case Depth == 1 andalso not Tuple of
@ -553,7 +553,7 @@ perf(M, F, Reps) when Reps > 0 ->
test(M,F),
perf(M,F,Reps-1);
perf(_,_,_) ->
done.
done.
%% Performance test. Needs a particularly large term I saved as a binary...
-spec perf1() -> {non_neg_integer(), non_neg_integer()}.
@ -570,7 +570,7 @@ format_test() ->
?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~p", [["foo", $b, $a, $r]], 50))),
?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~P", [["foo", $b, $a, $r], 10], 50))),
?assertEqual("[[102,111,111],98,97,114]", lists:flatten(format("~w", [["foo", $b, $a, $r]], 50))),
%% complex ones
?assertEqual(" foobar", lists:flatten(format("~10s", [["foo", $b, $a, $r]], 50))),
?assertEqual("f", lists:flatten(format("~1s", [["foo", $b, $a, $r]], 50))),
@ -836,7 +836,7 @@ depth_limit_test() ->
?assertEqual("[1|...]", lists:flatten(format("~P", [[1, 2, 3], 2], 50))),
?assertEqual("[1,2|...]", lists:flatten(format("~P", [[1, 2, 3], 3], 50))),
?assertEqual("[1,2,3]", lists:flatten(format("~P", [[1, 2, 3], 4], 50))),
?assertEqual("{1,...}", lists:flatten(format("~P", [{1, 2, 3}, 2], 50))),
?assertEqual("{1,2,...}", lists:flatten(format("~P", [{1, 2, 3}, 3], 50))),
?assertEqual("{1,2,3}", lists:flatten(format("~P", [{1, 2, 3}, 4], 50))),
@ -845,13 +845,13 @@ depth_limit_test() ->
?assertEqual("[1,2|...]", lists:flatten(format("~P", [[1, 2, <<3>>], 3], 50))),
?assertEqual("[1,2,<<...>>]", lists:flatten(format("~P", [[1, 2, <<3>>], 4], 50))),
?assertEqual("[1,2,<<3>>]", lists:flatten(format("~P", [[1, 2, <<3>>], 5], 50))),
?assertEqual("<<...>>", lists:flatten(format("~P", [<<0, 0, 0, 0>>, 1], 50))),
?assertEqual("<<0,...>>", lists:flatten(format("~P", [<<0, 0, 0, 0>>, 2], 50))),
?assertEqual("<<0,0,...>>", lists:flatten(format("~P", [<<0, 0, 0, 0>>, 3], 50))),
?assertEqual("<<0,0,0,...>>", lists:flatten(format("~P", [<<0, 0, 0, 0>>, 4], 50))),
?assertEqual("<<0,0,0,0>>", lists:flatten(format("~P", [<<0, 0, 0, 0>>, 5], 50))),
%% this is a seriously weird edge case
?assertEqual("<<\" \"...>>", lists:flatten(format("~P", [<<32, 32, 32, 0>>, 2], 50))),
?assertEqual("<<\" \"...>>", lists:flatten(format("~P", [<<32, 32, 32, 0>>, 3], 50))),

+ 89
- 15
test/lager_test_backend.erl View File

@ -1,4 +1,6 @@
%% Copyright (c) 2011-2012 Basho Technologies, Inc. All Rights Reserved.
%% -------------------------------------------------------------------
%%
%% Copyright (c) 2011-2015 Basho Technologies, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
@ -13,6 +15,8 @@
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------
-module(lager_test_backend).
@ -130,9 +134,25 @@ print_bad_state(Sink) ->
has_line_numbers() ->
%% are we R15 or greater
Rel = erlang:system_info(otp_release),
{match, [Major]} = re:run(Rel, "(?|(^R(\\d+)[A|B](|0(\\d)))|(^(\\d+)$))", [{capture, [2], list}]),
list_to_integer(Major) >= 15.
% this gets called a LOT - cache the answer
case erlang:get({?MODULE, has_line_numbers}) of
undefined ->
R = otp_version() >= 15,
erlang:put({?MODULE, has_line_numbers}, R),
R;
Bool ->
Bool
end.
otp_version() ->
otp_version(erlang:system_info(otp_release)).
otp_version([$R | Rel]) ->
{Ver, _} = string:to_integer(Rel),
Ver;
otp_version(Rel) ->
{Ver, _} = string:to_integer(Rel),
Ver.
not_running_test() ->
?assertEqual({error, lager_not_running}, lager:log(info, self(), "not running")).
@ -699,20 +719,42 @@ crash(Type) ->
test_body(Expected, Actual) ->
case has_line_numbers() of
true ->
FileLine = string:substr(Actual, length(Expected)+1),
Body = string:substr(Actual, 1, length(Expected)),
ExLen = length(Expected),
{Body, Rest} = case length(Actual) > ExLen of
true ->
{string:substr(Actual, 1, ExLen),
string:substr(Actual, (ExLen + 1))};
_ ->
{Actual, []}
end,
?assertEqual(Expected, Body),
case string:substr(FileLine, 1, 6) of
% OTP-17 (and maybe later releases) may tack on additional info
% about the failure, so if Actual starts with Expected (already
% confirmed by having gotten past assertEqual above) and ends
% with " line NNN" we can ignore what's in-between. By extension,
% since there may not be line information appended at all, any
% text we DO find is reportable, but not a test failure.
case Rest of
[] ->
%% sometimes there's no line information...
?assert(true);
" line " ->
?assert(true);
Other ->
?debugFmt("unexpected trailing data ~p", [Other]),
?assert(false)
ok;
_ ->
% isolate the extra data and report it if it's not just
% a line number indicator
case re:run(Rest, "^.*( line \\d+)$", [{capture, [1]}]) of
nomatch ->
?debugFmt(
"Trailing data \"~s\" following \"~s\"",
[Rest, Expected]);
{match, [{0, _}]} ->
% the whole sting is " line NNN"
ok;
{match, [{Off, _}]} ->
?debugFmt(
"Trailing data \"~s\" following \"~s\"",
[string:substr(Rest, 1, Off), Expected])
end
end;
false ->
_ ->
?assertEqual(Expected, Actual)
end.
@ -829,6 +871,17 @@ error_logger_redirect_test_() ->
?assertEqual(Expected, lists:flatten(Msg))
end
},
{"error messages with unicode characters in Args are printed",
fun() ->
sync_error_logger:error_msg("~ts", ["Привет!"]),
_ = gen_event:which_handlers(error_logger),
{Level, _, Msg,Metadata} = pop(),
?assertEqual(lager_util:level_to_num(error),Level),
?assertEqual(self(),proplists:get_value(pid,Metadata)),
?assertEqual("Привет!", lists:flatten(Msg))
end
},
{"error messages are truncated at 4096 characters",
fun() ->
sync_error_logger:error_msg("doom, doom has come upon you all ~p", [string:copies("doom", 10000)]),
@ -928,6 +981,27 @@ error_logger_redirect_test_() ->
?assert(length(lists:flatten(Msg)) < 5100)
end
},
{"info messages with unicode characters in Args are printed",
fun() ->
sync_error_logger:info_msg("~ts", ["Привет!"]),
_ = gen_event:which_handlers(error_logger),
{Level, _, Msg,Metadata} = pop(),
?assertEqual(lager_util:level_to_num(info),Level),
?assertEqual(self(),proplists:get_value(pid,Metadata)),
?assertEqual("Привет!", lists:flatten(Msg))
end
},
{"warning messages with unicode characters in Args are printed",
fun() ->
sync_error_logger:warning_msg("~ts", ["Привет!"]),
Map = error_logger:warning_map(),
_ = gen_event:which_handlers(error_logger),
{Level, _, Msg,Metadata} = pop(),
?assertEqual(lager_util:level_to_num(Map),Level),
?assertEqual(self(),proplists:get_value(pid,Metadata)),
?assertEqual("Привет!", lists:flatten(Msg))
end
},
{"warning messages are printed at the correct level",
fun() ->

Loading…
Cancel
Save