`
hideto
  • 浏览: 2678210 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

gen_event例子:terminal_logger

阅读更多
定义三个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>erl
Eshell 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, []).
ok
3> gen_event:add_handler(error_man, terminal_logger2, []).
ok
4> gen_event:add_handler(error_man, terminal_logger3, []).
ok
5> 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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics