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.

98 lines
3.5 KiB

9 years ago
9 years ago
9 years ago
  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. intensity/0,
  33. log/3,
  34. is_verbose/1]).
  35. -define(ERROR_LEVEL, 0).
  36. -define(WARN_LEVEL, 1).
  37. -define(INFO_LEVEL, 2).
  38. -define(DEBUG_LEVEL, 3).
  39. -define(DFLT_INTENSITY, low).
  40. %% ===================================================================
  41. %% Public API
  42. %% ===================================================================
  43. %% @doc Returns the color intensity, we first check the application envorinment
  44. %% if that is not set we check the environment variable REBAR_COLOR.
  45. intensity() ->
  46. case application:get_env(rebar, color_intensity) of
  47. undefined ->
  48. R = case os:getenv("REBAR_COLOR") of
  49. "high" ->
  50. high;
  51. "low" ->
  52. low;
  53. _ ->
  54. ?DFLT_INTENSITY
  55. end,
  56. application:set_env(rebar, color_intensity, R),
  57. R;
  58. {ok, Mode} ->
  59. Mode
  60. end.
  61. init(Caller, Verbosity) ->
  62. Level = case valid_level(Verbosity) of
  63. ?ERROR_LEVEL -> error;
  64. ?WARN_LEVEL -> warn;
  65. ?INFO_LEVEL -> info;
  66. ?DEBUG_LEVEL -> debug
  67. end,
  68. Intensity = intensity(),
  69. Log = ec_cmd_log:new(Level, Caller, Intensity),
  70. application:set_env(rebar, log, Log).
  71. set_level(Level) ->
  72. ok = application:set_env(rebar, log_level, Level).
  73. log(Level = error, Str, Args) ->
  74. {ok, LogState} = application:get_env(rebar, log),
  75. ec_cmd_log:Level(LogState, lists:flatten(cf:format("~!^~s~n", [Str])), Args);
  76. log(Level, Str, Args) ->
  77. {ok, LogState} = application:get_env(rebar, log),
  78. ec_cmd_log:Level(LogState, Str++"~n", Args).
  79. error_level() -> ?ERROR_LEVEL.
  80. default_level() -> ?INFO_LEVEL.
  81. is_verbose(State) ->
  82. rebar_state:get(State, is_verbose, false).
  83. %% ===================================================================
  84. %% Internal functions
  85. %% ===================================================================
  86. valid_level(Level) ->
  87. erlang:max(?ERROR_LEVEL, erlang:min(Level, ?DEBUG_LEVEL)).