Salome HOME
[EDF29150] : Add relevant traces in resource manager + dump of logmanager service
[modules/kernel.git] / src / SALOMELocalTrace / FileTraceCollector.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : FileTraceCollector.cxx
24 //  Author : Paul RASCLE (EDF)
25 //  Module : KERNEL
26 //  $Header$
27 //
28 #include <iostream>
29 #include <sstream>
30 #include <fstream>
31 #include <cstdlib>
32
33 //#define _DEVDEBUG_
34 #include "FileTraceCollector.hxx"
35
36 // Class attributes initialisation, for class method FileTraceCollector::run
37
38 std::string FileTraceCollector::_fileName = "";
39
40 // ============================================================================
41 /*!
42  *  This class is for use without CORBA, inside or outside SALOME.
43  *  SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
44  *  Type of trace (and corresponding class) is chosen in LocalTraceBufferPool.
45  *
46  *  Guarantees a unique object instance of the class (singleton thread safe)
47  *  a separate thread for loop to print traces is launched.
48  */
49 // ============================================================================
50
51 BaseTraceCollector* FileTraceCollector::instance(const char *fileName)
52 {
53   if (_singleton == 0) // no need of lock when singleton already exists
54     {
55       pthread_mutex_lock(&_singletonMutex);    // acquire lock to be alone
56       if (_singleton == 0)                     // another thread may have got
57         {                                      // the lock after the first test
58           DEVTRACE("FileTraceCollector:: instance()");
59           BaseTraceCollector* myInstance = new FileTraceCollector();
60           _fileName = fileName;
61           DEVTRACE(" _fileName: " << _fileName);
62
63           sem_init(&_sem,0,0); // to wait until run thread is initialized
64           pthread_t traceThread;
65           int bid = 0;
66           pthread_create(&traceThread, NULL,
67                                    FileTraceCollector::run, &bid);
68           sem_wait(&_sem);
69           _singleton = myInstance; // _singleton known only when init done
70           DEVTRACE("FileTraceCollector:: instance()-end");
71         }
72       pthread_mutex_unlock(&_singletonMutex); // release lock
73     }
74   return _singleton;
75 }
76
77 // ============================================================================
78 /*!
79  *  In a separate thread, loop to print traces.
80  *  Mutex guarantees initialisation on instance method is done and only one run
81  *  allowed (double check ...)
82  *  Loop until there is no more buffer to print,
83  *  and no ask for end from destructor.
84  *  Get a buffer. If type = ABORT then exit application with message.
85  */
86 // ============================================================================
87
88 void* FileTraceCollector::run(void* /*bid*/)
89 {
90   //DEVTRACE("init run");
91   _threadId = new pthread_t;
92   *_threadId = pthread_self();
93   sem_post(&_sem); // unlock instance
94
95   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
96   LocalTrace_TraceInfo myTrace;
97
98   // --- opens a file with append mode
99   //     so, several processes can share the same file
100
101   std::ofstream traceFile;
102   const char *theFileName = _fileName.c_str();
103   DEVTRACE("try to open trace file "<< theFileName);
104   traceFile.open(theFileName, std::ios::out | std::ios::app);
105   if (!traceFile)
106     {
107       std::cerr << "impossible to open trace file "<< theFileName << std::endl;
108       exit (1);
109     }
110
111   // --- Loop until there is no more buffer to print,
112   //     and no ask for end from destructor.
113   DEVTRACE("Begin loop");
114   while ((!_threadToClose) || myTraceBuffer->toCollect() )
115     {
116       if (_threadToClose)
117         {
118           DEVTRACE("FileTraceCollector _threadToClose");
119           //break;
120         }
121
122       myTraceBuffer->retrieve(myTrace);
123       if (myTrace.traceType == ABORT_MESS)
124         {
125 #ifndef WIN32
126           traceFile << "INTERRUPTION from thread " << myTrace.threadId
127                     << " : " <<  myTrace.trace;
128 #else
129           traceFile << "INTERRUPTION from thread "
130                     << (void*)(&myTrace.threadId)
131                     << " : " <<  myTrace.trace;
132 #endif
133           traceFile.close();
134           std::cout << std::flush ;
135 #ifndef WIN32
136           std::cerr << "INTERRUPTION from thread " << myTrace.threadId
137                << " : " <<  myTrace.trace;
138 #else
139           std::cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
140                << " : " <<  myTrace.trace;
141 #endif
142           std::cerr << std::flush ; 
143           exit(1);     
144         }
145       else
146         {
147 #ifndef WIN32
148           traceFile << "th. " << myTrace.threadId
149                     << " " << myTrace.trace;
150 #else
151           traceFile << "th. " << (void*)(&myTrace.threadId)
152                     << " " << myTrace.trace;
153 #endif
154         }
155     }
156   DEVTRACE("traceFile.close()");
157   traceFile.close();
158   DEVTRACE("traceFile.close()_end");
159   pthread_exit(NULL);
160 }
161
162 // ============================================================================
163 /*!
164  *  Destructor: wait until printing thread ends (FileTraceCollector::run)
165  */
166 // ============================================================================
167
168 FileTraceCollector:: ~FileTraceCollector()
169 {
170   pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
171   if (_singleton)
172     {
173       DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
174       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
175       _threadToClose = 1;
176       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
177       if (_threadId)
178         {
179           int ret = pthread_join(*_threadId, NULL);
180           if (ret) { std::cerr << "error close FileTraceCollector : "<< ret << std::endl; }
181           else { DEVTRACE("FileTraceCollector destruction OK"); }
182           delete _threadId;
183           _threadId = 0;
184           _threadToClose = 0;
185         }
186       _singleton = 0;
187     }
188   pthread_mutex_unlock(&_singletonMutex); // release lock
189 }
190
191 // ============================================================================
192 /*!
193  * Constructor: no need of LocalTraceBufferPool object initialization here,
194  * thread safe singleton used in LocalTraceBufferPool::instance()
195  */
196 // ============================================================================
197
198 FileTraceCollector::FileTraceCollector()
199 {
200   _threadId=0;
201   _threadToClose = 0;
202 }
203
204