You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 line
2.6 KiB

  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. -module(rebar_log).
  28. -export([init/2,
  29. set_level/1,
  30. error_level/0,
  31. default_level/0,
  32. log/3,
  33. is_verbose/1]).
  34. -define(ERROR_LEVEL, 0).
  35. -define(WARN_LEVEL, 1).
  36. -define(INFO_LEVEL, 2).
  37. -define(DEBUG_LEVEL, 3).
  38. %% ===================================================================
  39. %% Public API
  40. %% ===================================================================
  41. init(Caller, Verbosity) ->
  42. Level = case valid_level(Verbosity) of
  43. ?ERROR_LEVEL -> error;
  44. ?WARN_LEVEL -> warn;
  45. ?INFO_LEVEL -> info;
  46. ?DEBUG_LEVEL -> debug
  47. end,
  48. Log = ec_cmd_log:new(Level, Caller),
  49. application:set_env(rebar, log, Log).
  50. set_level(Level) ->
  51. ok = application:set_env(rebar, log_level, Level).
  52. log(Level, Str, Args) ->
  53. {ok, LogState} = application:get_env(rebar, log),
  54. ec_cmd_log:Level(LogState, Str++"~n", Args).
  55. error_level() -> ?ERROR_LEVEL.
  56. default_level() -> ?INFO_LEVEL.
  57. is_verbose(State) ->
  58. rebar_state:get(State, is_verbose, false).
  59. %% ===================================================================
  60. %% Internal functions
  61. %% ===================================================================
  62. valid_level(Level) ->
  63. erlang:max(?ERROR_LEVEL, erlang:min(Level, ?DEBUG_LEVEL)).