catch_actions.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  catch_actions.cpp -- Test for catch actions used with a custom report handler
23
24  Original Author: Philipp A. Hartmann, Intel, 2017-01-22
25
26 *****************************************************************************/
27
28#include <systemc>
29
30using std::cout;
31using std::endl;
32using sc_core::sc_actions;
33using sc_core::SC_DISPLAY;
34using sc_core::SC_ERROR;
35using sc_core::SC_LOG;
36using sc_core::SC_THROW;
37using sc_core::sc_report;
38using sc_core::sc_report_handler;
39
40/* anonymous */ namespace {
41bool last_report_thrown = false;
42void custom_handler(const sc_report& rep, const sc_actions& actions)
43{
44    if ( actions & SC_DISPLAY ) {
45        cout << endl
46             // if last_report_thrown is set, we're in a catch action
47             << (last_report_thrown ? "[caught] " : "[normal] ")
48             << sc_core::sc_report_compose_message(rep)
49             << endl;
50    }
51
52    // only log errors from catch actions
53    if (rep.get_severity() == SC_ERROR) {
54        if (actions & SC_LOG) {
55            sc_assert( !(actions & SC_THROW) );
56        } else {
57            sc_assert( actions & SC_THROW );
58        }
59    }
60
61    // cache SC_THROW state of current report
62    last_report_thrown = (actions & SC_THROW);
63
64    // delegate other actions to default handler
65    sc_report_handler::default_handler(rep, actions & ~SC_DISPLAY);
66
67} /* custom_handler */
68} /* anonymous namespace */
69
70int sc_main(int,char*[])
71{
72    // extended report logging, currently not checked in verify.pl
73    sc_report_handler::set_log_file_name("catch_actions.ext.log");
74
75    sc_report_handler::set_handler(custom_handler);
76    SC_REPORT_INFO("catch_actions", "prepared custom handler");
77    sc_assert(last_report_thrown == false);
78
79    SC_REPORT_INFO("catch_actions", "preparing catch actions");
80    sc_actions act = sc_report_handler::set_catch_actions(SC_DISPLAY | SC_LOG);
81    sc_assert(act == SC_DISPLAY);
82    sc_assert(sc_report_handler::get_catch_actions() == (SC_DISPLAY | SC_LOG));
83
84    SC_REPORT_INFO("catch_actions", "only log errors from catch actions");
85    act = sc_report_handler::set_actions
86      (SC_ERROR, (sc_core::SC_DEFAULT_ERROR_ACTIONS & ~SC_LOG));
87
88    // real test
89    SC_REPORT_ERROR("catch_actions", "throwing an exception");
90
91    // not reached
92    return 0;
93}
94