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.

83 lines
2.9 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/1,
  29. set_level/1, default_level/0,
  30. log/3]).
  31. %% ===================================================================
  32. %% Public API
  33. %% ===================================================================
  34. init(Config) ->
  35. Verbosity = rebar_config:get_global(Config, verbose, default_level()),
  36. case valid_level(Verbosity) of
  37. 0 -> set_level(error);
  38. 1 -> set_level(warn);
  39. 2 -> set_level(info);
  40. 3 -> set_level(debug)
  41. end.
  42. set_level(Level) ->
  43. ok = application:set_env(rebar, log_level, Level).
  44. log(Level, Str, Args) ->
  45. {ok, LogLevel} = application:get_env(rebar, log_level),
  46. case should_log(LogLevel, Level) of
  47. true ->
  48. io:format(log_prefix(Level) ++ Str, Args);
  49. false ->
  50. ok
  51. end.
  52. default_level() -> error_level().
  53. %% ===================================================================
  54. %% Internal functions
  55. %% ===================================================================
  56. valid_level(Level) ->
  57. erlang:max(error_level(), erlang:min(Level, debug_level())).
  58. error_level() -> 0.
  59. debug_level() -> 3.
  60. should_log(debug, _) -> true;
  61. should_log(info, debug) -> false;
  62. should_log(info, _) -> true;
  63. should_log(warn, debug) -> false;
  64. should_log(warn, info) -> false;
  65. should_log(warn, _) -> true;
  66. should_log(error, error) -> true;
  67. should_log(error, _) -> false;
  68. should_log(_, _) -> false.
  69. log_prefix(debug) -> "DEBUG: ";
  70. log_prefix(info) -> "INFO: ";
  71. log_prefix(warn) -> "WARN: ";
  72. log_prefix(error) -> "ERROR: ".