1 // Copyright (C) 2024 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "SALOME_LogManager.hxx"
21 #include "SALOME_Fake_NamingService.hxx"
22 #include "SALOME_ContainerManager.hxx"
28 #include <Basics_Utils.hxx>
37 const char SALOME_LogManager::NAME_IN_NS[]="/LogManager";
39 static std::vector<char> FromPyToCpp(PyObject *obj)
41 std::vector<char> ret;
42 char *buffer = nullptr;
43 Py_ssize_t length = 0;
44 PyBytes_AsStringAndSize(obj,&buffer,&length);
46 for(auto i = 0 ; i < length ; ++i)
51 static SALOME::vectorOfByte *FromVectCharToCorba(const std::vector<char>& data)
53 SALOME::vectorOfByte_var ret = new SALOME::vectorOfByte;
54 auto length = data.size();
56 for(auto i = 0 ; i < length ; ++i)
61 PortableServer::POA_var SALOME_ContainerScriptExecPerfLog::getPOA()
63 return father()->getPOA();
66 void SALOME_ContainerScriptExecPerfLog::assign(const SALOME::vectorOfByte& value)
68 auto sz = value.length();
70 for(auto i = 0 ; i < sz ; ++i)
73 AutoPyRef s( this->end( ) );
75 _data = FromPyToCpp(s);
78 SALOME::vectorOfByte *SALOME_ContainerScriptExecPerfLog::getObj()
80 return FromVectCharToCorba(this->_data);
83 void SALOME_ContainerScriptExecPerfLog::accept(SALOME_VisitorContainerLog &visitor)
85 visitor.visitContainerScriptExecPerfLog( *this );
88 void SALOME_ContainerScriptExecPerfLog::start()
91 AutoPyRef result = PyObject_CallMethod(pyObj(),(char*)"start","",nullptr);
94 std::string error("can not start");
96 THROW_SALOME_CORBA_EXCEPTION(error.c_str(),SALOME::INTERNAL_ERROR);
100 AutoPyRef SALOME_ContainerScriptExecPerfLog::end()
103 //https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue
104 AutoPyRef result(PyObject_CallMethod(pyObj(),(char*)"end","y#",_data.data(),_data.size(),nullptr) ) ;
105 if (PyErr_Occurred())
107 std::string error("can not end");
109 THROW_SALOME_CORBA_EXCEPTION(error.c_str(),SALOME::INTERNAL_ERROR);
116 PortableServer::POA_var SALOME_ContainerScriptPerfLog::getPOA()
118 return father()->getPOA();
121 char *SALOME_ContainerScriptPerfLog::getCode()
123 return CORBA::string_dup( _code.c_str() );
126 char *SALOME_ContainerScriptPerfLog::getName()
128 return CORBA::string_dup( _name.c_str() );
131 void SALOME_ContainerScriptPerfLog::accept(SALOME_VisitorContainerLog &visitor)
133 visitor.enterContainerScriptPerfLog( *this );
134 for(auto session : _sessions)
136 PortableServer::ServantBase *serv = getPOA()->reference_to_servant( session );
138 SALOME_ContainerScriptExecPerfLog *servC = dynamic_cast<SALOME_ContainerScriptExecPerfLog *>(serv);
139 visitor.visitContainerScriptExecPerfLog( *servC );
141 visitor.leaveContainerScriptPerfLog( *this );
144 Engines::ContainerScriptExecPerfLog_ptr SALOME_ContainerScriptPerfLog::addExecutionSession()
146 SALOME_ContainerScriptExecPerfLog *execution = new SALOME_ContainerScriptExecPerfLog(this);
147 PortableServer::ObjectId_var id(getPOA()->activate_object(execution));
148 execution->_remove_ref();
149 CORBA::Object_var executionPtr(getPOA()->id_to_reference(id));
150 Engines::ContainerScriptExecPerfLog_var executionPtr2 = Engines::ContainerScriptExecPerfLog::_narrow(executionPtr);
151 _sessions.push_back( executionPtr2 );
154 AutoPyRef result = PyObject_CallMethod(pyObj(),(char*)"addExecution","",nullptr);
155 if (PyErr_Occurred())
157 std::string error("can not addExecution");
159 THROW_SALOME_CORBA_EXCEPTION(error.c_str(),SALOME::INTERNAL_ERROR);
161 execution->setPyObj( result.retn() );//ownership of result is transfered to execution
164 return executionPtr2._retn();
167 Engines::ListOfContainerScriptExecPerfLog *SALOME_ContainerScriptPerfLog::listOfExecs()
169 Engines::ListOfContainerScriptExecPerfLog_var ret = new Engines::ListOfContainerScriptExecPerfLog;
170 auto sz = this->_sessions.size();
172 for(auto i = 0 ; i < sz ; ++i)
173 ret[i] = this->_sessions[i];
179 PortableServer::POA_var SALOME_ContainerPerfLog::getPOA()
181 return father()->getPOA();
184 Engines::ContainerScriptPerfLog_ptr SALOME_ContainerPerfLog::addScript(const char *name, const char *code)
186 SALOME_ContainerScriptPerfLog *script = new SALOME_ContainerScriptPerfLog(this,name,code);
187 PortableServer::ObjectId_var id(getPOA()->activate_object(script));
188 script->_remove_ref();
189 CORBA::Object_var scriptPtr(getPOA()->id_to_reference(id));
190 Engines::ContainerScriptPerfLog_var scriptPtr2 = Engines::ContainerScriptPerfLog::_narrow(scriptPtr);
191 _scripts.push_back( scriptPtr2 );
194 PyObject *result = PyObject_CallMethod(pyObj(),(char*)"addScript","",nullptr);
195 if (PyErr_Occurred())
197 std::string error("can not addScript");
199 THROW_SALOME_CORBA_EXCEPTION(error.c_str(),SALOME::INTERNAL_ERROR);
201 script->setPyObj( result );
203 return scriptPtr2._retn();
206 Engines::ListOfContainerScriptPerfLog *SALOME_ContainerPerfLog::listOfScripts()
208 Engines::ListOfContainerScriptPerfLog_var ret = new Engines::ListOfContainerScriptPerfLog;
209 std::size_t len( this->_scripts.size() );
211 for(std::size_t i = 0 ; i < len ; ++i)
212 ret[i] = this->_scripts[i];
216 void SALOME_ContainerPerfLog::accept(SALOME_VisitorContainerLog &visitor)
218 visitor.enterContainerPerfLog( *this );
219 for(auto script : _scripts)
221 PortableServer::ServantBase *serv = getPOA()->reference_to_servant( script );
223 SALOME_ContainerScriptPerfLog *servC = dynamic_cast<SALOME_ContainerScriptPerfLog *>(serv);
224 servC->accept(visitor);
226 visitor.leaveContainerPerfLog( *this );
229 char *SALOME_ContainerPerfLog::getLogFile()
231 return CORBA::string_dup( this->_log_file.c_str() );
234 char *SALOME_ContainerPerfLog::getContainerEntryInNS()
236 return CORBA::string_dup( this->_name_in_ns.c_str() );
241 SALOME_LogManager::SALOME_LogManager(CORBA::ORB_ptr orb, PortableServer::POA_var poa,SALOME_NamingService_Abstract *ns):_poa(poa)
244 PortableServer::ObjectId_var id(_poa->activate_object(this));
245 CORBA::Object_var obj(_poa->id_to_reference(id));
246 Engines::LogManager_var refPtr(Engines::LogManager::_narrow(obj));
247 _NS->Register(refPtr,NAME_IN_NS);
250 std::ostringstream myCommand;
251 myCommand << "mylogmanager = SALOME_LogManager.SALOME_LogManagerHelper()";
252 PyRun_SimpleString("import SALOME_LogManager");
253 PyRun_SimpleString((char*)myCommand.str().c_str());
254 PyObject *mainmod = PyImport_AddModule("__main__");
255 PyObject *globals = PyModule_GetDict(mainmod);
256 _pyLogManager = PyDict_GetItemString(globals, "mylogmanager");
260 Engines::ContainerPerfLog_ptr SALOME_LogManager::declareContainer(const char *contInNS, const char *logfile)
262 SALOME_ContainerPerfLog *cont = new SALOME_ContainerPerfLog(this,contInNS,logfile);
263 PortableServer::ObjectId_var id(_poa->activate_object(cont));
264 CORBA::Object_var contPtr(_poa->id_to_reference(id));
266 Engines::ContainerPerfLog_var contPtr2 = Engines::ContainerPerfLog::_narrow(contPtr);
267 _containers.push_back( contPtr2 );
270 PyObject *result = PyObject_CallMethod(_pyLogManager,(char*)"declareContainer","ss",contInNS,logfile,nullptr);
271 if (PyErr_Occurred())
273 std::string error("can not declareContainer");
275 THROW_SALOME_CORBA_EXCEPTION(error.c_str(),SALOME::INTERNAL_ERROR);
277 cont->setPyObj( result );
279 return contPtr2._retn();
282 Engines::ListOfContainerPerfLog *SALOME_LogManager::listOfContainerLogs()
284 Engines::ListOfContainerPerfLog_var ret = new Engines::ListOfContainerPerfLog;
285 std::size_t len( this->_containers.size() );
287 for(std::size_t i = 0 ; i < len ; ++i)
289 ret[i] = this->_containers[i];
294 void SALOME_LogManager::accept(SALOME_VisitorContainerLog &visitor)
296 visitor.enterLogManager( *this );
297 for(auto container : _containers)
299 PortableServer::ServantBase *serv = getPOA()->reference_to_servant( container );
301 SALOME_ContainerPerfLog *servC = dynamic_cast<SALOME_ContainerPerfLog *>(serv);
302 servC->accept(visitor);
304 visitor.leaveLogManager( *this );
308 \param [in] unloadMemory - specify if big part of struct data (SALOME_ContainerScriptExecPerfLog) is cleared after retrieving data
310 SALOME::vectorOfByte *SALOME_LogManager::getAllStruct(bool clearMemory)
312 std::vector<char> data = this->dumpCppInternalFrmt(clearMemory);
313 return FromVectCharToCorba(data);
316 ///////////////////////
320 static void PushIntInVC(std::uint32_t val, std::vector<char>& data)
322 char *valPtr = reinterpret_cast<char *>(&val);
323 data.insert(data.end(),valPtr,valPtr+sizeof(std::uint32_t));
327 static void PushStringInVC(const T& str, std::vector<char>& data)
329 std::uint32_t sz = static_cast<std::uint32_t>( str.size() );
330 PushIntInVC(sz,data);
331 data.insert(data.end(),str.data(),str.data()+sz);
334 class InternalFormatVisitorDump : public SALOME_VisitorContainerLog
337 InternalFormatVisitorDump(bool clearMemory, std::vector<char> *data):_clear_memory(clearMemory),_data(data) { }
338 void enterLogManager(SALOME_LogManager& inst) override;
339 void leaveLogManager(SALOME_LogManager& inst) override { }
340 void enterContainerPerfLog(SALOME_ContainerPerfLog& inst) override;
341 void leaveContainerPerfLog(SALOME_ContainerPerfLog& inst) override { }
342 void enterContainerScriptPerfLog(SALOME_ContainerScriptPerfLog& inst) override;
343 void leaveContainerScriptPerfLog(SALOME_ContainerScriptPerfLog& inst) override { }
344 void visitContainerScriptExecPerfLog(SALOME_ContainerScriptExecPerfLog& inst) override;
346 bool _clear_memory = false;
347 std::vector<char> *_data = nullptr;
350 void InternalFormatVisitorDump::visitContainerScriptExecPerfLog(SALOME_ContainerScriptExecPerfLog& inst)
352 PushStringInVC<std::vector<char>>(inst.data(),*_data);
357 void InternalFormatVisitorDump::enterContainerScriptPerfLog(SALOME_ContainerScriptPerfLog& inst)
359 PushStringInVC<std::string>(inst.name(),*_data);
360 PushStringInVC<std::string>(inst.code(),*_data);
361 PushIntInVC((std::uint32_t)inst.getNumberOfSessions(),*_data);
364 void InternalFormatVisitorDump::enterContainerPerfLog(SALOME_ContainerPerfLog& inst)
366 PushStringInVC<std::string>(inst.nameInNS(),*_data);
367 PushStringInVC<std::string>(inst.logFile(),*_data);
368 PushIntInVC((std::uint32_t)inst.getNumberOfScripts(),*_data);
371 void InternalFormatVisitorDump::enterLogManager(SALOME_LogManager& inst)
373 PushIntInVC((std::uint32_t)inst.getNumberOfContainers(),*_data);
376 std::vector<char> SALOME_LogManager::dumpCppInternalFrmt(bool clearMemory)
378 std::vector<char> ret;
379 InternalFormatVisitorDump visitor(clearMemory,&ret);
380 this->accept( visitor );