Permalink
Browse files

Initial commit to this project

  • Loading branch information...
0 parents commit b2776edaaa020d72e07d03e06c5e9f454989ad86 Gianfranco committed Jul 5, 2011
@@ -0,0 +1,2 @@
+{cover_enabled,true}.
+{erl_opts,[debug_info]}.
@@ -0,0 +1,102 @@
+-module(audit_collector).
+-behaviour(gen_server).
+
+%% API
+-export([start_link/0,
+ stop/0]).
+-export([audit/2]).
+-export([review/2]).
+
+%% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+-record(state, {log :: list()
+ }).
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+-spec(stop() -> ok).
+stop() ->
+ gen_server:call(?MODULE,stop).
+
+-spec(audit(process,[send|'receive']) -> ok).
+audit(process,Options) ->
+ gen_server:call(?MODULE,{add_tracing_on,{process,Options}}).
+
+-spec(review(process,[send|'receive']) -> [tuple()]).
+review(process,Options) ->
+ gen_server:call(?MODULE,{review,{process,Options}}).
+
+%%%===================================================================
+%%% gen_server callbacks
+%%%===================================================================
+init([]) ->
+ {ok, #state{log = []
+ }}.
+
+handle_call(stop,_, State) ->
+ {stop,normal,ok,State};
+handle_call({add_tracing_on,{process,Options}},_,State) ->
+ OPFlags = option_to_flag(Options),
+ Flags = [timestamp,{tracer,self()}|OPFlags],
+ erlang:trace(new,true,Flags),
+ {reply,ok,State};
+handle_call({review,{process,Options}},_,State) ->
+ OPFlags = option_to_flag(Options),
+ io:format(user,"Audit Collector :: review, {process,OPFlags} ---> ~p~n",[OPFlags]),
+ io:format(user,"Audit Collector :: State log ---> ~p~n",[State#state.log]),
+ Reply = make_log(OPFlags,lists:reverse(State#state.log)),
+ io:format(user,"Reply == ~p~n",[Reply]),
+ {reply,Reply,State}.
+
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info(X, State) ->
+ io:format(user,"Audit Collector :: handle_info/2 -> ~p~n",[X]),
+ {noreply, State#state{log = [X|State#state.log]}}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================
+option_to_flag(X) ->
+ X.
+
+make_log([],_) -> [];
+make_log([send|R],History) ->
+ lists:foldl(
+ fun({trace_ts,P,send,Msg,To,_},Acc) when is_pid(P)->
+io:format(user,"Foldl SEND ~p~n",[{trace_ts,P,send,Msg,To}]),
+ case Msg of
+ {io_request,P,To,_} ->
+ Acc;
+ _ ->
+ Acc++[{send,P,Msg}]
+ end;
+ (_,Acc) -> Acc
+ end,[],History)++make_log(R,History);
+make_log(['receive'|R],History) ->
+ lists:foldl(
+ fun({trace_ts,P,'receive',Msg,_},Acc) ->
+io:format(user,"Foldl RECEIVE ~p~n",[{trace_ts,P,'receive',Msg}]),
+ case Msg of
+ {io_reply,P2,_} when is_pid(P2)->
+ Acc;
+ _ ->
+ Acc++[{'receive',P,Msg}]
+ end;
+ (_,Acc) -> Acc
+ end,[],History)++make_log(R,History).
+
@@ -0,0 +1,52 @@
+-module(audit_collector).
+-behaviour(gen_server).
+
+%% API
+-export([start_link/0,
+ stop/0]).
+
+%% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+-record(state, {}).
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+start_link() ->
+ gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
+
+stop() ->
+ gen_server:call(?MODULE,stop).
+
+audit(process,Options) ->
+ gen_server:call(?MODULE,{add_tracing_on,{process,Options}}).
+
+%%%===================================================================
+%%% gen_server callbacks
+%%%===================================================================
+init([]) ->
+ {ok, #state{}}.
+
+handle_call(stop, _From, State) ->
+ {stop,ok,State};
+handle_call({add_tracing_on,{process,Options}},_,State) ->
+ erlang:trace(new,true,[timestamp,{tracer,?MODULE},option_to_flag(Opt)||Opt<-Options]),
+
+
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+terminate(_Reason, _State) ->
+ ok.
+
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================
@@ -0,0 +1,12 @@
+{application, audit_erl,
+ [
+ {description, ""},
+ {vsn, "1"},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib
+ ]},
+ {mod, { audit_erl_app, []}},
+ {env, []}
+ ]}.
@@ -0,0 +1,16 @@
+-module(audit_erl_app).
+
+-behaviour(application).
+
+%% Application callbacks
+-export([start/2, stop/1]).
+
+%% ===================================================================
+%% Application callbacks
+%% ===================================================================
+
+start(_StartType, _StartArgs) ->
+ audit_erl_sup:start_link().
+
+stop(_State) ->
+ ok.
@@ -0,0 +1,28 @@
+
+-module(audit_erl_sup).
+
+-behaviour(supervisor).
+
+%% API
+-export([start_link/0]).
+
+%% Supervisor callbacks
+-export([init/1]).
+
+%% Helper macro for declaring children of supervisor
+-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).
+
+%% ===================================================================
+%% API functions
+%% ===================================================================
+
+start_link() ->
+ supervisor:start_link({local, ?MODULE}, ?MODULE, []).
+
+%% ===================================================================
+%% Supervisor callbacks
+%% ===================================================================
+
+init([]) ->
+ {ok, { {one_for_one, 5, 10}, []} }.
+
@@ -0,0 +1,21 @@
+-module(audit_collector_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+audit_collector_test_() ->
+ {foreach,
+ fun() -> audit_collector:start_link() end,
+ fun(P) -> audit_collector:stop() end,
+ [fun audit_collector_process/0
+ ]}.
+
+audit_collector_process() ->
+ audit_collector:audit(process,[send,'receive']),
+ Self = self(),
+ Sender = spawn_link(fun() -> Self ! {i_sent_this,self()} end),
+ Receiver = spawn_link(fun() -> receive die -> ok end end),
+ receive {i_sent_this,Sender} -> ok end,
+ Receiver ! die,
+ ?assertMatch(
+ [{send,Sender,{i_sent_this,Sent}},
+ {'receive',Receiver,die}],
+ audit_collector:review(process,[send,'receive'])).
@@ -0,0 +1,20 @@
+-module(audit_collector_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+audit_collector_test_() ->
+ {foreach,
+ fun() -> audit_collector:start_link() end,
+ fun(P) -> audit_collector:stop() end,
+ [fun audit_collector_process/0
+ ]}.
+
+audit_collector_process() ->
+ audit_collector:audit(process,[send,'receive']),
+ Self = self(),
+ Sender = spawn_link(fun() -> Self ! {i_sent_this,self()} end),
+ Receiver = spawn_link(fun() -> receive die -> ok end end),
+ Receiver ! die,
+ ?assertMatch(
+ [{_,Sender,{i_sent_this,Sent}},
+ {_,Receiver,die}],
+ audit_collector:audit_log(process,[send,'receive'])).

0 comments on commit b2776ed

Please sign in to comment.