4
%% Copyright Ericsson AB 2009-2010. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
20
%%% @doc Event handler module
22
%%% <p>This is an event handler module used for testing that
23
%%% Common Test generates events as expected.</p>
25
-module(ct_test_support_eh).
27
-behaviour(gen_event).
29
-include_lib("test_server/include/test_server.hrl").
30
-include_lib("common_test/include/ct_event.hrl").
32
%% gen_event callbacks
33
-export([init/1, handle_event/2, handle_call/2,
34
handle_info/2, terminate/2, code_change/3]).
36
-record(state, {cbm=ct_test_support,
39
%%====================================================================
40
%% gen_event callbacks
41
%%====================================================================
42
%%--------------------------------------------------------------------
43
%% Function: init(Args) -> {ok, State}
44
%% Description: Whenever a new event handler is added to an event manager,
45
%% this function is called to initialize the event handler.
46
%%--------------------------------------------------------------------
47
init(String = [X|_]) when is_integer(X) ->
48
case erl_scan:string(String++".") of
50
case erl_parse:parse_term(Ts) of
61
S1 = case lists:keysearch(cbm, 1, Args) of
67
S2 = case lists:keysearch(trace_level, 1, Args) of
68
{_,{trace_level,Level}} ->
69
S1#state{trace_level=Level};
73
print(S2#state.trace_level, "Event Handler ~w started with ~p~n",
77
%%--------------------------------------------------------------------
79
%% handle_event(Event, State) -> {ok, State} |
80
%% {swap_handler, Args1, State1, Mod2, Args2} |
82
%% Description:Whenever an event manager receives an event sent using
83
%% gen_event:notify/2 or gen_event:sync_notify/2, this function is called for
84
%% each installed event handler to handle the event.
85
%%--------------------------------------------------------------------
86
handle_event(Event, State=#state{cbm=CBM, trace_level=_Level}) ->
87
% print(_Level, "~p: ~p~n", [Event#event.name,Event#event.data]),
88
CBM:handle_event(?MODULE, Event),
91
%%--------------------------------------------------------------------
93
%% handle_call(Request, State) -> {ok, Reply, State} |
94
%% {swap_handler, Reply, Args1, State1,
96
%% {remove_handler, Reply}
97
%% Description: Whenever an event manager receives a request sent using
98
%% gen_event:call/3,4, this function is called for the specified event
99
%% handler to handle the request.
100
%%--------------------------------------------------------------------
101
handle_call(_Req, State) ->
105
%%--------------------------------------------------------------------
107
%% handle_info(Info, State) -> {ok, State} |
108
%% {swap_handler, Args1, State1, Mod2, Args2} |
110
%% Description: This function is called for each installed event handler when
111
%% an event manager receives any other message than an event or a synchronous
112
%% request (or a system message).
113
%%--------------------------------------------------------------------
114
handle_info(_Info, State) ->
117
%%--------------------------------------------------------------------
118
%% Function: terminate(Reason, State) -> void()
119
%% Description:Whenever an event handler is deleted from an event manager,
120
%% this function is called. It should be the opposite of Module:init/1 and
121
%% do any necessary cleaning up.
122
%%--------------------------------------------------------------------
123
terminate(_Reason, _State) ->
126
%%--------------------------------------------------------------------
127
%% Function: code_change(OldVsn, State, Extra) -> {ok, NewState}
128
%% Description: Convert process state when code is changed
129
%%--------------------------------------------------------------------
130
code_change(_OldVsn, State, _Extra) ->
133
%%--------------------------------------------------------------------
134
%%% Internal functions
135
%%--------------------------------------------------------------------
137
print(Level, _Str, _Args) ->
138
test_server:format(Level, _Str,_Args).