From e379646b647cf817ef93f995cbd123458eae0c52 Mon Sep 17 00:00:00 2001 From: caremoli Date: Thu, 1 Oct 2009 16:23:10 +0000 Subject: [PATCH] CCAR: move DSC trace function from Calcium directory to DSC_Basic directory for more general use --- src/DSC/DSC_Basic/DSC_interface.cxx | 181 ++++++++++++++++++ src/DSC/DSC_Basic/DSC_interface.hxx | 3 + .../DSC_User/Datastream/Calcium/Calcium.cxx | 2 +- .../Calcium/CalciumCxxInterface.cxx | 158 --------------- .../Calcium/CalciumCxxInterface.hxx | 65 +++---- src/DSC/DSC_User/Datastream/GenericPort.hxx | 2 + 6 files changed, 218 insertions(+), 193 deletions(-) diff --git a/src/DSC/DSC_Basic/DSC_interface.cxx b/src/DSC/DSC_Basic/DSC_interface.cxx index fcfd8edf4..8cef59b0a 100644 --- a/src/DSC/DSC_Basic/DSC_interface.cxx +++ b/src/DSC/DSC_Basic/DSC_interface.cxx @@ -25,6 +25,11 @@ // #include #include "DSC_interface.hxx" +#include +#include +#include +#include +#include //#define MYDEBUG @@ -385,3 +390,179 @@ Engines_DSC_interface::get_port_properties(const char* port_name) rtn_properties = Ports::PortProperties::_duplicate(my_ports[port_name]->port_prop); return rtn_properties; } + +//Trace functions for DSC operations: a local function (initTrace) and a class method (Engines_DSC_interface::writeEvent) +static int traceType=-1; // 0=stderr;1=file; +static int traceLevel=-1; // 0=no trace;1=normal trace;2=detailed trace +static std::ofstream traceFile; +static std::ostream *out; + +//! Initialize the trace file +/*! + * The trace file depends on an environment variable (DSC_TRACE). If this variable + * is equal to 1, the trace file is a file with the name : /.tce. + * In all other cases, the trace file is stderr + * The environment variable DSC_TRACELEVEL can be used to suppress the trace (value 0) + * + * \param containerName the name of the container where the trace is built + */ +static void initTrace(const std::string& containerName) +{ + // if initialization has already been done do nothing + if(traceLevel >= 0)return; + + std::string typeenv="0"; + std::string levelenv="1"; + char* valenv=0; + valenv=getenv("DSC_TRACE"); + if(valenv)typeenv=valenv; + valenv=getenv("DSC_TRACELEVEL"); + if(valenv)levelenv=valenv; + + if(levelenv=="0") + traceLevel=0; // no trace + else if(levelenv=="2") + traceLevel=2; //detailed trace + else + traceLevel=1; // normal trace (default) + + if(traceLevel==0) + return; + + if(typeenv=="1") + { + //trace in file + traceType=1; +#ifdef WNT + std::string logFilename=getenv("TEMP"); + logFilename += "\\"; +#else + std::string logFilename="/tmp"; + char* val = getenv("SALOME_TMP_DIR"); + if(val) + { + struct stat file_info; + stat(val, &file_info); + bool is_dir = S_ISDIR(file_info.st_mode); + if (is_dir)logFilename=val; + } + logFilename += "/"; +#endif + + logFilename=logFilename+containerName+".tce"; + traceFile.open(logFilename.c_str(), std::ios::out | std::ios::app); + out=&traceFile; + } + else + { + //trace to stderr (default) + traceType=0; + out=&std::cerr; + } + //trace heading + out->width(17); + *out << "Elapsed time" ; + *out << " | " ; + out->width(16); + *out << "Request" ; + *out << " | " ; + out->width(16); + *out << "Container" ; + *out << " | " ; + out->width(16); + *out << "Instance" ; + *out << " | " ; + out->width(16); + *out << "Port" ; + *out << " | " ; + out->width(24); + *out << "Error"; + *out << " | " ; + *out << "Infos" ; + *out << std::endl; +} + + +//! Write a record in the trace file +/*! + * \param request the name of the request executed + * \param containerName the name of the container where the request is executed + * \param instance_name the name of the component where the request is executed + * \param port_name the name of the port that is concerned + * \param error if an error has occured, a string that identifies the error + * \param message informations about error or about the request + */ +void Engines_DSC_interface::writeEvent(const char* request,const std::string& containerName, const char* instance_name, + const char* port_name, const char* error, const char* message) +{ + if(traceLevel < 0) + initTrace(containerName); + if(traceLevel == 0)return; + + struct timeval tv; + gettimeofday(&tv,0); + long tt0=tv.tv_sec/3600; //hours + + if(traceType == 2) + { + //notifier (not used: salome notifier is now obsolete) + std::ostringstream msg; + msg.width(7); + msg << tt0 ; + msg << ":" ; + long tt1=(tv.tv_sec-3600*tt0)/60;//minutes + msg.width(2); + msg << tt1 ; + msg << ":" ; + long tt2=tv.tv_sec - 3600*tt0-60*tt1; //seconds + msg.width(2); + msg << tt2 ; + msg << ":" ; + long tt3=tv.tv_usec/1000; //milliseconds + msg.width(3); + msg << tt3 ; + msg << " | " ; + msg.width(24); + msg << error; + msg << " | " ; + msg << message ; + //send event to notifier (containerName.c_str(),instance_name, request, msg.str().c_str()) + } + else + { + //cerr or file + out->width(7); + *out << tt0 ; + *out << ":" ; + long tt1=(tv.tv_sec-3600*tt0)/60;//minutes + out->width(2); + *out << tt1 ; + *out << ":" ; + long tt2=tv.tv_sec - 3600*tt0-60*tt1; //seconds + out->width(2); + *out << tt2 ; + *out << ":" ; + long tt3=tv.tv_usec/1000; //milliseconds + out->width(3); + *out << tt3 ; + *out << " | " ; + out->width(16); + *out << request ; + *out << " | " ; + out->width(16); + *out << containerName ; + *out << " | " ; + out->width(16); + *out << instance_name ; + *out << " | " ; + out->width(16); + *out << port_name ; + *out << " | " ; + out->width(24); + *out << error; + *out << " | " ; + *out << message ; + *out << std::endl; + } +} + diff --git a/src/DSC/DSC_Basic/DSC_interface.hxx b/src/DSC/DSC_Basic/DSC_interface.hxx index af7d7f5b2..ad0eb3539 100644 --- a/src/DSC/DSC_Basic/DSC_interface.hxx +++ b/src/DSC/DSC_Basic/DSC_interface.hxx @@ -148,6 +148,9 @@ public: virtual Ports::PortProperties_ptr get_port_properties(const char* port_name) throw (Engines::DSC::PortNotDefined); + static void writeEvent(const char* request,const std::string& containerName, const char* instance_name, + const char* port_name, const char* error, const char* message); + protected: /*-------------------------------------------------*/ diff --git a/src/DSC/DSC_User/Datastream/Calcium/Calcium.cxx b/src/DSC/DSC_User/Datastream/Calcium/Calcium.cxx index f98b1edb6..2b6429f8a 100644 --- a/src/DSC/DSC_User/Datastream/Calcium/Calcium.cxx +++ b/src/DSC/DSC_User/Datastream/Calcium/Calcium.cxx @@ -90,7 +90,7 @@ extern "C" msg << type << " " << mode << " " << depend; CORBA::String_var componentName=compo->instanceName(); std::string containerName=compo->getContainerName(); - CalciumInterface::writeEvent("create_calcium_port",containerName,componentName,name,0,msg.str().c_str()); + Engines_DSC_interface::writeEvent("create_calcium_port",containerName,componentName,name,"",msg.str().c_str()); if(std::string(mode) == "IN") { diff --git a/src/DSC/DSC_User/Datastream/Calcium/CalciumCxxInterface.cxx b/src/DSC/DSC_User/Datastream/Calcium/CalciumCxxInterface.cxx index 5a2525252..8b05780c4 100644 --- a/src/DSC/DSC_User/Datastream/Calcium/CalciumCxxInterface.cxx +++ b/src/DSC/DSC_User/Datastream/Calcium/CalciumCxxInterface.cxx @@ -30,168 +30,10 @@ #include "CalciumCxxInterface.hxx" #define PRG_MAIN #include "calciumP.h" -#include -#include -#include using namespace std; namespace CalciumInterface { - - int traceType=-1; // 0=stderr;1=file; - int traceLevel=-1; // 0=no trace;1=normal trace;2=detailed trace - std::ofstream traceFile; - std::ostream *out; - - void initTrace(const std::string& containerName) - { - // if initialization has already been done do nothing - if(traceLevel >= 0)return; - - std::string typeenv="0"; - std::string levelenv="1"; - char* valenv=0; - valenv=getenv("DSC_TRACE"); - if(valenv)typeenv=valenv; - valenv=getenv("DSC_TRACELEVEL"); - if(valenv)levelenv=valenv; - - if(levelenv=="0") - traceLevel=0; // no trace - else if(levelenv=="2") - traceLevel=2; //detailed trace - else - traceLevel=1; // normal trace (default) - - if(traceLevel==0) - return; - - if(typeenv=="1") - { - //trace in file - traceType=1; -#ifdef WNT - std::string logFilename=getenv("TEMP"); - logFilename += "\\"; -#else - std::string logFilename="/tmp"; - char* val = getenv("SALOME_TMP_DIR"); - if(val) - { - struct stat file_info; - stat(val, &file_info); - bool is_dir = S_ISDIR(file_info.st_mode); - if (is_dir)logFilename=val; - } - logFilename += "/"; -#endif - - logFilename=logFilename+containerName+".tce"; - traceFile.open(logFilename.c_str(), std::ios::out | std::ios::app); - out=&traceFile; - } - else - { - //trace to stderr (default) - traceType=0; - out=&std::cerr; - } - //trace heading - out->width(17); - *out << "Elapsed time" ; - *out << " | " ; - out->width(16); - *out << "Request" ; - *out << " | " ; - out->width(16); - *out << "Container" ; - *out << " | " ; - out->width(16); - *out << "Instance" ; - *out << " | " ; - out->width(16); - *out << "Port" ; - *out << " | " ; - out->width(24); - *out << "Error"; - *out << " | " ; - *out << "Infos" ; - *out << std::endl; - } - - void writeEvent(const char* request,const std::string& containerName, const char* instance_name, const char* port_name, - int error, const char* message) - { - if(traceLevel < 0) - initTrace(containerName); - if(traceLevel == 0)return; - - struct timeval tv; - gettimeofday(&tv,0); - long tt0=tv.tv_sec/3600; //hours - - if(traceType == 2) - { - //notifier (not used: salome notifier is now obsolete) - std::ostringstream msg; - msg.width(7); - msg << tt0 ; - msg << ":" ; - long tt1=(tv.tv_sec-3600*tt0)/60;//minutes - msg.width(2); - msg << tt1 ; - msg << ":" ; - long tt2=tv.tv_sec - 3600*tt0-60*tt1; //seconds - msg.width(2); - msg << tt2 ; - msg << ":" ; - long tt3=tv.tv_usec/1000; //milliseconds - msg.width(3); - msg << tt3 ; - msg << " | " ; - msg.width(24); - msg << CPMESSAGE[error]; - msg << " | " ; - msg << message ; - //send event to notifier (containerName.c_str(),instance_name, request, msg.str().c_str()) - } - else - { - //cerr or file - out->width(7); - *out << tt0 ; - *out << ":" ; - long tt1=(tv.tv_sec-3600*tt0)/60;//minutes - out->width(2); - *out << tt1 ; - *out << ":" ; - long tt2=tv.tv_sec - 3600*tt0-60*tt1; //seconds - out->width(2); - *out << tt2 ; - *out << ":" ; - long tt3=tv.tv_usec/1000; //milliseconds - out->width(3); - *out << tt3 ; - *out << " | " ; - out->width(16); - *out << request ; - *out << " | " ; - out->width(16); - *out << containerName ; - *out << " | " ; - out->width(16); - *out << instance_name ; - *out << " | " ; - out->width(16); - *out << port_name ; - *out << " | " ; - out->width(24); - *out << CPMESSAGE[error]; - *out << " | " ; - *out << message ; - *out << std::endl; - } - } }; diff --git a/src/DSC/DSC_User/Datastream/Calcium/CalciumCxxInterface.hxx b/src/DSC/DSC_User/Datastream/Calcium/CalciumCxxInterface.hxx index 18738f46a..e1129154c 100644 --- a/src/DSC/DSC_User/Datastream/Calcium/CalciumCxxInterface.hxx +++ b/src/DSC/DSC_User/Datastream/Calcium/CalciumCxxInterface.hxx @@ -53,17 +53,14 @@ struct IsSameType { static const bool value = true; }; +extern const char * CPMESSAGE[]; + //#define MYDEBUG #include namespace CalciumInterface { - /********************* LOGGING INTERFACE *****************/ - - void initTrace(const std::string&); - void writeEvent(const char*, const std::string&, const char*, const char*, int, const char*); - /********************* CONNECTION INTERFACE *****************/ static inline void @@ -73,7 +70,7 @@ namespace CalciumInterface { CORBA::String_var componentName=component.instanceName(); std::string containerName=component.getContainerName(); if (instanceName.empty()) instanceName=componentName; - writeEvent("CP_CD",containerName,componentName,"",0,""); + Engines_DSC_interface::writeEvent("CP_CD",containerName,componentName,"","",""); } static void @@ -81,7 +78,7 @@ namespace CalciumInterface { { CORBA::String_var componentName=component.instanceName(); std::string containerName=component.getContainerName(); - writeEvent("CP_FIN",containerName,componentName,"",0,""); + Engines_DSC_interface::writeEvent("CP_FIN",containerName,componentName,"","",""); std::vector usesPortNames; std::vector::const_iterator it; @@ -117,12 +114,12 @@ namespace CalciumInterface { } catch ( const Superv_Component_i::BadCast & ex) { - writeEvent("CP_FIN",containerName,componentName,"",CalciumTypes::CPTPVR,ex.what()); + Engines_DSC_interface::writeEvent("CP_FIN",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPTPVR],ex.what()); throw (CalciumException(CalciumTypes::CPTPVR,ex)); } catch ( const DSC_Exception & ex) { - writeEvent("CP_FIN",containerName,componentName,"",CalciumTypes::CPOK,ex.what()); + Engines_DSC_interface::writeEvent("CP_FIN",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPOK],ex.what()); // Exception venant de SupervComponent : // PortNotDefined(CPNMVR), PortNotConnected(CPLIEN) // ou du port uses : Dsc_Exception @@ -130,7 +127,7 @@ namespace CalciumInterface { } catch (...) { - writeEvent("CP_FIN",containerName,componentName,"",CalciumTypes::CPATAL,"Unexpected exception"); + Engines_DSC_interface::writeEvent("CP_FIN",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPATAL],"Unexpected exception"); throw (CalciumException(CalciumTypes::CPATAL,"Unexpected exception")); // En fonction du mode de gestion des erreurs throw; } @@ -202,7 +199,7 @@ namespace CalciumInterface { if (nomVar.empty()) { - writeEvent("BEGIN_READ",containerName,componentName,"",CalciumTypes::CPNMVR,""); + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,"",CPMESSAGE[CalciumTypes::CPNMVR],""); throw CalciumException(CalciumTypes::CPNMVR, LOC("Empty variable name")); } PortType * port; @@ -223,18 +220,18 @@ namespace CalciumInterface { } catch ( const Superv_Component_i::PortNotDefined & ex) { - writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CalciumTypes::CPNMVR,ex.what()); + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPNMVR],ex.what()); throw (CalciumException(CalciumTypes::CPNMVR,ex)); } catch ( const Superv_Component_i::PortNotConnected & ex) { - writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CalciumTypes::CPLIEN,ex.what()); + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPLIEN],ex.what()); throw (CalciumException(CalciumTypes::CPLIEN,ex)); // VERIFIER LES CAS DES CODES : CPINARRET, CPSTOPSEQ, CPCTVR, CPLIEN } catch ( const Superv_Component_i::BadCast & ex) { - writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CalciumTypes::CPTPVR,ex.what()); + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPTPVR],ex.what()); throw (CalciumException(CalciumTypes::CPTPVR,ex)); } @@ -243,13 +240,13 @@ namespace CalciumInterface { if ( portDependencyType == CalciumTypes::UNDEFINED_DEPENDENCY ) { - writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CalciumTypes::CPIT,"Dependency mode is undefined"); + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPIT],"Dependency mode is undefined"); throw CalciumException(CalciumTypes::CPIT, LOC(OSS()<<"Dependency mode of variable " << nomVar << " is undefined.")); } if ( ( portDependencyType != _dependencyType ) && ( _dependencyType != CalciumTypes::SEQUENCE_DEPENDENCY ) ) { - writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CalciumTypes::CPIT, + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPIT], "Dependency mode is not the same as the required one"); throw CalciumException(CalciumTypes::CPITVR, LOC(OSS()<<"Dependency mode of variable " << nomVar << ": " << portDependencyType << " is not the same as the required one.")); @@ -263,7 +260,7 @@ namespace CalciumInterface { { double tt=ti; msg << "ti=" << ti << ", tf=" << tf ; - writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),0,msg.str().c_str()); + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),"",msg.str().c_str()); corbaData = port->get(tt,tf, 0); msgout << "read t=" << tt ; #ifdef MYDEBUG @@ -272,7 +269,7 @@ namespace CalciumInterface { } catch ( const DSC_Exception & ex) { - writeEvent("END_READ",containerName,componentName,nomVar.c_str(),0,ex.what()); + Engines_DSC_interface::writeEvent("END_READ",containerName,componentName,nomVar.c_str(),"",ex.what()); throw; } } @@ -281,7 +278,7 @@ namespace CalciumInterface { try { msg << "i=" << i ; - writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),0,msg.str().c_str()); + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),"",msg.str().c_str()); corbaData = port->get(0, i); msgout << "read i=" << i ; #ifdef MYDEBUG @@ -290,7 +287,7 @@ namespace CalciumInterface { } catch ( const DSC_Exception & ex) { - writeEvent("END_READ",containerName,componentName,nomVar.c_str(),0,ex.what()); + Engines_DSC_interface::writeEvent("END_READ",containerName,componentName,nomVar.c_str(),"",ex.what()); throw; } } @@ -302,7 +299,7 @@ namespace CalciumInterface { #ifdef MYDEBUG std::cout << "-------- CalciumInterface(ecp_lecture) MARK 7 ------------------" << std::endl; #endif - writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),0,"Sequential read"); + Engines_DSC_interface::writeEvent("BEGIN_READ",containerName,componentName,nomVar.c_str(),"","Sequential read"); corbaData = port->next(ti,i); msgout << "read "; if(i==0)msgout<< "t=" <put(*corbaData,t, -1); std::stringstream msg; msg << "t=" << t ; - writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CalciumTypes::CPOK,msg.str().c_str()); + Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPOK],msg.str().c_str()); } catch ( const DSC_Exception & ex) { - writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CalciumTypes::CPATAL,ex.what()); + Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPATAL],ex.what()); throw (CalciumException(CalciumTypes::CPATAL,ex.what())); } //Le -1 peut être traité par le cst DataIdContainer et transformé en 0 @@ -580,11 +577,11 @@ namespace CalciumInterface { port->put(*corbaData,-1, i); std::stringstream msg; msg << "i=" << i ; - writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CalciumTypes::CPOK,msg.str().c_str()); + Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPOK],msg.str().c_str()); } catch ( const DSC_Exception & ex) { - writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CalciumTypes::CPATAL,ex.what()); + Engines_DSC_interface::writeEvent("WRITE",containerName,componentName,nomVar.c_str(),CPMESSAGE[CalciumTypes::CPATAL],ex.what()); throw (CalciumException(CalciumTypes::CPATAL,ex.what())); } #ifdef MYDEBUG diff --git a/src/DSC/DSC_User/Datastream/GenericPort.hxx b/src/DSC/DSC_User/Datastream/GenericPort.hxx index ba046314e..a760ee011 100644 --- a/src/DSC/DSC_User/Datastream/GenericPort.hxx +++ b/src/DSC/DSC_User/Datastream/GenericPort.hxx @@ -502,6 +502,7 @@ GenericPort::get(TimeType time, // Waiting too long probably blocking std::stringstream msg; msg<<"Timeout ("<::next(TimeType &t, // Waiting too long probably blocking std::stringstream msg; msg<<"Timeout ("<