1 |
|
|
/*************************************** |
2 |
|
|
Auteur : Pierre Aubert |
3 |
|
|
Mail : pierre.aubert@lapp.in2p3.fr |
4 |
|
|
Licence : CeCILL-C |
5 |
|
|
****************************************/ |
6 |
|
|
|
7 |
|
|
#include "convertToString.h" |
8 |
|
|
#include "string_filename.h" |
9 |
|
|
#include "string_system.h" |
10 |
|
|
|
11 |
|
|
#include "PLog.h" |
12 |
|
|
|
13 |
|
|
///Default constructor of PLog |
14 |
✓ |
9 |
PLog::PLog(){ |
15 |
✓ |
9 |
initialisationPLog(); |
16 |
|
9 |
} |
17 |
|
|
|
18 |
|
|
///Destructor of PLog |
19 |
|
26 |
PLog::~PLog(){ |
20 |
|
18 |
close(); |
21 |
|
18 |
clear(); |
22 |
|
26 |
} |
23 |
|
|
|
24 |
|
|
///Set the output filename of the current PLog |
25 |
|
|
/** @param fileName : output filename of the current PLog |
26 |
|
|
*/ |
27 |
|
9 |
void PLog::setFileName(const std::string & fileName){ |
28 |
|
9 |
p_fileName = fileName; |
29 |
|
9 |
} |
30 |
|
|
|
31 |
|
|
///Set the mode of the current PLog |
32 |
|
|
/** @param mode : mode of the current PLog |
33 |
|
|
*/ |
34 |
|
7 |
void PLog::setMode(PLog::Mode mode){ |
35 |
|
7 |
p_mode = mode; |
36 |
|
7 |
} |
37 |
|
|
|
38 |
|
|
///Resize the number of cihldren log file |
39 |
|
|
/** @param nbThread : number of sub log files to be created (typically the number of threads of a program) |
40 |
|
|
*/ |
41 |
|
1 |
void PLog::resize(size_t nbThread){ |
42 |
✓ |
1 |
clear(); |
43 |
✓ |
1 |
p_vecLog.resize(nbThread); |
44 |
✓ |
2 |
std::string baseFileName(eraseExtension(p_fileName)); |
45 |
✓ |
2 |
std::string extention(getExtention(p_fileName)); |
46 |
✓✓ |
5 |
for(size_t i(0lu); i < nbThread; ++i){ |
47 |
✓✓ |
4 |
p_vecLog[i] = new PLog; |
48 |
✓✓✓✓ ✓✓ |
4 |
p_vecLog[i]->setFileName(baseFileName + "_" + convertToString(i) + "." + extention); |
49 |
✓ |
4 |
p_vecLog[i]->setMode(p_mode); |
50 |
|
|
} |
51 |
|
1 |
} |
52 |
|
|
|
53 |
|
|
///Open the current PLog and its children |
54 |
|
|
/** @return true on success, false otherwise |
55 |
|
|
*/ |
56 |
|
9 |
bool PLog::open(){ |
57 |
|
9 |
bool b(true); |
58 |
|
9 |
b &= streamOpen(); |
59 |
|
9 |
p_isOpen = b; |
60 |
✓✗ |
9 |
if(b){ |
61 |
✓✓ |
9 |
getLog() << "Start logging at " << phoenix_getDate() << std::endl; |
62 |
|
|
} |
63 |
✓✓ |
13 |
for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){ |
64 |
|
4 |
PLog* log = *it; |
65 |
✓✗ |
4 |
if(log != NULL){ |
66 |
✓ |
4 |
b &= log->open(); |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
9 |
return b; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
///Close the current PLog and its children |
73 |
|
16 |
void PLog::close(){ |
74 |
✓✓✓✗
|
16 |
if(p_stream != NULL && p_isOpen){ |
75 |
✓✓ |
9 |
getLog() << "Close Log File at " << phoenix_getDate() << std::endl; |
76 |
|
|
} |
77 |
✓✓ |
16 |
if(p_logFile.is_open()){ |
78 |
|
7 |
p_logFile.close(); |
79 |
|
|
} |
80 |
✓✓ |
16 |
if(p_oldStdCerrBuffer != NULL){ |
81 |
|
2 |
std::cerr.rdbuf(p_oldStdCerrBuffer); //Let's get back to previous std::cerr buffer |
82 |
|
|
} |
83 |
✓✓ |
16 |
if(p_oldStdCoutBuffer != NULL){ |
84 |
|
2 |
std::cout.rdbuf(p_oldStdCoutBuffer); //Let's get back to previous std::cout buffer |
85 |
|
|
} |
86 |
✓✓ |
16 |
if(p_stream != NULL){ |
87 |
✓✗ |
9 |
delete p_stream; |
88 |
|
9 |
p_stream = NULL; |
89 |
|
|
} |
90 |
|
16 |
p_isOpen = false; |
91 |
✓✓ |
20 |
for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){ |
92 |
|
4 |
PLog* log = *it; |
93 |
✓✗ |
4 |
if(log != NULL){ |
94 |
✓ |
4 |
log->close(); |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
16 |
} |
98 |
|
|
|
99 |
|
|
///Clear the children of the current PLog |
100 |
|
10 |
void PLog::clear(){ |
101 |
✓✓ |
10 |
if(p_vecLog.size() != 0lu){ |
102 |
✓✓ |
5 |
for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){ |
103 |
|
4 |
PLog* log = *it; |
104 |
✓✗ |
4 |
if(log != NULL){ |
105 |
✓✗ |
4 |
delete log; |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
1 |
p_vecLog.clear(); |
109 |
|
|
} |
110 |
|
10 |
} |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
///Get the PLog at given index |
115 |
|
|
/** @return PLog at Index |
116 |
|
|
*/ |
117 |
|
4 |
PLog & PLog::getLog(size_t threadIndex){ |
118 |
|
4 |
return *(p_vecLog[threadIndex]); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
///Get the current log file |
122 |
|
|
/** @return current log file |
123 |
|
|
*/ |
124 |
|
|
std::ofstream & PLog::getLogFile(){ |
125 |
|
|
return p_logFile; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
///Parenthesis operator to write log into the PLog |
129 |
|
|
/** @return ofstream to be written |
130 |
|
|
*/ |
131 |
|
34 |
std::ostream & PLog::getLog(){ |
132 |
|
|
// std::ofstream & fs = getLogFile(); |
133 |
|
|
// fs << "[" << phoenix_getTime() << "] : "; |
134 |
|
|
// return fs; |
135 |
|
34 |
*p_stream << "[" << phoenix_getTime() << "] : "; |
136 |
|
34 |
return *p_stream; |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
///Get the filename of the current log |
140 |
|
|
/** @return filename of the current log |
141 |
|
|
*/ |
142 |
|
1 |
const std::string & PLog::getFileName() const{ |
143 |
|
1 |
return p_fileName; |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
///Get the mode of the current PLog |
147 |
|
|
/** @return mode of the current PLog |
148 |
|
|
*/ |
149 |
|
|
PLog::Mode PLog::getMode() const{ |
150 |
|
|
return p_mode; |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
///Initialisation function of the class PLog |
154 |
|
9 |
void PLog::initialisationPLog(){ |
155 |
|
9 |
p_mode = PLog::FILE_ONLY; |
156 |
|
9 |
p_oldStdCerrBuffer = NULL; |
157 |
|
9 |
p_oldStdCoutBuffer = NULL; |
158 |
|
9 |
p_isOpen = false; |
159 |
|
9 |
p_stream = NULL; |
160 |
|
9 |
} |
161 |
|
|
|
162 |
|
|
///Allocate the stream |
163 |
|
|
/** @param buffer : buffer to be used |
164 |
|
|
*/ |
165 |
|
9 |
void PLog::allocateStream(std::streambuf* buffer){ |
166 |
✗✓ |
9 |
if(p_stream != NULL){ |
167 |
|
|
delete p_stream; |
168 |
|
|
} |
169 |
✓ |
9 |
p_stream = new std::ostream(buffer); |
170 |
|
9 |
} |
171 |
|
|
|
172 |
|
|
///Open the streams |
173 |
|
|
/** @return true on success, false otherwise |
174 |
|
|
*/ |
175 |
|
9 |
bool PLog::streamOpen(){ |
176 |
|
9 |
bool b(true); |
177 |
✓✓ |
9 |
if(p_mode == PLog::FILE_ONLY){ |
178 |
|
6 |
p_logFile.open(p_fileName); |
179 |
|
6 |
b &= p_logFile.is_open(); |
180 |
✓✗ |
6 |
if(b){ |
181 |
|
6 |
allocateStream(p_logFile.rdbuf()); |
182 |
|
|
} |
183 |
✓✓ |
3 |
}else if(p_mode == PLog::FILE_CAPTURE_STDOUT_STDERR){ |
184 |
|
1 |
p_logFile.open(p_fileName); |
185 |
|
1 |
b &= p_logFile.is_open(); |
186 |
✓✗ |
1 |
if(b){ |
187 |
|
1 |
p_oldStdCerrBuffer = std::cerr.rdbuf(p_logFile.rdbuf()); |
188 |
|
1 |
p_oldStdCoutBuffer = std::cout.rdbuf(p_logFile.rdbuf()); |
189 |
|
1 |
allocateStream(p_logFile.rdbuf()); |
190 |
|
|
} |
191 |
✓✓ |
2 |
}else if(p_mode == PLog::STDOUT_ONLY){ |
192 |
|
1 |
allocateStream(std::cout.rdbuf()); |
193 |
✓✗ |
1 |
}else if(p_mode == PLog::DISABLE){ |
194 |
|
1 |
allocateStream(NULL); |
195 |
|
|
} |
196 |
|
9 |
return b; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
|