00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 #include "GeneratorTrace.hpp"
00021 #include "Utils.hpp"
00022 #include "TimeUtils.hpp"
00023 
00024 static const char  gen_log_header_date   [] = "                       "     ;
00025 static const char  gen_log_header_nodate [] = ""                            ;
00026              char *gen_log_header           = (char*) gen_log_header_nodate ;
00027 
00028 const unsigned int gen_mask_table [] = {
00029   0x00000000,
00030   0x00000001,
00031   0x00000002,
00032   0x00000004,
00033   0x00000008,
00034   0x00000010,
00035   0xFFFFFFFF
00036 } ;
00037 
00038 const char gen_mask_char_table [] = {
00039   ' ',
00040   'E',
00041   'W',
00042   'M',
00043   'B',
00044   'T',
00045   'A'
00046 } ;
00047 
00048 T_TimeFunc     genTimeFunction     = no_time_func ;
00049 iostream_output*  genTrace            = &iostream_error ;
00050 unsigned int   genTraceLevel       = gen_mask_table[LOG_LEVEL_ALL] ;
00051 T_TraceEndProc genTraceEndFunction = no_end_func ;
00052 void*          genTraceEndArg      = NULL ;
00053 
00054 void init_trace (unsigned int P_level, 
00055                  char        *P_fileName,
00056                  bool         P_useTime) {
00057 
00058 
00059   genTraceLevel = P_level ;
00060 
00061   if (P_useTime == true) {
00062     genTimeFunction = current_time_func ;
00063     gen_log_header = (char*)gen_log_header_date ;
00064   } else {
00065     genTimeFunction = no_time_func ;
00066   }
00067 
00068   if (P_level) {
00069     if (P_fileName != NULL) {
00070       fstream_output* L_stream ;
00071       NEW_VAR(L_stream, fstream_output(P_fileName));
00072       if (!L_stream->good()) {
00073         iostream_error <<  "Unable to open file [" << P_fileName << "]" ;
00074         exit (0) ;
00075       } else {
00076         genTrace = L_stream ;
00077         GEN_LOG_EVENT_FORCE("START log");
00078       }
00079     } 
00080   }
00081 }
00082 
00083 
00084 void init_trace (unsigned int  P_level, 
00085                  string_t&  P_fileName,
00086                  bool          P_useTime) {
00087 
00088 
00089   genTraceLevel = P_level ;
00090   if (P_useTime == true) {
00091     genTimeFunction = current_time_func ;
00092     gen_log_header = (char*)gen_log_header_date ;
00093   } else {
00094     genTimeFunction = no_time_func ;
00095   }
00096 
00097   if (P_level) {
00098     if (P_fileName != "") {
00099       fstream_output* L_stream ;
00100       
00101       NEW_VAR(L_stream, fstream_output(P_fileName.c_str()));
00102       if (!L_stream->good()) {
00103         iostream_error <<  "Unable to open file [" << P_fileName << "]" ;
00104         exit (0) ;
00105       } else {
00106         genTrace = L_stream ;
00107         GEN_LOG_EVENT_FORCE("START log");
00108       }
00109     } 
00110   }
00111 }
00112 
00113 
00114 
00115 void close_trace () {
00116   
00117   GEN_LOG_EVENT_FORCE("STOP  log");
00118   if (genTrace != &iostream_error) {
00119     fstream_output *L_stream = dynamic_cast<fstream_output*>(genTrace);
00120     DELETE_VAR(L_stream);
00121     genTrace = &iostream_error;
00122   }
00123 
00124 }
00125 
00126 void no_time_func (iostream_output* P_stream) {
00127 }
00128 
00129 void no_end_func (void*) {
00130 }
00131 
00132 
00133 void current_time_func (iostream_output* P_stream) {
00134   char L_timeChar[TIME_STRING_LENGTH+1] ;
00135   struct timeval L_time ;
00136 
00137   GET_TIME(&L_time);
00138   time_tochar(L_timeChar, &L_time) ;
00139   *P_stream << L_timeChar ;
00140 }
00141 
00142 void use_trace_end (T_TraceEndProc P_proc, void* P_arg) {
00143   genTraceEndFunction = P_proc ;
00144   genTraceEndArg = P_arg ;
00145 }