From 015e58201c87193a7ad6c30bb8b6ba3ab9c2c3e6 Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Tue, 17 Jan 2012 11:37:21 +0100 Subject: [PATCH] Support different log levels --- src/rebar.erl | 14 +++++++++++--- src/rebar_config.erl | 5 +++-- src/rebar_ct.erl | 12 ++++++------ src/rebar_log.erl | 21 ++++++++++++++------- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/rebar.erl b/src/rebar.erl index 5abf823..8d3359d 100644 --- a/src/rebar.erl +++ b/src/rebar.erl @@ -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 @@ -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"}, @@ -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 -> diff --git a/src/rebar_config.erl b/src/rebar_config.erl index 06396d8..2da3191 100644 --- a/src/rebar_config.erl +++ b/src/rebar_config.erl @@ -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). @@ -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). diff --git a/src/rebar_ct.erl b/src/rebar_ct.erl index 626144e..b3e77b0 100644 --- a/src/rebar_ct.erl +++ b/src/rebar_ct.erl @@ -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, @@ -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. diff --git a/src/rebar_log.erl b/src/rebar_log.erl index 54774d7..b7529a9 100644 --- a/src/rebar_log.erl +++ b/src/rebar_log.erl @@ -27,7 +27,7 @@ -module(rebar_log). -export([init/0, - set_level/1, get_level/0, + set_level/1, get_level/0, default_level/0, log/3]). %% =================================================================== @@ -35,14 +35,13 @@ %% =================================================================== 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). @@ -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;