log_file.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 log file 23 24 That that messages are printed into the log file. 25 26 Original Author: Ulli Holtmann, Synopsys, Inc., Jan 2003 27 28 *****************************************************************************/ 29 30/***************************************************************************** 31 32 MODIFICATION LOG - modifiers, enter your name, affiliation, date and 33 changes you are making here. 34 35 Name, Affiliation, Date: 36 Description of Modification: 37 38 *****************************************************************************/ 39 40#include <systemc.h> 41 42SC_MODULE( M ) 43{ 44 sc_event trigger_t, trigger_m; 45 SC_CTOR( M ) { 46 SC_THREAD( thread1 ); 47 sensitive << trigger_t; 48 dont_initialize(); 49 50 SC_METHOD( method1 ); 51 sensitive << trigger_m; 52 dont_initialize(); 53 } 54 void method1() { 55 sc_report_handler::report( SC_INFO, "ID1", "after log is opened", "method", 101); 56 sc_report_handler::report( SC_WARNING, "ID2", "after log is opened", "method", 102); 57 sc_report_handler::report( SC_ERROR, "ID1", "after log is opened", "method", 103); 58 sc_report_handler::report( SC_FATAL, "ID2", "after log is opened", "method", 104); 59 } 60 void thread1() { 61 while(1) { 62 sc_report_handler::report( SC_INFO, "ID2", "after log is opened", "thread", 201); 63 sc_report_handler::report( SC_WARNING, "ID1", "after log is opened", "thread", 202); 64 sc_report_handler::report( SC_ERROR, "ID2", "after log is opened", "thread", 203); 65 sc_report_handler::report( SC_FATAL, "ID1", "after log is opened", "thread", 204); 66 wait(); 67 } 68 } 69}; 70 71int sc_main(int,char**) 72{ 73 const char fname[] = "./sc_report.log"; 74 M uut("uut"); 75 76 // do not abort when on error, fatal 77 sc_report_handler::stop_after( SC_ERROR, 0 ); 78 sc_report_handler::stop_after( SC_FATAL, 0 ); 79 sc_report_handler::suppress( SC_STOP | SC_ABORT ); 80 sc_report_handler::set_actions( "ID1", SC_LOG | SC_DISPLAY ); 81 sc_report_handler::set_actions( "ID2", SC_LOG | SC_DISPLAY ); 82 83 // produces messages before the log file is opened 84 sc_report_handler::report( SC_INFO, "ID1", "before log is opened", "no_file", 0); 85 sc_report_handler::report( SC_WARNING, "ID1", "before log is opened", "no_file", 0); 86 87 // open log file 88 sc_report_handler::set_log_file_name( fname ); 89 sc_assert( strcmp(fname,sc_report_handler::get_log_file_name()) == 0 ); 90 91 // emit report from global context at t=0 92 sc_report_handler::report( SC_INFO, "ID1", "after log is opened", "file1", 1); 93 sc_report_handler::report( SC_WARNING, "ID2", "after log is opened", "file2", 2); 94 sc_report_handler::report( SC_ERROR, "ID1", "after log is opened", "file3", 3); 95 sc_report_handler::report( SC_FATAL, "ID2", "after log is opened", "file4", 4); 96 97 // emit report from global context at 10ns, method at 20ns, thread at 30ns 98 uut.trigger_m.notify( 20, SC_NS ); 99 uut.trigger_t.notify( 30, SC_NS ); 100 sc_start( 10,SC_NS ); 101 sc_report_handler::report( SC_ERROR, "ID1", "after log is opened", "file3", 5); 102 sc_start( 100, SC_NS ); 103 104 // close log file and more messages 105 sc_report_close_default_log(); 106 107 sc_report_handler::report( SC_INFO, "ID1", "after log is closed", "no_file", 0); 108 sc_report_handler::report( SC_WARNING, "ID1", "after log is closed", "no_file", 0); 109 110 // open file again and dump into cout so we cover it by regression 111 cout << "\n\nDump the logfile\n"; 112 FILE* log = fopen( fname,"r" ); 113 if ( !log ) { 114 SC_REPORT_FATAL( "Can not open the sc_report log file: ", fname ); 115 return 1; 116 } 117 while ( 1 ) { 118 int c = fgetc(log); 119 if (c==EOF) 120 break; 121 cout << static_cast<char>(c); 122 } 123 fclose( log) ; 124 125 return 0; 126} 127