2006-12-04 17:07:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_loglevel.erl
|
|
|
|
%%% Author : Mickael Remond <mremond@process-one.net>
|
|
|
|
%%% Purpose : Loglevel switcher.
|
|
|
|
%%% Be careful: you should not have any ejabberd_logger module
|
|
|
|
%%% as ejabberd_loglevel switcher is compiling and loading
|
|
|
|
%%% dynamically a "virtual" ejabberd_logger module (Described
|
|
|
|
%%% in a string at the end of this module).
|
|
|
|
%%% Created : 29 Nov 2006 by Mickael Remond <mremond@process-one.net>
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2010-01-12 17:15:16 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2010 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2006-12-04 17:07:44 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
2007-12-24 12:41:41 +01:00
|
|
|
|
2006-12-04 17:07:44 +01:00
|
|
|
-module(ejabberd_loglevel).
|
|
|
|
-author('mickael.remond@process-one.net').
|
|
|
|
|
2010-10-13 14:04:48 +02:00
|
|
|
-export([set/1,
|
|
|
|
get/0,
|
2010-10-15 11:19:08 +02:00
|
|
|
get_default/0,
|
2010-10-13 14:04:48 +02:00
|
|
|
set_custom/2,
|
|
|
|
clear_custom/0,
|
|
|
|
clear_custom/1]).
|
2006-12-04 17:07:44 +01:00
|
|
|
|
2008-03-21 15:44:16 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2006-12-04 17:07:44 +01:00
|
|
|
-define(LOGMODULE, "error_logger").
|
|
|
|
|
|
|
|
%% Error levels:
|
2010-10-12 14:41:21 +02:00
|
|
|
-record(loglevel, {ordinal,
|
|
|
|
name,
|
2010-10-12 15:30:18 +02:00
|
|
|
description,
|
|
|
|
function = no_log,
|
|
|
|
event_type = no_log,
|
|
|
|
msg_prefix = no_log}).
|
2010-10-12 14:41:21 +02:00
|
|
|
|
|
|
|
-define(LOG_LEVELS,
|
|
|
|
[#loglevel{ordinal = 0, name = no_log, description = "No log"},
|
2010-10-12 15:30:18 +02:00
|
|
|
#loglevel{ordinal = 1, name = critical, description = "Critical",
|
|
|
|
function = critical_msg, event_type = error, msg_prefix = "C"},
|
|
|
|
#loglevel{ordinal = 2, name = error, description = "Error",
|
|
|
|
function = error_msg, event_type = error, msg_prefix = "E"},
|
|
|
|
#loglevel{ordinal = 3, name = warning, description = "Warning",
|
2010-10-12 16:00:44 +02:00
|
|
|
function = warning_msg, event_type = warning_msg, msg_prefix = "W"},
|
2010-10-12 15:30:18 +02:00
|
|
|
#loglevel{ordinal = 4, name = info, description = "Info",
|
|
|
|
function = info_msg, event_type = info_msg, msg_prefix = "I"},
|
|
|
|
#loglevel{ordinal = 5, name = debug, description = "Debug",
|
|
|
|
function = debug_msg, event_type = info_msg, msg_prefix = "D"}]).
|
2009-05-06 16:51:51 +02:00
|
|
|
|
2010-10-15 11:31:18 +02:00
|
|
|
%% @type level() = integer() | atom().
|
|
|
|
|
2010-10-28 18:23:02 +02:00
|
|
|
%% @spec () -> {DefaultLevelOrdinal::integer(), [{Module::atom(), LevelOrdinal::integer()}]}
|
2010-10-15 11:31:18 +02:00
|
|
|
%% @doc Get the default and all custom levels
|
2009-08-06 12:56:55 +02:00
|
|
|
get() ->
|
2010-10-15 11:19:08 +02:00
|
|
|
ejabberd_logger:get().
|
|
|
|
|
2010-10-15 11:31:18 +02:00
|
|
|
%% @spec () -> {Ordinal::integer(), Name::atom(), Description::string()}
|
|
|
|
%% @doc Get the default level
|
2010-10-15 11:19:08 +02:00
|
|
|
get_default() ->
|
2010-10-13 12:52:56 +02:00
|
|
|
{DefaultLevel, _CustomLevels} = ejabberd_logger:get(),
|
|
|
|
case lists:keysearch(DefaultLevel, #loglevel.ordinal, ?LOG_LEVELS) of
|
2010-10-12 14:41:21 +02:00
|
|
|
{value, Result = #loglevel{}} ->
|
|
|
|
{Result#loglevel.ordinal, Result#loglevel.name, Result#loglevel.description};
|
|
|
|
_ ->
|
2010-10-13 12:52:56 +02:00
|
|
|
erlang:error({no_such_loglevel, DefaultLevel})
|
2009-08-06 12:56:55 +02:00
|
|
|
end.
|
|
|
|
|
2010-10-28 18:23:02 +02:00
|
|
|
%% @spec (DefaultLevel::level() | {DefaultLevel::level(), [{Module::atom(), Level::level()}]}) ->
|
2010-10-15 11:31:18 +02:00
|
|
|
%% {module, ejabberd_logger}
|
|
|
|
%% @doc Set the default and all custom levels
|
2010-10-13 12:52:56 +02:00
|
|
|
set(DefaultLevel) when is_atom(DefaultLevel) orelse is_integer(DefaultLevel) ->
|
|
|
|
set({DefaultLevel, []});
|
|
|
|
set({DefaultLevel, CustomLevels}) when is_list(CustomLevels) ->
|
|
|
|
DefaultInt = level_to_integer(DefaultLevel),
|
|
|
|
CustomInts = [level_to_integer(C) || C <- CustomLevels],
|
|
|
|
Loglevel = {DefaultInt, CustomInts},
|
2009-05-06 16:51:51 +02:00
|
|
|
try
|
|
|
|
{Mod,Code} = dynamic_compile:from_string(ejabberd_logger_src(Loglevel)),
|
|
|
|
code:load_binary(Mod, ?LOGMODULE ++ ".erl", Code)
|
|
|
|
catch
|
|
|
|
Type:Error -> ?CRITICAL_MSG("Error compiling logger (~p): ~p~n", [Type, Error])
|
|
|
|
end;
|
2006-12-04 17:07:44 +01:00
|
|
|
set(_) ->
|
2010-10-13 12:52:56 +02:00
|
|
|
exit("Invalid loglevel format").
|
2006-12-04 17:07:44 +01:00
|
|
|
|
2010-10-28 18:23:02 +02:00
|
|
|
%% @spec (Module::atom(), CustomLevel::level()) -> ok
|
2010-10-15 11:31:18 +02:00
|
|
|
%% @doc Set a custom level
|
2010-10-13 14:04:48 +02:00
|
|
|
set_custom(Module, Level) ->
|
|
|
|
{DefaultLevel, CustomLevels} = ejabberd_logger:get(),
|
|
|
|
case lists:keysearch(Module, 1, CustomLevels) of
|
|
|
|
{value, {Module, Level}} ->
|
|
|
|
ok;
|
|
|
|
{value, _} ->
|
|
|
|
set({DefaultLevel, lists:keyreplace(Module, 1, CustomLevels, {Module, Level})});
|
|
|
|
_ ->
|
|
|
|
set({DefaultLevel, [{Module, Level} | CustomLevels]})
|
|
|
|
end.
|
|
|
|
|
2010-10-15 11:31:18 +02:00
|
|
|
%% @spec () -> ok
|
|
|
|
%% @doc Clear all custom levels
|
2010-10-13 14:04:48 +02:00
|
|
|
clear_custom() ->
|
2010-10-14 11:19:41 +02:00
|
|
|
{DefaultLevel, _CustomLevels} = ejabberd_logger:get(),
|
2010-10-13 14:04:48 +02:00
|
|
|
set({DefaultLevel, []}).
|
|
|
|
|
2010-10-15 11:31:18 +02:00
|
|
|
%% @spec (Module::atom()) -> ok
|
|
|
|
%% @doc Clear a custom level
|
2010-10-13 14:04:48 +02:00
|
|
|
clear_custom(Module) ->
|
|
|
|
{DefaultLevel, CustomLevels} = ejabberd_logger:get(),
|
|
|
|
case lists:keysearch(Module, 1, CustomLevels) of
|
|
|
|
{value, _} ->
|
|
|
|
set({DefaultLevel, lists:keydelete(Module, 1, CustomLevels)});
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2010-10-13 12:52:56 +02:00
|
|
|
level_to_integer(Level) when is_integer(Level) ->
|
|
|
|
Level;
|
|
|
|
level_to_integer({Module, Level}) ->
|
|
|
|
{Module, level_to_integer(Level)};
|
2009-05-06 16:51:51 +02:00
|
|
|
level_to_integer(Level) ->
|
2010-10-12 14:41:21 +02:00
|
|
|
case lists:keysearch(Level, #loglevel.name, ?LOG_LEVELS) of
|
|
|
|
{value, #loglevel{ordinal = Int}} -> Int;
|
2009-05-06 16:51:51 +02:00
|
|
|
_ -> erlang:error({no_such_loglevel, Level})
|
2006-12-04 17:07:44 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
%% --------------------------------------------------------------
|
|
|
|
%% Code of the ejabberd logger, dynamically compiled and loaded
|
|
|
|
%% This allows to dynamically change log level while keeping a
|
2009-08-06 12:56:55 +02:00
|
|
|
%% very efficient code.
|
2006-12-04 17:07:44 +01:00
|
|
|
ejabberd_logger_src(Loglevel) ->
|
2010-10-12 15:30:18 +02:00
|
|
|
lists:flatten([header_src(),
|
|
|
|
get_src(Loglevel),
|
|
|
|
[log_src(Loglevel, LevelSpec) || LevelSpec <- ?LOG_LEVELS],
|
|
|
|
notify_src()]).
|
|
|
|
|
|
|
|
header_src() ->
|
2006-12-04 17:07:44 +01:00
|
|
|
"-module(ejabberd_logger).
|
|
|
|
-author('mickael.remond@process-one.net').
|
|
|
|
|
|
|
|
-export([debug_msg/4,
|
|
|
|
info_msg/4,
|
|
|
|
warning_msg/4,
|
|
|
|
error_msg/4,
|
2009-08-06 12:56:55 +02:00
|
|
|
critical_msg/4,
|
|
|
|
get/0]).
|
2010-10-12 15:30:18 +02:00
|
|
|
".
|
2009-08-06 12:56:55 +02:00
|
|
|
|
2010-10-12 15:30:18 +02:00
|
|
|
get_src(Loglevel) ->
|
2010-10-13 12:52:56 +02:00
|
|
|
io_lib:format("get() -> ~w.
|
|
|
|
", [Loglevel]).
|
2010-10-12 15:30:18 +02:00
|
|
|
|
|
|
|
log_src(_Loglevel, #loglevel{function = no_log}) ->
|
|
|
|
[];
|
2010-10-13 12:52:56 +02:00
|
|
|
log_src({DefaultLevel, [{Module, Level} | Tail]}, Spec = #loglevel{ordinal = MinLevel})
|
|
|
|
when Level < MinLevel andalso MinLevel =< DefaultLevel ->
|
|
|
|
[atom_to_list(Spec#loglevel.function), "(", atom_to_list(Module), ", _, _, _) -> ok;
|
|
|
|
", log_src({DefaultLevel, Tail}, Spec)];
|
|
|
|
log_src({DefaultLevel, [{Module, Level} | Tail]}, Spec = #loglevel{ordinal = MinLevel})
|
|
|
|
when DefaultLevel < MinLevel andalso MinLevel =< Level ->
|
|
|
|
[atom_to_list(Spec#loglevel.function), "(", atom_to_list(Module), " = Module, Line, Format, Args) ->",
|
|
|
|
log_notify_src(Spec), ";
|
|
|
|
", log_src({DefaultLevel, Tail}, Spec)];
|
|
|
|
log_src({DefaultLevel, [_Head | Tail]}, Spec = #loglevel{}) ->
|
|
|
|
log_src({DefaultLevel, Tail}, Spec);
|
|
|
|
log_src({DefaultLevel, []}, Spec = #loglevel{ordinal = MinLevel})
|
|
|
|
when DefaultLevel < MinLevel ->
|
2010-10-12 15:30:18 +02:00
|
|
|
[atom_to_list(Spec#loglevel.function), "(_, _, _, _) -> ok.
|
|
|
|
"];
|
2010-10-13 12:52:56 +02:00
|
|
|
log_src({_DefaultLevel, []}, Spec = #loglevel{}) ->
|
|
|
|
[atom_to_list(Spec#loglevel.function), "(Module, Line, Format, Args) ->",
|
|
|
|
log_notify_src(Spec), ".
|
|
|
|
"].
|
|
|
|
|
|
|
|
log_notify_src(Spec = #loglevel{}) ->
|
|
|
|
["notify(", atom_to_list(Spec#loglevel.event_type), ",
|
|
|
|
\"", Spec#loglevel.msg_prefix, "(~p:~p:~p) : \"++Format++\"~n\",
|
|
|
|
[self(), Module, Line | Args])"].
|
2010-10-12 15:30:18 +02:00
|
|
|
|
|
|
|
notify_src() ->
|
2006-12-04 17:07:44 +01:00
|
|
|
%% Distribute the message to the Erlang error logger
|
2010-10-12 15:30:18 +02:00
|
|
|
"notify(Type, Format, Args) ->
|
2006-12-04 17:07:44 +01:00
|
|
|
LoggerMsg = {Type, group_leader(), {self(), Format, Args}},
|
|
|
|
gen_event:notify(error_logger, LoggerMsg).
|
|
|
|
".
|