GCC Code Coverage Report


Directory: ./
File: tmp_project/PhoenixCore/src/PLog.cpp
Date: 2025-03-14 11:56:07
Exec Total Coverage
Lines: 159 164 97.0%
Branches: 98 112 87.5%

Line Branch Exec Source
1 /***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5 ****************************************/
6
7 #include "convertToString.h"
8 #include "phoenix_system.h"
9 #include "PLog.h"
10
11 ///Convert the log level into a PString
12 /** @param logLevel : log level to be converted
13 * @return corresponding PString
14 */
15 107 PString phoenix_logLevelToStr(PLog::Level logLevel){
16
6/6
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 50 times.
✓ Branch 5 taken 4 times.
107 switch(logLevel){
17 38 case PLog::INFO:
18 38 return "INFO";
19 5 case PLog::WARNING:
20 5 return "WARNING";
21 5 case PLog::ERROR:
22 5 return "ERROR";
23 5 case PLog::CRITICAL:
24 5 return "CRITICAL";
25 50 case PLog::ALWAYS:
26 50 return "ALWAYS";
27 4 default:
28 4 return "DEBUG";
29 }
30 }
31
32 ///Convert a string into a log level
33 /** @param str : string ot be converted
34 * @return corresponding PLog::Level
35 */
36 6 PLog::Level phoenix_strToLogLevel(const PString & str){
37
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
6 if(str == "DEBUG"){return PLog::DEBUG;}
38
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
5 else if(str == "WARNING"){return PLog::WARNING;}
39
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
4 else if(str == "ERROR"){return PLog::ERROR;}
40
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 else if(str == "CRITICAL"){return PLog::CRITICAL;}
41
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 else if(str == "ALWAYS"){return PLog::ALWAYS;}
42 1 else{return PLog::INFO;}
43 }
44
45 ///Default constructor of PLog
46
2/2
✓ Branch 2 taken 12 times.
✓ Branch 5 taken 12 times.
12 PLog::PLog(){
47
1/1
✓ Branch 1 taken 12 times.
12 initialisationPLog();
48 12 }
49
50 ///Destructor of PLog
51 32 PLog::~PLog(){
52 24 close();
53 24 clear();
54
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
24 delete p_nullStream;
55 32 }
56
57 ///Set the output filename of the current PLog
58 /** @param fileName : output filename of the current PLog
59 */
60 11 void PLog::setFileName(const PPath & fileName){
61 11 p_fileName = fileName;
62 11 }
63
64 ///Set the mode of the current PLog
65 /** @param mode : mode of the current PLog
66 */
67 9 void PLog::setMode(PLog::Mode mode){
68 9 p_mode = mode;
69 9 }
70
71 ///Set the log level of the current PLog
72 /** @param logLevel : log level of the current PLog
73 */
74 6 void PLog::setLogLevel(PLog::Level logLevel){
75 6 p_logLevel = logLevel;
76 6 }
77
78 ///Set the thread index of the current PLog
79 /** @param threadIndex : thread index of the current PLog
80 */
81 2 void PLog::setThreadIndex(size_t threadIndex){
82 2 p_threadIndex = threadIndex;
83 2 }
84
85 ///Resize the number of cihldren log file
86 /** @param nbThread : number of sub log files to be created (typically the number of threads of a program)
87 */
88 1 void PLog::resize(size_t nbThread){
89
1/1
✓ Branch 1 taken 1 times.
1 clear();
90
1/1
✓ Branch 1 taken 1 times.
1 p_vecLog.resize(nbThread);
91
2/2
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
1 PString baseFileName(p_fileName.eraseExtension());
92
1/1
✓ Branch 1 taken 1 times.
1 PString extention(p_fileName.getExtension());
93
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for(size_t i(0lu); i < nbThread; ++i){
94
2/2
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
4 p_vecLog[i] = new PLog;
95
9/9
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 4 times.
✓ Branch 11 taken 4 times.
✓ Branch 14 taken 4 times.
✓ Branch 17 taken 4 times.
✓ Branch 20 taken 4 times.
✓ Branch 23 taken 4 times.
✓ Branch 26 taken 4 times.
4 p_vecLog[i]->setFileName(PString(baseFileName + "_" + PString(valueToString(i)) + "." + extention));
96
1/1
✓ Branch 2 taken 4 times.
4 p_vecLog[i]->setMode(p_mode);
97
1/1
✓ Branch 2 taken 4 times.
4 p_vecLog[i]->setLogLevel(p_logLevel);
98 }
99 1 }
100
101 ///Open the current PLog and its children
102 /** @return true on success, false otherwise
103 */
104 12 bool PLog::open(){
105 12 bool b(true);
106 12 b &= streamOpen();
107 12 p_isOpen = b;
108
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if(b){
109 12 getLogAlways() << "[UTC][Date][ThreadIndex][LogLevel] : log message" << std::endl;
110
2/2
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 12 times.
12 getLogAlways() << "Start logging at " << phoenix_getDate() << std::endl;
111
3/3
✓ Branch 5 taken 12 times.
✓ Branch 8 taken 12 times.
✓ Branch 11 taken 12 times.
12 getLogAlways() << "Current logging level '"<<phoenix_logLevelToStr(getLogLevel())<<"'" << std::endl;
112 }
113
2/2
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 12 times.
16 for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
114 4 PLog* log = *it;
115
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if(log != NULL){
116
1/1
✓ Branch 1 taken 4 times.
4 b &= log->open();
117 }
118 }
119 12 return b;
120 }
121
122 ///Close the current PLog and its children
123 21 void PLog::close(){
124
3/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
21 if(p_stream != NULL && p_isOpen){
125
2/2
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 12 times.
12 getLogAlways() << "Close Log File at " << phoenix_getDate() << std::endl;
126 }
127
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 12 times.
21 if(p_logFile.is_open()){
128 9 p_logFile.close();
129 }
130 // if(p_mode == PLog::STRING_ONLY){
131 // p_logString.close();
132 // }
133
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
21 if(p_oldStdCerrBuffer != NULL){
134 2 std::cerr.rdbuf(p_oldStdCerrBuffer); //Let's get back to previous std::cerr buffer
135 }
136
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 19 times.
21 if(p_oldStdCoutBuffer != NULL){
137 2 std::cout.rdbuf(p_oldStdCoutBuffer); //Let's get back to previous std::cout buffer
138 }
139
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
21 if(p_stream != NULL){
140
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 delete p_stream;
141 12 p_stream = NULL;
142 }
143 21 p_isOpen = false;
144
2/2
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 21 times.
25 for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
145 4 PLog* log = *it;
146
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if(log != NULL){
147
1/1
✓ Branch 1 taken 4 times.
4 log->close();
148 }
149 }
150 21 }
151
152 ///Clear the children of the current PLog
153 13 void PLog::clear(){
154
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 12 times.
13 if(p_vecLog.size() != 0lu){
155
2/2
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 1 times.
5 for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
156 4 PLog* log = *it;
157
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if(log != NULL){
158
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 delete log;
159 }
160 }
161 1 p_vecLog.clear();
162 }
163 13 }
164
165 ///Append the log (STRING_ONLY mode) into an other log
166 /** @param str : log string to be appended
167 */
168 1 void PLog::appendLog(std::stringstream & str){
169
1/1
✓ Branch 5 taken 1 times.
1 getLog(PLog::ALWAYS) << "Append log" << std::endl << str.str();
170 1 }
171
172 ///Get the PLog at given index
173 /** @return PLog at Index
174 */
175 4 PLog & PLog::getLog(size_t threadIndex){
176 4 return *(p_vecLog[threadIndex]);
177 }
178
179 ///Get the current log file
180 /** @return current log file
181 */
182 std::ofstream & PLog::getLogFile(){
183 return p_logFile;
184 }
185
186 ///Get the log string
187 /** @return log string
188 */
189 1 std::stringstream & PLog::getLogString(){
190 1 return p_logString;
191 }
192
193 ///Write log into the PLog
194 /** @param logLevel : log level of the current line
195 * @return ofstream to be written
196 */
197 91 std::ostream & PLog::getLog(PLog::Level logLevel){
198
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 2 times.
91 if(logLevel >= p_logLevel){
199
7/7
✓ Branch 6 taken 89 times.
✓ Branch 9 taken 89 times.
✓ Branch 12 taken 89 times.
✓ Branch 15 taken 89 times.
✓ Branch 18 taken 89 times.
✓ Branch 21 taken 89 times.
✓ Branch 24 taken 89 times.
89 *p_stream << "[" << phoenix_getTime() << "][" << phoenix_getDateCompact() << "][" << p_threadIndex << "]["<<phoenix_logLevelToStr(logLevel)<<"] : ";
200 89 return *p_stream;
201 }else{
202 2 return *p_nullStream;
203 }
204 }
205
206 ///Write debug message into the PLog
207 /** @return ofstream to be written
208 */
209 2 std::ostream & PLog::getLogDebug(){
210 2 return getLog(PLog::DEBUG);
211 }
212
213 ///Write info message into the PLog
214 /** @return ofstream to be written
215 */
216 6 std::ostream & PLog::getLogInfo(){
217 6 return getLog(PLog::INFO);
218 }
219
220 ///Write warning message into the PLog
221 /** @return ofstream to be written
222 */
223 2 std::ostream & PLog::getLogWarning(){
224 2 return getLog(PLog::WARNING);
225 }
226
227 ///Write error message into the PLog
228 /** @return ofstream to be written
229 */
230 2 std::ostream & PLog::getLogError(){
231 2 return getLog(PLog::ERROR);
232 }
233
234 ///Write critical message into the PLog
235 /** @return ofstream to be written
236 */
237 2 std::ostream & PLog::getLogCritical(){
238 2 return getLog(PLog::CRITICAL);
239 }
240
241 ///Write always message into the PLog
242 /** @return ofstream to be written
243 */
244 48 std::ostream & PLog::getLogAlways(){
245 48 return getLog(PLog::ALWAYS);
246 }
247
248
249 ///Get the filename of the current log
250 /** @return filename of the current log
251 */
252 2 const PPath & PLog::getFileName() const{
253 2 return p_fileName;
254 }
255
256 ///Get the mode of the current PLog
257 /** @return mode of the current PLog
258 */
259 PLog::Mode PLog::getMode() const{
260 return p_mode;
261 }
262
263 ///Get the log level of the current PLog
264 /** @return log level of the current PLog
265 */
266 12 PLog::Level PLog::getLogLevel() const{
267 12 return p_logLevel;
268 }
269
270 ///Get the thread index of the current PLog
271 /** @return thread index of the current PLog
272 */
273 1 size_t PLog::getThreadIndex() const{
274 1 return p_threadIndex;
275 }
276
277 ///Say if the current PLog is opened or not
278 /** @return true if the current PLog is opened, false otherwise
279 */
280 1 bool PLog::isOpen() const{
281 1 return p_isOpen;
282 }
283
284 ///Initialisation function of the class PLog
285 12 void PLog::initialisationPLog(){
286 12 p_mode = PLog::FILE_ONLY;
287 12 p_logLevel = PLog::INFO;
288 12 p_oldStdCerrBuffer = NULL;
289 12 p_oldStdCoutBuffer = NULL;
290 12 p_isOpen = false;
291 12 p_stream = NULL;
292
1/1
✓ Branch 2 taken 12 times.
12 p_nullStream = new std::ostream(NULL);
293 12 p_threadIndex = 0lu;
294 12 }
295
296 ///Allocate the stream
297 /** @param buffer : buffer to be used
298 */
299 12 void PLog::allocateStream(std::streambuf* buffer){
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if(p_stream != NULL){
301 delete p_stream;
302 }
303
1/1
✓ Branch 2 taken 12 times.
12 p_stream = new std::ostream(buffer);
304 12 }
305
306 ///Open the streams
307 /** @return true on success, false otherwise
308 */
309 12 bool PLog::streamOpen(){
310 12 bool b(true);
311
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if(p_mode == PLog::FILE_ONLY){
312 8 p_logFile.open(p_fileName);
313 8 b &= p_logFile.is_open();
314
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if(b){
315 8 allocateStream(p_logFile.rdbuf());
316 }
317
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 }else if(p_mode == PLog::STRING_ONLY){
318 1 std::cerr << "PLog::streamOpen : p_logString.rdbuf() = " << p_logString.rdbuf() << std::endl;
319 1 allocateStream(p_logString.rdbuf());
320
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 }else if(p_mode == PLog::FILE_CAPTURE_STDOUT_STDERR){
321 1 p_logFile.open(p_fileName);
322 1 b &= p_logFile.is_open();
323
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(b){
324 1 p_oldStdCerrBuffer = std::cerr.rdbuf(p_logFile.rdbuf());
325 1 p_oldStdCoutBuffer = std::cout.rdbuf(p_logFile.rdbuf());
326 1 allocateStream(p_logFile.rdbuf());
327 }
328
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 }else if(p_mode == PLog::STDOUT_ONLY){
329 1 allocateStream(std::cout.rdbuf());
330
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 }else if(p_mode == PLog::DISABLE){
331 1 allocateStream(NULL);
332 }
333 12 return b;
334 }
335
336
337
338
339