六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 46|回复: 0

gen_event例子:terminal_logger

[复制链接]

升级  84.33%

137

主题

137

主题

137

主题

举人

Rank: 3Rank: 3

积分
453
 楼主| 发表于 2013-1-27 05:07:12 | 显示全部楼层 |阅读模式
定义三个terminal_logger:
$$ terminal_logger1.erl
-module(terminal_logger1).-behaviour(gen_event).-export([init/1, handle_event/2, handle_call/2, handle_info/2, code_change/3, terminate/2]).init(_Args) ->  {ok, []}.handle_event(ErrorMsg, State) ->  io:format("*** Error1 *** ~p~n", [ErrorMsg]),  {ok, State}.handle_call(_Request, _State) ->  {ok, [], []}.handle_info(_Info, _State) ->  {ok, []}.code_change(_OlvVsn, _State, _Extra) ->  ok.terminate(_Args, _State) ->  ok.
$$ terminal_logger2.erl
-module(terminal_logger2).-behaviour(gen_event).-export([init/1, handle_event/2, handle_call/2, handle_info/2, code_change/3, terminate/2]).init(_Args) ->  {ok, []}.handle_event(ErrorMsg, State) ->  io:format("*** Error2 *** ~p~n", [ErrorMsg]),  {ok, State}.handle_call(_Request, _State) ->  {ok, [], []}.handle_info(_Info, _State) ->  {ok, []}.code_change(_OlvVsn, _State, _Extra) ->  ok.terminate(_Args, _State) ->  ok.
$$ terminal_logger3.erl
-module(terminal_logger3).-behaviour(gen_event).-export([init/1, handle_event/2, handle_call/2, handle_info/2, code_change/3, terminate/2]).init(_Args) ->  {ok, []}.handle_event(ErrorMsg, State) ->  io:format("*** Error3 *** ~p~n", [ErrorMsg]),  {ok, State}.handle_call(_Request, _State) ->  {ok, [], []}.handle_info(_Info, _State) ->  {ok, []}.code_change(_OlvVsn, _State, _Extra) ->  ok.terminate(_Args, _State) ->  ok.

编译:
Eshell> c(terminal_logger1).Eshell> c(terminal_logger2).Eshell> c(terminal_logger3).

运行:
D:\erl\code>erlEshell V5.6.3  (abort with ^G)1> gen_event:start_link({local, error_man}).{ok,<0.31.0>}2> gen_event:add_handler(error_man, terminal_logger1, []).ok3> gen_event:add_handler(error_man, terminal_logger2, []).ok4> gen_event:add_handler(error_man, terminal_logger3, []).ok5> gen_event:which_handlers(error_man).[terminal_logger3,terminal_logger2,terminal_logger1]6> gen_event:notify(error_man, "Hideto").*** Error3 *** "Hideto"ok*** Error2 *** "Hideto"7> *** Error1 *** "Hideto"7>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表