action_selection.cpp revision 12855:588919e0e4aa
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  sc_report -- test error reporting, sepcifically which actions are selected
23
24  An important part of the sc_report functionality is to translate
25  an reported tupel (id,severity) into a set of actions. This test
26  is dedicated to this functionality. It uses user-defined actions and
27  user-defined handler.
28
29  Original Author: Ulli Holtmann, Synopsys, Inc., Jan 2003
30
31 *****************************************************************************/
32
33/*****************************************************************************
34
35  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
36  changes you are making here.
37
38      Name, Affiliation, Date:
39  Description of Modification:
40
41 *****************************************************************************/
42
43#include <systemc.h>
44
45
46/*
47  Allocate user-defined action:
48
49  Allocate as many user-defined actions as possible.
50  Note: We need to have at least usr1..usr6 action which are used in
51  subsequent tests.
52
53*/
54
55const unsigned num_usr_actions = 6;
56sc_actions usr_actions[num_usr_actions];
57
58sc_actions& usr1 = usr_actions[0];
59sc_actions& usr2 = usr_actions[1];
60sc_actions& usr3 = usr_actions[2];
61sc_actions& usr4 = usr_actions[3];
62sc_actions& usr5 = usr_actions[4];
63sc_actions& usr6 = usr_actions[5];
64
65const char* id1 = "ID1";
66const char* id2 = "ID2";
67const char* id3 = "ID3";
68const char* id4 = "ID4";
69
70
71void allocate_user_actions( )
72{
73    sc_actions usr;
74    for (unsigned int n=0; n<1000; n++) {
75	usr = sc_report_handler::get_new_action_id();
76	if ( usr == SC_UNSPECIFIED ) {
77	    cout << "We got " << n << " user-defined actions\n";
78	    break;
79	}
80	// make sure we don't get the same usr action again and that it
81	// is really new
82	sc_assert (usr!=SC_UNSPECIFIED && usr!=SC_DO_NOTHING && usr!=SC_THROW &&
83		usr!=SC_LOG         && usr!=SC_DISPLAY    && usr!=SC_CACHE_REPORT &&
84		usr!=SC_STOP        && usr!=SC_ABORT );
85	if ( n < num_usr_actions ) {
86	    // save for later use
87	    usr_actions[n] = usr;
88	    for (unsigned int i=0; i<n; i++) {
89		// lso check that is is new
90		sc_assert( usr!=usr_actions[i]);
91	    }
92	}
93    }
94}
95
96
97static const char* severity2str[] = {
98    "INFO", "WARNING", "ERROR", "FATAL", "UNKNOWN_SEVERITY"
99};
100
101// custom handler which is used to dump out reports and actions
102void dump_all_handler( const sc_report& report, const sc_actions& actions)
103{
104    // dump out report
105    cout << "report: " << report.get_msg_type()
106	 << " " << severity2str[ report.get_severity() ];
107    cout << " --> ";
108    for (int n=0; n<32; n++) {
109	sc_actions action = actions & 1<<n;
110	if (action) {
111	    cout << " ";
112	    switch(action) {
113	    case SC_UNSPECIFIED:  cout << "unspecified"; break;
114	    case SC_DO_NOTHING:   cout << "do-nothing"; break;
115	    case SC_THROW:        cout << "throw"; break;
116	    case SC_LOG:          cout << "log"; break;
117	    case SC_DISPLAY:      cout << "display"; break;
118	    case SC_CACHE_REPORT: cout << "cache-report"; break;
119	    case SC_INTERRUPT:    cout << "interrupt"; break;
120	    case SC_STOP:         cout << "stop"; break;
121	    case SC_ABORT:        cout << "abort"; break;
122	    default:
123		bool found=false;
124		for (unsigned int u=0; u<num_usr_actions; u++)
125		    if (action == usr_actions[u]) {
126			cout << "usr" << u+1;
127			found=true;
128			break;
129		    }
130		if (!found)
131		    cout << "UNKNOWN";
132	    }
133	}
134    }
135    cout << endl;
136    cout << " msg="      << report.get_msg()
137	 << " file="     << report.get_file_name()
138	 << " line "     << report.get_line_number()
139	 << " time="     << report.get_time();
140	 const char* name = report.get_process_name();
141    cout << " process=" << (name ? name : "<none>") << endl;
142}
143
144
145/*
146  Test selection schema  id x severity :
147
148            ID
149  Severity   1       2       3
150
151  info      usr2   usr1     usr5*
152  warning   usr3   usr1     usr3
153  error     usr4   usr1     usr1*
154  fatal     usr5   usr1     usr5
155
156  usr1..usr5 are user-defined actions
157
158  ID 1 selects by severity rule which has lowest priority,
159  ID 2 selects by ID rule,
160  ID 3 selects by  individual severity x ID rules (highest priority)
161
162*/
163void set_rules()
164{
165    // set rule 1: by severity
166    sc_report_handler::set_actions( SC_INFO,    usr2 );
167    sc_report_handler::set_actions( SC_WARNING, usr3 );
168    sc_report_handler::set_actions( SC_ERROR,   usr4 );
169    sc_report_handler::set_actions( SC_FATAL,   usr5 );
170
171    // set rule 2: by id
172    sc_report_handler::set_actions( id2, usr1 );
173
174    // set rule 3: by (id,severity)
175    sc_report_handler::set_actions ( id3, SC_INFO,  usr5 );
176    sc_report_handler::set_actions ( id3, SC_ERROR, usr1 );
177}
178void query_rules( const char* id )
179{
180    sc_report_handler::report( SC_INFO,    id, "extra_msg_for_info",   "no_specific_file", 0);
181    sc_report_handler::report( SC_WARNING, id, "extra_msg_for_warning","no_specific_file", 1);
182    sc_report_handler::report( SC_ERROR,   id, "extra_msg_for_error",  "no_specific_file", 2);
183    sc_report_handler::report( SC_FATAL,   id, "extra_msg_for_fatal",  "no_specific_file", 3);
184    cout << endl;
185}
186void query_rules()
187{
188    query_rules( id1 );
189    query_rules( id2 );
190    query_rules( id3 );
191}
192
193
194int sc_main(int,char**)
195{
196    allocate_user_actions();
197    sc_report_handler::set_handler( &dump_all_handler );
198
199    // disable automatic stop
200    sc_report_handler::stop_after( SC_ERROR, 0 );
201    sc_report_handler::stop_after( SC_FATAL, 0 );
202
203    // Don't emit error|fatal for ID4 because this would
204    // terminate the simulation.
205      cout << "Default settings for ID4\n";
206      query_rules( id4 );
207
208    // check default setting of rules
209    cout << "Specific settings for ID1..ID3\n";
210    set_rules();
211    query_rules();
212
213    // temporarily suppress usr4:
214    // - check which actions are emitted
215    // - check that suppress() restores old state
216    // - check return value of suppress(.)
217    cout << "temporarily suppress usr4\n";
218    sc_start( 1,SC_NS );
219    sc_report_handler::suppress( usr3 );
220    sc_assert( sc_report_handler::suppress( usr4 ) == usr3 );
221    query_rules( id1 );
222    sc_assert( sc_report_handler::suppress() == usr4 );
223    query_rules( id1 );
224
225    // temporarily force usr1: same checking as with suppress
226    cout << "temporarily force usr1\n";
227    sc_start( 1,SC_NS );
228    sc_report_handler::force( usr2 );
229    sc_assert( sc_report_handler::force( usr1 ) == usr2 );
230    query_rules( id1 );
231    sc_assert( sc_report_handler::force() == usr1 );
232    query_rules( id1 );
233
234    // temporarily force usr1: same checking as with suppress
235    cout << "temporarily suppress {usr3,usr4} and force {usr1,usr3}\n";
236    sc_start( 1,SC_NS );
237    sc_report_handler::force   ( usr1|usr3 );
238    sc_report_handler::suppress( usr3|usr4 );
239    query_rules( id1 );
240    sc_report_handler::force();
241    sc_report_handler::suppress();
242    query_rules( id1 );
243
244    return 0;
245}
246