Skip to content

Commit

Permalink
Support different log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuncer Ayaz committed Jan 17, 2012
1 parent 263b49f commit 015e582
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
14 changes: 11 additions & 3 deletions src/rebar.erl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ parse_args(Args) ->
proplists:get_bool(profile, 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),
DefJobs = rebar_config:get_jobs(),
case proplists:get_value(jobs, Options, DefJobs) of
Expand Down Expand Up @@ -280,7 +282,7 @@ option_spec_list() ->
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
{help, $h, "help", undefined, "Show the program options"},
{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"},
{force, $f, "force", undefined, "Force"},
{defines, $D, undefined, string, "Define compiler macro"},
Expand All @@ -299,8 +301,14 @@ filter_flags([Item | Rest], Commands) ->
case string:tokens(Item, "=") of
[Command] ->
filter_flags(Rest, [Command | Commands]);
[KeyStr, Value] ->
[KeyStr, RawValue] ->
Key = list_to_atom(KeyStr),
Value = case Key of
verbose ->
list_to_integer(RawValue);
_ ->
RawValue
end,
rebar_config:set_global(Key, Value),
filter_flags(Rest, Commands);
Other ->
Expand Down
5 changes: 3 additions & 2 deletions src/rebar_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ set(Config, Key, Value) ->
set_global(jobs=Key, Value) when is_list(Value) ->
set_global(Key, list_to_integer(Value));
set_global(jobs=Key, Value) when is_integer(Value) ->
application:set_env(rebar_global, Key, erlang:max(1,Value));
application:set_env(rebar_global, Key, erlang:max(1, Value));
set_global(Key, Value) ->
application:set_env(rebar_global, Key, Value).

Expand All @@ -122,7 +122,8 @@ get_global(Key, Default) ->
end.

is_verbose() ->
get_global(verbose, "0") =:= "1".
DefaulLevel = rebar_log:default_level(),
get_global(verbose, DefaulLevel) > DefaulLevel.

get_jobs() ->
get_global(jobs, 3).
Expand Down
12 changes: 6 additions & 6 deletions src/rebar_ct.erl
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ run_test_if_present(TestDir, Config, File) ->
run_test(TestDir, Config, _File) ->
{Cmd, RawLog} = make_cmd(TestDir, Config),
clear_log(RawLog),
case rebar_config:get_global(verbose, "0") of
"0" ->
case rebar_config:is_verbose() of
false ->
Output = " >> " ++ RawLog ++ " 2>&1";
_ ->
true ->
Output = " 2>&1 | tee -a " ++ RawLog
end,

Expand Down Expand Up @@ -112,11 +112,11 @@ check_log(RawLog) ->
%% Show the log if it hasn't already been shown because verbose was on
show_log(RawLog) ->
?CONSOLE("Showing log\n", []),
case rebar_config:get_global(verbose, "0") of
"0" ->
case rebar_config:is_verbose() of
false ->
{ok, Contents} = file:read_file(RawLog),
?CONSOLE("~s", [Contents]);
_ ->
true ->
ok
end.

Expand Down
21 changes: 14 additions & 7 deletions src/rebar_log.erl
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,21 @@
-module(rebar_log).

-export([init/0,
set_level/1, get_level/0,
set_level/1, get_level/0, default_level/0,
log/3]).

%% ===================================================================
%% Public API
%% ===================================================================

init() ->
case rebar_config:get_global(verbose, "0") of
"1" ->
set_level(debug);
_ ->
set_level(error)
case valid_level(rebar_config:get_global(verbose, error_level())) of
0 -> set_level(error);
1 -> set_level(warn);
2 -> set_level(info);
3 -> set_level(debug)
end.


set_level(Level) ->
ok = application:set_env(rebar, log_level, Level).

Expand All @@ -63,10 +62,18 @@ log(Level, Str, Args) ->
ok
end.

default_level() -> error_level().

%% ===================================================================
%% 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(info, debug) -> false;
should_log(info, _) -> true;
Expand Down

0 comments on commit 015e582

Please sign in to comment.