00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "scv/scv_config.h"
00044
00045 #include <time.h>
00046 #include <string.h>
00047
00048 #include "scv/scv_util.h"
00049 #include "scv/scv_debug.h"
00050 #include "scv/scv_constraint.h"
00051 #include "scv/scv_report.h"
00052 #include "scv/scv_tr.h"
00053
00054 #define SCV_DEBUG_DEFAULT_TRACE_FILE "scv_debug.log"
00055
00056 #ifdef _MSC_VER
00057 #define strcasecmp stricmp
00058 #else
00059 extern "C" int strcasecmp(const char *, const char *);
00060 #endif
00061
00062 int scv_debug::_debug = -1;
00063 scv_debug *scv_debug::scv_debug_p = 0;
00064 int scv_debug::local_origination = false;
00065
00066
00067 const char *scv_debug::facility_names[] = {
00068 "all",
00069 "data_structures",
00070 "introspection",
00071 "messages",
00072 "randomization",
00073 "recording",
00074 "signals",
00075 "transactors",
00076 "last"
00077 };
00078
00079 const char *scv_debug::_kind = "scv_debug";
00080
00081
00082 static bool valid_facility(scv_debug::debug_facilities facility);
00083 static bool valid_level(int level);
00084
00085
00086 scv_debug::scv_debug(const char *filename)
00087 {
00088 int i;
00089 char localString1[1000];
00090 time_t t;
00091
00092 if (filename == 0) {
00093 file_p = fopen(SCV_DEBUG_DEFAULT_TRACE_FILE, "w");
00094 } else {
00095 file_p = fopen(filename, "w");
00096 }
00097
00098 send_to_stdout = 1;
00099 for (i = 0; i < LAST; i++) {
00100 facility_levels[i] = INITIAL_DEBUG_LEVEL;
00101 if (i != ALL) {
00102 set_level_for_classes(i, INITIAL_DEBUG_LEVEL);
00103 }
00104 }
00105
00106 t = time(0);
00107 sprintf(localString1, "SCV Trace started at %s\n",
00108 ctime(&t));
00109
00110 send_to_log(localString1);
00111 }
00112
00113 scv_debug::~scv_debug()
00114 { close_trace_file(); }
00115
00116 void scv_debug::record_data(
00117 const char *filename,
00118 int lineno,
00119 scv_object_if *object_p,
00120 int target_level,
00121 const char *data
00122 )
00123 {
00124 char localString[1000];
00125
00126 if (scv_debug_p == 0) return;
00127
00128 if (target_level == 0) {
00129 sprintf(
00130 localString,
00131 "scv_debug: Class \"%s\", in File \"%s\", Line %d:\n",
00132 object_p->kind(), filename, lineno
00133 );
00134 }
00135 else {
00136 sprintf(
00137 localString,
00138 "scv_debug: Class \"%s\", Level %d, in File \"%s\", Line %d:\n",
00139 object_p->kind(), target_level, filename, lineno
00140 );
00141 }
00142 scv_debug_p->send_to_log(localString);
00143
00144 sprintf(localString, " \"%s\"\n", data);
00145 scv_debug_p->send_to_log(localString);
00146 }
00147
00148 int scv_debug::check(int actual_level, int target_level)
00149 {
00150 if (target_level <= SUSPENDED_DEBUG_LEVEL) return 0;
00151 if (actual_level <= SUSPENDED_DEBUG_LEVEL) return 0;
00152 if (actual_level == 0) return 1;
00153 return actual_level >= target_level;
00154 }
00155
00156 void scv_debug::send_to_log(const char *the_string)
00157 {
00158 if (file_p != 0) fprintf(file_p, "%s", the_string);
00159 if (send_to_stdout == 1) scv_out << the_string;
00160 }
00161
00162 void scv_debug::open_trace_file(const char *filename)
00163 {
00164
00165 if (scv_debug_p == 0) {
00166 scv_debug_p = new scv_debug(filename);
00167 }
00168 else {
00169 scv_debug::close_trace_file();
00170 scv_debug_p->file_p = fopen(filename, "w");
00171 }
00172 }
00173
00174 void scv_debug::close_trace_file()
00175 {
00176 time_t t;
00177 char localString[1000];
00178
00179 if (scv_debug_p == 0) return;
00180 if (scv_debug_p->file_p == 0) return;
00181
00182 t = time(0);
00183 sprintf(localString, "\n*** Closing SCV Trace file at %s\n",
00184 ctime(&t));
00185 scv_debug_p->send_to_log(localString);
00186
00187 fclose(scv_debug_p->file_p);
00188 scv_debug_p->file_p = 0;
00189 }
00190
00191
00192
00193
00194
00195
00196
00197 void scv_debug::set_level_for_classes(int facility, int level)
00198 {
00199 local_origination = true;
00200 switch ( facility ) {
00201 case scv_debug::DATA_STRUCTURES :
00202 _scv_data_structure::set_debug(level);
00203 break;
00204 case scv_debug::INTROSPECTION :
00205 scv_extensions_if::set_debug(level);
00206 break;
00207 case scv_debug::MESSAGES :
00208 # ifndef _SCV_USE_SC_REPORT
00209 scv_report_handler::set_debug(level);
00210 # endif
00211 break;
00212 case scv_debug::RANDOMIZATION :
00213 scv_random::set_debug(level);
00214 scv_expression::set_debug(level);
00215 scv_constraint_base::set_debug(level);
00216 break;
00217 case scv_debug::RECORDING :
00218 scv_tr_db::set_debug(level);
00219 scv_tr_stream::set_debug(level);
00220 scv_tr_handle::set_debug(level);
00221 scv_tr_generator_base::set_debug(level);
00222 break;
00223 case scv_debug::SIGNALS :
00224 break;
00225 case scv_debug::TRANSACTORS :
00226 break;
00227 default :
00228 assert(0);
00229 break;
00230 }
00231 local_origination = false;
00232 }
00233
00234 void scv_debug::suspend(int facility)
00235 {
00236 char localString[1000];
00237
00238 if (scv_debug_p == 0) return;
00239
00240 if (facility == 0) {
00241 scv_debug_p->send_to_log("*** All tracing is now suspended.\n");
00242 for (int i = 1; i < LAST; i++) set_level_for_classes(i,SUSPENDED_DEBUG_LEVEL);
00243 }
00244 else {
00245 sprintf(localString, "*** All tracing for facility %s is suspended.\n",
00246 scv_debug_p->facility_names[facility]);
00247 scv_debug_p->send_to_log(localString);
00248 set_level_for_classes(facility,SUSPENDED_DEBUG_LEVEL);
00249 }
00250 }
00251
00252 void scv_debug::resume(int facility)
00253 {
00254 char localString[1000];
00255
00256 if (scv_debug_p == 0) scv_debug_p = new scv_debug();
00257
00258 if (facility == 0) {
00259 scv_debug_p->send_to_log("*** All tracing is now resumed.\n");
00260 for (int i = 1; i < LAST; i++) set_level_for_classes(i,scv_debug_p->facility_levels[i]);
00261 }
00262 else {
00263 sprintf(localString, "*** All tracing for facility %s is resumed.\n",
00264 scv_debug_p->facility_names[facility]);
00265 scv_debug_p->send_to_log(localString);
00266 set_level_for_classes(facility,scv_debug_p->facility_levels[facility]);
00267 }
00268 }
00269
00270 void scv_debug::set_level(debug_facilities facility, int level)
00271 {
00272 char localString[1000];
00273
00274 if (scv_debug_p == 0) scv_debug_p = new scv_debug();
00275
00276 if ( facility == ALL ) {
00277 for ( int i = 1; i < LAST; i++ ) {
00278 scv_debug_p->facility_levels[i] = level;
00279 set_level_for_classes(i,level);
00280 }
00281 sprintf(
00282 localString, "*** All tracing at level %d is now set.\n", level
00283 );
00284 }
00285 else {
00286 scv_debug_p->facility_levels[facility] = level;
00287 set_level_for_classes(facility,level);
00288 sprintf(
00289 localString, "*** Tracing for facility %s at level %d is now set.\n",
00290 scv_debug_p->facility_names[facility], level
00291 );
00292 }
00293 scv_debug_p->send_to_log(localString);
00294 }
00295
00296 void scv_debug::set_level(const char *facility_name, int level)
00297 {
00298 if (scv_debug_p == 0) scv_debug_p = new scv_debug();
00299
00300 for (int i = 0; i < LAST; i++) {
00301 if (strcasecmp(facility_name, scv_debug_p->facility_names[i]) == 0) {
00302 scv_debug::set_level((debug_facilities) i, level);
00303 return;
00304 }
00305 }
00306 }
00307
00308
00309 void scv_debug::set_facility_level(debug_facilities facility, int level)
00310 {
00311 char localString[1000];
00312
00313 if (local_origination) return;
00314
00315 if (scv_debug_p == 0) scv_debug_p = new scv_debug();
00316
00317 if (valid_facility(facility)) {
00318 if (valid_level(level)) {
00319 if (facility == ALL) {
00320 for (int i = 1; i < LAST; i++) scv_debug_p->facility_levels[i] = level;
00321 sprintf(localString, "*** All tracing at level %d is now set.\n", level);
00322 }
00323 else {
00324 scv_debug_p->facility_levels[facility] = level;
00325 sprintf(localString, "*** Tracing for facility %s at level %d is now set.\n",
00326 scv_debug_p->facility_names[facility], level);
00327 }
00328 }
00329 else {
00330 sprintf(
00331 localString, "*** Attempt to set invalid tracing level %d for facility %s.\n",
00332 level, scv_debug_p->facility_names[facility]
00333 );
00334 }
00335 }
00336 else {
00337 sprintf(localString, "*** Invalid tracing facility: %d.\n", facility);
00338 }
00339 scv_debug_p->send_to_log(localString);
00340 return;
00341 }
00342
00343 void scv_debug::open_stdout()
00344 {
00345 if (scv_debug_p == 0) scv_debug_p = new scv_debug();
00346 scv_debug_p->send_to_stdout = 1;
00347 }
00348
00349 void scv_debug::close_stdout()
00350 {
00351 if (scv_debug_p == 0) scv_debug_p = new scv_debug();
00352 scv_debug_p->send_to_stdout = 0;
00353 }
00354
00355 void scv_debug::set_debug(int debug)
00356 {
00357 if ( _debug == debug ) return;
00358 _debug = debug;
00359 scv_debug::set_facility_level(scv_debug::MESSAGES,debug);
00360 }
00361
00362 void scv_debug::indent(ostream& os, int indent)
00363 { for ( int i = 0; i < indent; i++ ) os << " "; }
00364
00365 void scv_debug::print(ostream&, int, int ) const {}
00366
00367 static bool valid_facility(scv_debug::debug_facilities facility)
00368 {
00369 if (facility >= scv_debug::ALL && facility < scv_debug::LAST) {
00370 return true;
00371 }
00372 else {
00373 return false;
00374 }
00375 }
00376
00377 #ifndef _HPUX_SOURCE
00378 #define MIN(A,B) ( (A>B) ? B : A )
00379 #endif
00380
00381 static bool valid_level(int level)
00382 {
00383 if ( level >= MIN(scv_debug::SUSPENDED_DEBUG_LEVEL,scv_debug::INITIAL_DEBUG_LEVEL) ) {
00384 return true;
00385 }
00386 else {
00387 return false;
00388 }
00389 }