Salome HOME
- Major update for launcher:
[modules/kernel.git] / src / DSC / DSC_Basic / DSC_interface.cxx
index eb6db311ced1705bcf4a9acf03d096ed17719c71..f8f309c69a4fde1ab83d61bb3bf9b18716ecc28c 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
+//  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  File   : DSC_interface.cxx
 //  Author : AndrĂ© RIBES (EDF)
 //  Module : KERNEL
 //
 #include <string>
 #include "DSC_interface.hxx"
+#ifdef WNT
+#else
+#include <sys/time.h>
+#endif
+#include <fstream>
+#include <sys/stat.h>
+#include <sstream>
+#include <stdlib.h>
+
+//#define MYDEBUG
 
 Engines_DSC_interface::Engines_DSC_interface() {}
 
@@ -38,8 +49,8 @@ Engines_DSC_interface::~Engines_DSC_interface()
 
 void
 Engines_DSC_interface::add_provides_port(Ports::Port_ptr ref, 
-                                const char* provides_port_name,
-                                Ports::PortProperties_ptr port_prop) 
+                                 const char* provides_port_name,
+                                 Ports::PortProperties_ptr port_prop) 
 throw (Engines::DSC::PortAlreadyDefined,
        Engines::DSC::NilPort,
        Engines::DSC::BadProperty) 
@@ -69,8 +80,8 @@ throw (Engines::DSC::PortAlreadyDefined,
 
 void
 Engines_DSC_interface::add_uses_port(const char* repository_id, 
-                            const char* uses_port_name,
-                            Ports::PortProperties_ptr port_prop) 
+                             const char* uses_port_name,
+                             Ports::PortProperties_ptr port_prop) 
 throw (Engines::DSC::PortAlreadyDefined,
        Engines::DSC::BadProperty) 
 {
@@ -101,10 +112,10 @@ throw (Engines::DSC::PortAlreadyDefined,
 
 Ports::Port_ptr
 Engines_DSC_interface::get_provides_port(const char* provides_port_name,
-                                const CORBA::Boolean connection_error) 
+                                 const CORBA::Boolean connection_error) 
   throw (Engines::DSC::PortNotDefined,
-        Engines::DSC::PortNotConnected, 
-        Engines::DSC::BadPortType) 
+         Engines::DSC::PortNotConnected, 
+         Engines::DSC::BadPortType) 
 {
   // Method arg test
   assert(provides_port_name);
@@ -138,8 +149,8 @@ Engines_DSC_interface::get_provides_port(const char* provides_port_name,
 Engines::DSC::uses_port * 
 Engines_DSC_interface::get_uses_port(const char* uses_port_name) 
   throw (Engines::DSC::PortNotDefined,
-        Engines::DSC::PortNotConnected,
-        Engines::DSC::BadPortType) 
+         Engines::DSC::PortNotConnected,
+         Engines::DSC::BadPortType) 
 {
   // Method arg test
   assert(uses_port_name);
@@ -154,7 +165,9 @@ Engines_DSC_interface::get_uses_port(const char* uses_port_name)
     Engines::DSC::BadPortType BPT;
     BPT.expected = CORBA::string_dup("Expected a uses port");
     BPT.received = CORBA::string_dup((std::string("Received a provides/none port : ")+uses_port_name).c_str());
+#ifdef MYDEBUG
    std::cout << "---- DSC_Interface : MARK 1 ---- exception : " << uses_port_name << "----" << std::endl;
+#endif
     throw BPT;
   }
 
@@ -164,7 +177,9 @@ Engines_DSC_interface::get_uses_port(const char* uses_port_name)
   }
   else
     {
+#ifdef MYDEBUG
    std::cout << "---- DSC_Interface : MARK 2 ---- exception : " << uses_port_name << "----" << std::endl;
+#endif
     throw Engines::DSC::PortNotConnected();
     }
   
@@ -190,17 +205,17 @@ Engines_DSC_interface::connect_provides_port(const char* provides_port_name)
   my_ports[provides_port_name]->connection_nbr += 1;
   // User code is informed
   provides_port_changed(provides_port_name, 
-                       my_ports[provides_port_name]->connection_nbr,
-                       Engines::DSC::AddingConnection
-                      );
+                        my_ports[provides_port_name]->connection_nbr,
+                        Engines::DSC::AddingConnection
+                       );
 }
 
 void
 Engines_DSC_interface::connect_uses_port(const char* uses_port_name,
-                                        Ports::Port_ptr provides_port_ref) 
+                                         Ports::Port_ptr provides_port_ref) 
   throw (Engines::DSC::PortNotDefined,
-        Engines::DSC::BadPortType,
-        Engines::DSC::NilPort)
+         Engines::DSC::BadPortType,
+         Engines::DSC::NilPort)
 {
   // Method arg test
   assert(uses_port_name);
@@ -234,14 +249,14 @@ Engines_DSC_interface::connect_uses_port(const char* uses_port_name,
     my_ports[uses_port_name]->connection_nbr += 1;
     // User code is informed
     uses_port_changed(uses_port_name,
-                     new Engines::DSC::uses_port(my_ports[uses_port_name]->uses_port_refs),
-                     Engines::DSC::AddingConnection);
+                      new Engines::DSC::uses_port(my_ports[uses_port_name]->uses_port_refs),
+                      Engines::DSC::AddingConnection);
   }
   else {
     Engines::DSC::BadPortType BPT;
     BPT.expected = CORBA::string_dup("Expected ...");
     BPT.received = CORBA::string_dup((std::string("Received an incorrect repository id type ")+
-                                     repository_id).c_str());
+                                      repository_id).c_str());
     throw BPT;
   }
 
@@ -269,7 +284,7 @@ Engines_DSC_interface::is_connected(const char* port_name)
 
 void
 Engines_DSC_interface::disconnect_provides_port(const char* provides_port_name,
-                                       const Engines::DSC::Message message)
+                                        const Engines::DSC::Message message)
 throw (Engines::DSC::PortNotDefined,
        Engines::DSC::PortNotConnected)
 {
@@ -287,8 +302,8 @@ throw (Engines::DSC::PortNotDefined,
   {
     my_ports[provides_port_name]->connection_nbr -= 1;
     provides_port_changed(provides_port_name,
-                         my_ports[provides_port_name]->connection_nbr,
-                         message);
+                          my_ports[provides_port_name]->connection_nbr,
+                          message);
   }
   else
     throw Engines::DSC::PortNotConnected();
@@ -296,8 +311,8 @@ throw (Engines::DSC::PortNotDefined,
 
 void
 Engines_DSC_interface::disconnect_uses_port(const char* uses_port_name,
-                                   Ports::Port_ptr provides_port_ref,
-                                   const Engines::DSC::Message message)
+                                    Ports::Port_ptr provides_port_ref,
+                                    const Engines::DSC::Message message)
 throw (Engines::DSC::PortNotDefined,
        Engines::DSC::PortNotConnected,
        Engines::DSC::BadPortReference) 
@@ -322,8 +337,8 @@ throw (Engines::DSC::PortNotDefined,
     {
       if (my_ports[uses_port_name]->uses_port_refs[i]->_is_equivalent(provides_port_ref))
       {
-       port_index = i;
-       break;
+        port_index = i;
+        break;
       }
     }
     if (port_index == -1)
@@ -339,15 +354,15 @@ throw (Engines::DSC::PortNotDefined,
     for(;index_ancien < seq_length;) {
       if (index_ancien == port_index) 
       {
-       // Rien a faire !
-       // On ne change pas le index du nouveau tableau
-       index_ancien += 1;
+        // Rien a faire !
+        // On ne change pas le index du nouveau tableau
+        index_ancien += 1;
       }
       else 
       {
-       (*new_uses_port)[index_nouveau] = my_ports[uses_port_name]->uses_port_refs[index_ancien];
-       index_ancien += 1;
-       index_nouveau += 1;
+        (*new_uses_port)[index_nouveau] = my_ports[uses_port_name]->uses_port_refs[index_ancien];
+        index_ancien += 1;
+        index_nouveau += 1;
       }
     }
 
@@ -356,8 +371,8 @@ throw (Engines::DSC::PortNotDefined,
 
     // The user code is informed
     uses_port_changed(uses_port_name,
-                     new_uses_port,
-                     message);
+                      new_uses_port,
+                      message);
   }
   else
     throw Engines::DSC::PortNotConnected();
@@ -379,3 +394,182 @@ 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 : <TMPDIR>/<container 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;
+
+#ifdef WNT
+#else
+  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;
+    }
+#endif
+}
+