|
定义三个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> |
|