Event tracer (ET) is an interesting thing in the Erlang class library. It can be used for event data collection and graphical display. it can collect event data for graphical display. I think this method is more intuitive for beginners. official documentation: http://www.erlang.org/doc/apps/et/et_intro.html
The following code quickly starts et. Let's take a look:
Eshell V5.9.1 (abort with ^G)1> {ok, ViewerPid} = et_viewer:start([{title,"Coffee Order"}]),CollectorPid = et_viewer:get_collector_pid(ViewerPid).<0.36.0>2> et_collector:report_event(CollectorPid,85,from,to,message,extra_stuff).{ok,{table_handle,<0.36.0>,16402,trace_ts, #Fun<et_collector.0.92094374>}}3>
Note:
{OK, viewerpid} = et_viewer: Start ([{Title, "coffee order"}]), % start the UI of et_view, and set the title displayed in the window to coffe order
Collectorpid = et_viewer: get_collector_pid (viewerpid). % create collector Process
Et_collector: report_event (collectorpid, 85, from, to, message, extra_stuff). % et_collector: report_event (collector PID, 85, from, to, message, extra_stuff ).
The above 85 represents the details of the information, the value range is 1 ~ 100. You can use this value for simple message filtering;
The following is a demo using a module at hand:
-Module (kv_view ). -behaviour (gen_server ). -Export ([start/0, start_link/0]). -Export ([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). % exit (whereis (Kv), kill ). %-define (server ,? Module). Start ()-> gen_server: Start ({local ,? Server },? Module, [], []). start_link ()-> gen_server: start_link ({local ,? Server },? Module, [], []). % ------ callbacksinit ([])-> process_flag (trap_exit, true), r = ETS: New (ets_kv, [named_table, {keypos, 1}, set]), {OK, viewer} = et_viewer: Start ([{Title, "K-V View" },{ max_actors, 10}]), c = et_viewer: get_collector_pid (viewer), et_collector: report_event (C, 90, Shell, k_v_server, init, [R]), {OK, [{collector, c}]}. handle_call ({lookup, key}, from, [{collector, c}] = S)-> result = ETS: Lookup (ets_kv, key ), Et_collector: report_event (C, 80, from, k_v_server, lookup, [Result]), {reply, result, s}; handle_call (MC, _ from, {collector, c} = S)-> Timer: Sleep (8000), et_collector: report_event (C, 80, Shell, k_v_server, handle_call_but_late, [{sleep, 8000}]), {reply, {OK, but_late}, s}; handle_call (_ REQ, _ from, [{collector, c}] = S)-> {reply, OK, s }. handle_cast (STOP, S)-> {stop, normal, s}; handle_cast ({Add, K, V}, [{collector, C}] = S)-> IO: Format ("~ P ~ N ", [s]), ETS: insert (ets_kv, {k, V}), et_collector: report_event (C, 70, Shell, k_v_server, handle_call_but_late, [{key, k}, {value, V}]), {noreply, s}; handle_cast (MSG, {collector, c} = S)-> et_collector: report_event (C, 60, shell, k_v_server, handle_cast, [{message, MSG}]), {noreply, s }. handle_info (die, S)-> exit (kv_die); % {noreply, S); handle_info (Info, [{collector, c}] = S)-> et_collector: report_event (C, 20, Shell, k_v_serve R, handle_info, [{message, info}]), {noreply, s }. % if the process is in the supervisor, if it is killed by the supervisor, gen_server will call the callback function terminate (shutdown, state) % terminate (reason, _ s)-> IO: format ("~ P terminate by reason :~ P ~ N ", [self (), reason]), OK. code_change (_ oldvsn, S, _ extra)-> {OK, s }.
Call:
Eshell V5.9 (abort with ^G)1> kv_view:start().{ok,<0.32.0>}2> gen_server:cast(kv_view,{add,name,"zen"}).[{collector,<0.37.0>}]ok3> gen_server:call(kv_view,{lookup,name}).[{name,"zen"}]4> gen_server:call(kv_view,{lookup,name}).[{name,"zen"}]5> gen_server:call(kv_view,{lookup,name}).[{name,"zen"}]6> whereis(kv_view)!hello_world.hello_world7> whereis(kv_view)!hello_world.hello_world8> exit(whereis(kv_view)).** exception exit: <0.32.0>9>
After such a call, the GUI synchronously draws the following figure: