Erlang如何查看gen_server内部的状态

来源:互联网 发布:电信网络诈骗 编辑:程序博客网 时间:2024/04/30 08:32

方法1、自写个函数,想gen_server发查看的消息。

方法2、利用系统现有的架构。sasl应用带了一个si的东西 全名是status inspector, 这个东西就是设计来帮用户解决这个问题的。

%%%-------------------------------------------------------------------%%% @author flybird <flybird@flybird>%%% @copyright (C) 2013, flybird%%% @doc%%%%%% @end%%% Created : 29 May 2013 by flybird <flybird@flybird>%%%--------------------------------------------------------------------module(wy).-behaviour(gen_server).%% API-export([start_link/0]).-export([test/0, stop/0, check_msg/0]).%% gen_server callbacks-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).-define(SERVER, ?MODULE). -record(state, {a, b}).test() ->    gen_server:call(?SERVER, {test, "fuck you!"}).stop() ->    gen_server:cast(?SERVER, stop).check_msg() ->    gen_server:call(?SERVER, check_msg).start_link() ->    gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).init([]) ->    {ok, #state{a = hello, b = world}}.handle_call({test, _} = Request, _From, State) ->    io:format("Got msg:~p~n", [Request]),    {reply, ok, State};handle_call(check_msg, _From, State)->  %方法1的实现    {reply, State, State};handle_call(_Request, _From, State) ->    Reply = ok,    {reply, Reply, State}.handle_cast(stop, State) ->    {stop, normal, State};handle_cast(_Msg, State) ->    {noreply, State}.handle_info(_Info, State) ->    {noreply, State}.terminate(_Reason, _State) ->    ok.code_change(_OldVsn, State, _Extra) ->    {ok, State}.


第二种方法,详情参考

http://blog.yufeng.info/archives/99