00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "SugoiTracer\base.h"
00022
00023 namespace SG_TRC{
00024 using namespace SGE;
00025
00026
00027
00028 CL_TRACE_BASE_PARAMS::CL_TRACE_BASE_PARAMS()
00029 : SGE::CL_XML_OBJ("params")
00030 {
00031
00032 }
00033
00034 CL_TRACE_BASE_PARAMS::
00035 CL_TRACE_BASE_PARAMS(std::string _paramName, std::string &_ParamStr)
00036 : SGE::CL_XML_OBJ("params")
00037 {
00038 Params = "|";
00039 Params += _paramName;
00040 Params += ":";
00041 Params +=_ParamStr;
00042 Params += "|";
00043 }
00044
00045
00046
00047
00048
00049 CL_TRACE_BASE_PARAMS::CL_TRACE_BASE_PARAMS(std::string _paramName,int *_ParamInt, int _number)
00050 : SGE::CL_XML_OBJ("params")
00051 {
00052 AddInt(_paramName, _ParamInt, _number);
00053 }
00054
00055 CL_TRACE_BASE_PARAMS::~CL_TRACE_BASE_PARAMS(){}
00056
00057 void CL_TRACE_BASE_PARAMS::AddInt(std::string _paramName, const int * _IntArray, int _nbr, int _stringSize)
00058 {
00059 Params += "|";
00060 Params +=_paramName;
00061 Params += ":";
00062
00063
00064 for(int i =0; i < _nbr; i++)
00065 {
00066
00067 Params += ToCharStr(_IntArray[i]);
00068 if (i< _nbr - 1)
00069 Params += "/";
00070 }
00071 Params += "|";
00072 }
00073
00074 void CL_TRACE_BASE_PARAMS::AddChar(std::string _paramName, const char * _CharArray)
00075 {
00076 Params += "|";
00077 Params +=_paramName;
00078 Params += ":";
00079 Params += _CharArray;
00080 Params += "|";
00081 }
00082
00083 void CL_TRACE_BASE_PARAMS::ParseParams(int _ParamID, std::string & _DestStrArr)
00084 {
00085
00086
00087 std::string tempSource;
00088
00089 size_t Pos = -1;
00090 size_t Start = 0;
00091 size_t End = 0;
00092 tempSource = Params;
00093 do
00094 {
00095
00096 Start = tempSource.find_first_of('|');
00097 if (Start==-1)
00098 break;
00099 tempSource = tempSource.substr(Start+1,Params.size());
00100
00101 End = tempSource.find_first_of('|');
00102 _DestStrArr = tempSource.substr(Start,End);
00103 tempSource = tempSource.substr(End+1,Params.size());
00104 Pos++;
00105 }
00106 while(Pos != _ParamID);
00107
00108 if (Start == -1)
00109 _DestStrArr = "";
00110
00111
00112 }
00113
00114 bool CL_TRACE_BASE_PARAMS::SplitParam(const std::string &_src, std::string &_name, std::string &_val)
00115 {
00116 if(_src== "")
00117 return false;
00118 int Middle = _src.find_first_of(':');
00119
00120 if(Middle)
00121 {
00122 _name = _src.substr(0,Middle);
00123 _val = _src.substr(Middle+1, _src.size());
00124 return true;
00125 }
00126 return false;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165 #if _SG_TLS_XML
00166
00172 TiXmlElement* CL_TRACE_BASE_PARAMS::XMLLoad(TiXmlElement* _XML_ROOT)
00173 {
00174 SG_Assert(_XML_ROOT, "No XML root");
00175
00176 XMLReadVal (_XML_ROOT, "params", Params);
00177 int NewParamID=0;
00178 XMLReadVal(_XML_ROOT, "paramNbr", NewParamID);
00179
00180 std::string ParamTag;
00181 std::string ParamVal;
00182 std::string ParamName;
00183 TiXmlElement* XMLParam = NULL;
00184 for (int i=0; i< NewParamID; i++)
00185 {
00186 ParamTag = "param";
00187
00188 ParamTag += ToCharStr(i+1);
00189
00190 XMLParam = XMLGetElement(_XML_ROOT, ParamTag);
00191 if (XMLParam)
00192 {
00193 XMLReadVal(XMLParam, "name", ParamName);
00194 XMLReadVal(XMLParam, "val", ParamVal);
00195 if (ParamVal!="" && ParamName=="")
00196 {
00197 Params += ParamVal;
00198 }
00199 else if (ParamName!="" && ParamVal!="")
00200 {
00201 AddChar(ParamName, ParamVal.c_str());
00202 }
00203 }
00204 }
00205
00206 if (Params.size())
00207 return _XML_ROOT;
00208 else
00209 return NULL;
00210 }
00211
00212
00213
00219 TiXmlElement* CL_TRACE_BASE_PARAMS::XMLSave(TiXmlElement* _XML_ROOT)
00220 {
00221 SG_Assert(_XML_ROOT, "No XML root");
00222 std::string NewParam="";
00223 std::string ParamTag;
00224 int NewParamID = 0;
00225 TiXmlElement * XML_NewParam = NULL;
00226 int Start = 0;
00227
00228 #if 0
00229 do{
00230 ParseParams(NewParamID, NewParam);
00231 if (NewParam!="")
00232 {
00233 ParamTag = "param";
00234 ParamTag += IntToString(NewParamID+1, 1);
00235
00236 if (Start!=-1)
00237 {
00238 XML_NewParam = new TiXmlElement(ParamTag.c_str());
00239 XMLWriteVal(XML_NewParam, "val", NewParam);
00240 _XML_ROOT->InsertEndChild(*XML_NewParam);
00241 }
00242 }
00243 NewParamID++;
00244 }
00245 while(NewParam!="");
00246 #else
00247 std::string Name, Val;
00248
00249 do{
00250 ParseParams(NewParamID, NewParam);
00251 if (NewParam!="")
00252 {
00253 ParamTag = "param";
00254
00255 ParamTag += ToCharStr(NewParamID+1);
00256 if(!SplitParam(NewParam, Name, Val))
00257 {
00258 NewParamID++;
00259 continue;
00260 }
00261
00262 XML_NewParam = new TiXmlElement(ParamTag.c_str());
00263 XMLWriteVal(XML_NewParam, "name", Name);
00264 XMLWriteVal(XML_NewParam, "val", Val);
00265 _XML_ROOT->InsertEndChild(*XML_NewParam);
00266 }
00267 NewParamID++;
00268 }
00269 while(NewParam!="");
00270 #endif
00271
00272 XMLWriteVal(_XML_ROOT, "paramNbr", NewParamID);
00273
00274
00275 return _XML_ROOT;
00276 }
00277 #endif//_SG_TLS_XML
00278
00279 #if 1
00280
00281 _SG_TRC_DLL_EXPORT bool XMLSaveBench(const char *Filename)
00282 {
00283 #if _SG_TLS_XML
00284 TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00285 if (!CurApplitrc)
00286 {
00287 SG_WARNING_LOG("XMLLoadBench(" << Filename << "), no main TTCL_APPLI_TRACER\n");
00288 return false;
00289 }
00290
00291 if (CurApplitrc->XMLSaveToFile(Filename))
00292 return true;
00293 else
00294 SG_WARNING_LOG("File : " << Filename << " Could not be saved\n");
00295 return false;
00296 #else
00297 SG_Assert(0, "_SG_TLS_XML is not defined");
00298 return false;
00299 #endif
00300 }
00301
00302 _SG_TRC_DLL_EXPORT bool XMLLoadBench(const char *Filename)
00303 {
00304 #if _SG_TLS_XML
00305 TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00306 if (!CurApplitrc)
00307 {
00308 SG_WARNING_LOG("XMLLoadBench(" << Filename << "), no main TTCL_APPLI_TRACER\n");
00309
00310 CurApplitrc = CreateMainTracer<SG_TRC_Default_Trc_Type>();
00311 }
00312
00313 if (CurApplitrc->XMLLoadFromFile(Filename))
00314 return true;
00315 else
00316 SG_WARNING_LOG("File : " << Filename << " Could not be loaded\n");
00317 return false;
00318 #else
00319 SG_Assert(0, "_SG_TLS_XML is not defined");
00320 #endif
00321 }
00322
00323 int CreateFunction(const char * _fctName)
00324 {
00325 if(!_fctName)
00326 {
00327 SG_WARNING_LOG("CreateFunction(): function name is null");
00328 return -1;
00329 }
00330
00331
00332 TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00333 if (!CurApplitrc)
00334 CurApplitrc = CreateMainTracer<CL_TimerVal>();
00335
00336 SG_Assert(CurApplitrc, "Could not create the main appli tracer!");
00337
00338 if (!CurApplitrc->MapFunction)
00339 return 0;
00340
00341
00342
00343
00345 TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle Fct = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer->AddFunct(_fctName);
00346
00347
00348 std::map<int, TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle>::iterator itFct;
00349 for ( itFct = CurApplitrc->MapFunction->begin( ) ; itFct!= CurApplitrc->MapFunction->end( ) ;itFct++ )
00350 {
00351 if ((*itFct).second == Fct)
00352 return (*itFct).first;
00353 }
00354
00355
00356 CurApplitrc->MapFunction->insert(std::pair<int, TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle>(++CurApplitrc->MapFunctionLastID, Fct));
00357 return CurApplitrc->MapFunctionLastID;
00358
00359 }
00360
00361
00362 int StartRecording(const int _FctNumber, CL_TRACE_BASE_PARAMS * _Params)
00363 {
00364
00365
00366 TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00367 if (!CurApplitrc) return 0;
00368
00369 TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle CurFct = NULL;
00370
00371 std::map<int, TTCL_APPLI_TRACER<CL_TimerVal>::TplFctHdle>::iterator iFct = CurApplitrc->MapFunction->find(_FctNumber);
00372 if (iFct != CurApplitrc->MapFunction->end())
00373 CurFct = (*iFct).second;
00374
00375
00376 CL_TEMP_TRACER<CL_TimerVal> * NewTracer = new CL_TEMP_TRACER<CL_TimerVal>(CurFct, _Params);
00377
00378
00379 CurApplitrc->MapTempTracer->insert(std::pair<int, CL_TEMP_TRACER<CL_TimerVal> *>(++CurApplitrc->MapTracerLastID, NewTracer));
00380
00381
00382 return CurApplitrc->MapTracerLastID;
00383
00384 }
00385
00386
00387 int StartRecordingInt(const int _FctNumber, const char * ParamsName, const int * _intArray, const int _nbr)
00388 {
00389 return StartRecording(_FctNumber, new CL_TRACE_BASE_PARAMS(ParamsName, (int*)_intArray, _nbr));
00390 }
00391
00392 int StartRecordingChar(const int _FctNumber, const char * ParamsName, const char *_Value)
00393 {
00394 return StartRecording(_FctNumber, new CL_TRACE_BASE_PARAMS(ParamsName, std::string(_Value)));
00395 }
00396
00397
00398 void StopRecording(const int _TracerNumber)
00399 {
00400 CL_TimerVal *CurTimer=NULL;
00401 CL_TimerVal TempTimer;
00402 TempTimer.Run();
00403
00404
00405
00406
00407 TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00408 if (!CurApplitrc) return;
00409
00410
00411
00412 std::map<int, CL_TEMP_TRACER<CL_TimerVal>*>::iterator iTrc = CurApplitrc->MapTempTracer->find(_TracerNumber);
00413 if (iTrc != CurApplitrc->MapTempTracer->end())
00414 {
00415
00416
00417 CurTimer = (*iTrc).second->GetTracer()->GetResult();
00418
00419
00420 if (CurTimer)
00421 {
00422
00423 CurTimer->Run(&TempTimer);
00424 TempTimer = *CurTimer;
00425
00426
00427 }
00428
00429 delete (*iTrc).second;
00430 CurApplitrc->MapTempTracer-> erase(_TracerNumber);
00431 }
00432
00433
00434 if (CurTimer)
00435 {
00436 *CurTimer = TempTimer;
00437
00438
00439 }
00440
00441
00442 }
00443
00444 bool AddParamInt(const int _TracerNumber, const char * _Name, const int _Value)
00445 {
00446 TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00447 if (!CurApplitrc) return false;
00448
00449
00450 std::map<int, CL_TEMP_TRACER<CL_TimerVal>*>::iterator iTrc = CurApplitrc->MapTempTracer->find(_TracerNumber);
00451 if (iTrc != CurApplitrc->MapTempTracer->end())
00452 {
00453 CL_TRACE_BASE_PARAMS * TrcParam = (*iTrc).second->GetTracer()->GetParams();
00454 if (TrcParam)
00455 {
00456 TrcParam->AddInt(_Name, &_Value);
00457 }
00458 else
00459 {std::cout << "AddParam()=> tracer has no params"<< std::endl;}
00460 }
00461 return true;
00462
00463 }
00464 bool AddParamChar(const int _TracerNumber, const char * _Name, const char * _Value)
00465 {
00466 TTCL_APPLI_TRACER<CL_TimerVal> * CurApplitrc = TTCL_APPLI_TRACER<CL_TimerVal>::MainAppliTracer;
00467 if (!CurApplitrc) return false;
00468
00469
00470 std::map<int, CL_TEMP_TRACER<CL_TimerVal>*>::iterator iTrc = CurApplitrc->MapTempTracer->find(_TracerNumber);
00471 if (iTrc != CurApplitrc->MapTempTracer->end())
00472 {
00473 CL_TRACE_BASE_PARAMS * TrcParam = (*iTrc).second->GetTracer()->GetParams();
00474 if (TrcParam)
00475 {
00476 TrcParam->AddChar(_Name, _Value);
00477 }
00478 else
00479 {std::cout << "AddParam()=> tracer has no params"<< std::endl;}
00480 }
00481 return true;
00482 }
00483
00484
00485 #endif
00486
00487 };