mirror of
https://github.com/correl/rebar.git
synced 2024-12-18 11:06:20 +00:00
Support different log levels
This commit is contained in:
parent
263b49f970
commit
015e58201c
4 changed files with 34 additions and 18 deletions
|
@ -156,7 +156,9 @@ parse_args(Args) ->
|
||||||
proplists:get_bool(profile, Options)),
|
proplists:get_bool(profile, Options)),
|
||||||
|
|
||||||
%% Set global variables based on getopt options
|
%% Set global variables based on getopt options
|
||||||
set_global_flag(Options, verbose),
|
LogLevel = proplists:get_value(verbose, Options,
|
||||||
|
rebar_log:default_level()),
|
||||||
|
rebar_config:set_global(verbose, LogLevel),
|
||||||
set_global_flag(Options, force),
|
set_global_flag(Options, force),
|
||||||
DefJobs = rebar_config:get_jobs(),
|
DefJobs = rebar_config:get_jobs(),
|
||||||
case proplists:get_value(jobs, Options, DefJobs) of
|
case proplists:get_value(jobs, Options, DefJobs) of
|
||||||
|
@ -280,7 +282,7 @@ option_spec_list() ->
|
||||||
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
|
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
|
||||||
{help, $h, "help", undefined, "Show the program options"},
|
{help, $h, "help", undefined, "Show the program options"},
|
||||||
{commands, $c, "commands", undefined, "Show available commands"},
|
{commands, $c, "commands", undefined, "Show available commands"},
|
||||||
{verbose, $v, "verbose", undefined, "Be verbose about what gets done"},
|
{verbose, $v, "verbose", integer, "Verbosity level"},
|
||||||
{version, $V, "version", undefined, "Show version information"},
|
{version, $V, "version", undefined, "Show version information"},
|
||||||
{force, $f, "force", undefined, "Force"},
|
{force, $f, "force", undefined, "Force"},
|
||||||
{defines, $D, undefined, string, "Define compiler macro"},
|
{defines, $D, undefined, string, "Define compiler macro"},
|
||||||
|
@ -299,8 +301,14 @@ filter_flags([Item | Rest], Commands) ->
|
||||||
case string:tokens(Item, "=") of
|
case string:tokens(Item, "=") of
|
||||||
[Command] ->
|
[Command] ->
|
||||||
filter_flags(Rest, [Command | Commands]);
|
filter_flags(Rest, [Command | Commands]);
|
||||||
[KeyStr, Value] ->
|
[KeyStr, RawValue] ->
|
||||||
Key = list_to_atom(KeyStr),
|
Key = list_to_atom(KeyStr),
|
||||||
|
Value = case Key of
|
||||||
|
verbose ->
|
||||||
|
list_to_integer(RawValue);
|
||||||
|
_ ->
|
||||||
|
RawValue
|
||||||
|
end,
|
||||||
rebar_config:set_global(Key, Value),
|
rebar_config:set_global(Key, Value),
|
||||||
filter_flags(Rest, Commands);
|
filter_flags(Rest, Commands);
|
||||||
Other ->
|
Other ->
|
||||||
|
|
|
@ -122,7 +122,8 @@ get_global(Key, Default) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
is_verbose() ->
|
is_verbose() ->
|
||||||
get_global(verbose, "0") =:= "1".
|
DefaulLevel = rebar_log:default_level(),
|
||||||
|
get_global(verbose, DefaulLevel) > DefaulLevel.
|
||||||
|
|
||||||
get_jobs() ->
|
get_jobs() ->
|
||||||
get_global(jobs, 3).
|
get_global(jobs, 3).
|
||||||
|
|
|
@ -64,10 +64,10 @@ run_test_if_present(TestDir, Config, File) ->
|
||||||
run_test(TestDir, Config, _File) ->
|
run_test(TestDir, Config, _File) ->
|
||||||
{Cmd, RawLog} = make_cmd(TestDir, Config),
|
{Cmd, RawLog} = make_cmd(TestDir, Config),
|
||||||
clear_log(RawLog),
|
clear_log(RawLog),
|
||||||
case rebar_config:get_global(verbose, "0") of
|
case rebar_config:is_verbose() of
|
||||||
"0" ->
|
false ->
|
||||||
Output = " >> " ++ RawLog ++ " 2>&1";
|
Output = " >> " ++ RawLog ++ " 2>&1";
|
||||||
_ ->
|
true ->
|
||||||
Output = " 2>&1 | tee -a " ++ RawLog
|
Output = " 2>&1 | tee -a " ++ RawLog
|
||||||
end,
|
end,
|
||||||
|
|
||||||
|
@ -112,11 +112,11 @@ check_log(RawLog) ->
|
||||||
%% Show the log if it hasn't already been shown because verbose was on
|
%% Show the log if it hasn't already been shown because verbose was on
|
||||||
show_log(RawLog) ->
|
show_log(RawLog) ->
|
||||||
?CONSOLE("Showing log\n", []),
|
?CONSOLE("Showing log\n", []),
|
||||||
case rebar_config:get_global(verbose, "0") of
|
case rebar_config:is_verbose() of
|
||||||
"0" ->
|
false ->
|
||||||
{ok, Contents} = file:read_file(RawLog),
|
{ok, Contents} = file:read_file(RawLog),
|
||||||
?CONSOLE("~s", [Contents]);
|
?CONSOLE("~s", [Contents]);
|
||||||
_ ->
|
true ->
|
||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@
|
||||||
-module(rebar_log).
|
-module(rebar_log).
|
||||||
|
|
||||||
-export([init/0,
|
-export([init/0,
|
||||||
set_level/1, get_level/0,
|
set_level/1, get_level/0, default_level/0,
|
||||||
log/3]).
|
log/3]).
|
||||||
|
|
||||||
%% ===================================================================
|
%% ===================================================================
|
||||||
|
@ -35,14 +35,13 @@
|
||||||
%% ===================================================================
|
%% ===================================================================
|
||||||
|
|
||||||
init() ->
|
init() ->
|
||||||
case rebar_config:get_global(verbose, "0") of
|
case valid_level(rebar_config:get_global(verbose, error_level())) of
|
||||||
"1" ->
|
0 -> set_level(error);
|
||||||
set_level(debug);
|
1 -> set_level(warn);
|
||||||
_ ->
|
2 -> set_level(info);
|
||||||
set_level(error)
|
3 -> set_level(debug)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
set_level(Level) ->
|
set_level(Level) ->
|
||||||
ok = application:set_env(rebar, log_level, Level).
|
ok = application:set_env(rebar, log_level, Level).
|
||||||
|
|
||||||
|
@ -63,10 +62,18 @@ log(Level, Str, Args) ->
|
||||||
ok
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
default_level() -> error_level().
|
||||||
|
|
||||||
%% ===================================================================
|
%% ===================================================================
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%% ===================================================================
|
%% ===================================================================
|
||||||
|
|
||||||
|
valid_level(Level) ->
|
||||||
|
erlang:max(error_level(), erlang:min(Level, debug_level())).
|
||||||
|
|
||||||
|
error_level() -> 0.
|
||||||
|
debug_level() -> 3.
|
||||||
|
|
||||||
should_log(debug, _) -> true;
|
should_log(debug, _) -> true;
|
||||||
should_log(info, debug) -> false;
|
should_log(info, debug) -> false;
|
||||||
should_log(info, _) -> true;
|
should_log(info, _) -> true;
|
||||||
|
|
Loading…
Reference in a new issue