PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

/ucengine/src/core/uce_log.erl

http://github.com/AF83/ucengine
Erlang | 64 lines | 35 code | 12 blank | 17 comment | 0 complexity | 605df99013dc1259b5741277376e0c19 MD5 | raw file
 1%%
 2%%  U.C.Engine - Unified Collaboration Engine
 3%%  Copyright (C) 2011 af83
 4%%
 5%%  This program is free software: you can redistribute it and/or modify
 6%%  it under the terms of the GNU Affero General Public License as published by
 7%%  the Free Software Foundation, either version 3 of the License, or
 8%%  (at your option) any later version.
 9%%
10%%  This program is distributed in the hope that it will be useful,
11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13%%  GNU Affero General Public License for more details.
14%%
15%%  You should have received a copy of the GNU Affero General Public License
16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17%%
18-module(uce_log).
19
20-export([debug/3, info/3, warning/3, error/3, critical/3]).
21
22debug(Format, ML, Args) ->
23    log(debug, info_msg, "DEBUG: ~p:~p: ", Format, ML, Args).
24
25info(Format, ML, Args) ->
26    log(info, info_msg, "~p:~p: ", Format, ML, Args).
27
28warning(Format, ML, Args) ->
29    log(warning, warning_msg, "~p:~p: ", Format, ML, Args).
30
31error(Format, ML, Args) ->
32    log(error, error_msg, "~p:~p: ", Format, ML, Args).
33
34critical(Format, ML, Args) ->
35    log(critical, critical_msg, "~p:~p: ", Format, ML, Args).
36
37current_level(debug) ->
38    1;
39current_level(info) ->
40    2;
41current_level(warning) ->
42    3;
43current_level(error) ->
44    4;
45current_level(critical) ->
46    5.
47
48log(Level, Fun, Pre, Format, ML, Args) ->
49    log(current_level(Level), current_level(config:get(log_level)), Fun, Pre, Format, ML, Args).
50
51log(Level, ConfigLevel, Fun, Pre, Format, [Module, Line], Args) when Level >= ConfigLevel ->
52    error_logger:Fun(Pre ++ Format, [Module, Line] ++ Args);
53log(_Level, _Configlevel, _Fun, _Pre, _Format, [_Module, _Line], _Args) ->
54    ok.
55
56-ifdef(TEST).
57-include_lib("eunit/include/eunit.hrl").
58
59current_level_test() ->
60    lists:foreach(fun({Level, Expected}) ->
61                          ?assertEqual(Expected, current_level(Level))
62                  end, [{debug, 1}, {info, 2}, {warning, 3}, {error, 4}, {critical, 5}]).
63
64-endif.