运行过程
$ erlc.exe -o ebin src/*.erl
$ erl -boot start_sasl -pa ebin
Erlang R16A (erts-5.10) [smp:4:4] [async-threads:10]
=PROGRESS REPORT==== 23-Feb-2013::22:36:39 ===
...
Eshell V5.10 (abort with ^G)
1> appmon:start().
=PROGRESS REPORT==== 23-Feb-2013::22:37:14 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.46.0>},
{name,timer_server},
{mfargs,{timer,start_link,[]}},
{restart_type,permanent},
{shutdown,1000},
{child_type,worker}]
{ok,<0.43.0>}
2> application:start(simple_cache).
=PROGRESS REPORT==== 23-Feb-2013::22:37:37 ===
supervisor: {local,sc_sup}
started: [{pid,<0.57.0>},
{name,sc_element_sup},
{mfargs,{sc_element_sup,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,supervisor}]
=PROGRESS REPORT==== 23-Feb-2013::22:37:37 ===
supervisor: {local,sc_sup}
started: [{pid,<0.58.0>},
{name,sc_event},
{mfargs,{sc_event,start_link,[]}},
{restart_type,permanent},
{shutdown,2000},
{child_type,worker}]
=PROGRESS REPORT==== 23-Feb-2013::22:37:37 ===
application: simple_cache
started_at: nonode@nohost
ok
3> simple_cache:insert("www.google.com",gurl).
=INFO REPORT==== 23-Feb-2013::22:39:20 ===
create([119,119,119,46,103,111,111,103,108,101,46,99,111,109], gurl)
ok
4> simple_cache:lookup("www.google.com").
=INFO REPORT==== 23-Feb-2013::22:39:45 ===
lookup([119,119,119,46,103,111,111,103,108,101,46,99,111,109])
{ok,gurl}
5> sc_event_logger:delete_handler().
ok
6> simple_cache:lookup("www.google.com").
{ok,gurl}
7> sc_event_logger:add_handler().
ok
8> simple_cache:lookup("www.google.com").
=INFO REPORT==== 23-Feb-2013::22:48:36 ===
lookup([119,119,119,46,103,111,111,103,108,101,46,99,111,109])
{ok,gurl}
9>
运行过程
启动过程
application:start(simple_cache). -> simple_cache.app(见p104表4-2中对mod参数的说明) -> sc_app.start/2(见p105对代码清单4-2的说明) -> sc_store:init/0 -> ets:new/2 sc_sup:start_link -> sc_sup:init/0(通过supervisor:start_link/0方法回调) -> sc_element_sup:start_link/0 -> sc_element:start_link/0 -> gen_server:start_link sc_event:start_link -> gen_event:start_link sc_event_logger:add_handler/0(将自己注册到gen_event容器中) -> sc_event:add_handler/2 -> gen_event:add_handler/3
插入新记录
simple_cache:insert/2
-> sc_event:create/2
-> gen_event:notify/2(利用初始化时定义的回调)
-> sc_event_logger:handle_event(create,...)
-> error_logger:info_msg("create(~w, ~w)~n", [Key, Value])(完成实际的打印日志动作)
删除处理器
sc_event_logger:delete_handler/0
-> sc_event:delete_handler/2
-> gen_event:delete_handler(取消自己在gen_evnet容器中的注册)