Is it possible to add an filter property for the erlang process?

litao cheng <>
Thu Jun 17 06:29:16 CEST 2010


hi, buddies.
my question is if it's possible to add an "filter" property for the erlang
process, so that process can drop the uninterested messages. then we can
reduce the needless memory consuming.

like this:
% spawn a process with filter : {foo, _}
PidA = spawn(?MODULE, do_something, [], [{filter, {foo, _}}]),

% the message {foo, hello} will be saved to PidA message queue, because it
match the filter
PidA ! {foo, hello},

% this message will be dropped
PidA ! {bar, world},

% reset the filter
process_flag(filter, [_|_]),

% this message will be stored in message queue
PidA ! "hello world",
ok.

by this feature,  I can write the code like this ( in my logging library,
which is similar with python logging module):
{ok, _} = logging:start_logger("logname"),
Logger = logging:get_logger("logname"),
Logger:set_level(?CRITICAL),
Logger:debug("log"),
Logger:warning("log"),
Logger:critical("log"),
ok

logger is an parameterized module, logging is an gen_server process, in
logging module:
handle_call({set_level, Level}, _From, State) ->
    process_flag(filter, #log_record{level = MsgLevel} when MsgLevel >
Level),
    {reply, ok, State};

in my logging library,  I have two methods to resolve this problem:
* dynamic compile the logger module, in the debug, info, warning, error
function check if the log allowed
* in logging gen_server process handl_xxx function, test if the log record
is allowed
all two solutions have flew.

I want to known if this process filter feature is valuable?
thanks


More information about the erlang-questions mailing list