PhoenixPresentation  2.0.0
Set of cmake function to automate presentation generation
PLog.cpp
Go to the documentation of this file.
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 
12 
16  switch(logLevel){
17  case PLog::INFO:
18  return "INFO";
19  case PLog::WARNING:
20  return "WARNING";
21  case PLog::ERROR:
22  return "ERROR";
23  case PLog::CRITICAL:
24  return "CRITICAL";
25  case PLog::ALWAYS:
26  return "ALWAYS";
27  default:
28  return "DEBUG";
29  }
30 }
31 
33 
37  if(str == "DEBUG"){return PLog::DEBUG;}
38  else if(str == "WARNING"){return PLog::WARNING;}
39  else if(str == "ERROR"){return PLog::ERROR;}
40  else if(str == "CRITICAL"){return PLog::CRITICAL;}
41  else if(str == "ALWAYS"){return PLog::ALWAYS;}
42  else{return PLog::INFO;}
43 }
44 
48 }
49 
52  close();
53  clear();
54  delete p_nullStream;
55 }
56 
58 
60 void PLog::setFileName(const PPath & fileName){
61  p_fileName = fileName;
62 }
63 
65 
68  p_mode = mode;
69 }
70 
72 
75  p_logLevel = logLevel;
76 }
77 
79 
81 void PLog::setThreadIndex(size_t threadIndex){
82  p_threadIndex = threadIndex;
83 }
84 
86 
88 void PLog::resize(size_t nbThread){
89  clear();
90  p_vecLog.resize(nbThread);
91  PString baseFileName(p_fileName.eraseExtension());
92  PString extention(p_fileName.getExtension());
93  for(size_t i(0lu); i < nbThread; ++i){
94  p_vecLog[i] = new PLog;
95  p_vecLog[i]->setFileName(PString(baseFileName + "_" + PString(valueToString(i)) + "." + extention));
96  p_vecLog[i]->setMode(p_mode);
97  p_vecLog[i]->setLogLevel(p_logLevel);
98  }
99 }
100 
102 
104 bool PLog::open(){
105  bool b(true);
106  b &= streamOpen();
107  p_isOpen = b;
108  if(b){
109  getLogAlways() << "[UTC][Date][ThreadIndex][LogLevel] : log message" << std::endl;
110  getLogAlways() << "Start logging at " << phoenix_getDate() << std::endl;
111  getLogAlways() << "Current logging level '"<<phoenix_logLevelToStr(getLogLevel())<<"'" << std::endl;
112  }
113  for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
114  PLog* log = *it;
115  if(log != NULL){
116  b &= log->open();
117  }
118  }
119  return b;
120 }
121 
123 void PLog::close(){
124  if(p_stream != NULL && p_isOpen){
125  getLogAlways() << "Close Log File at " << phoenix_getDate() << std::endl;
126  }
127  if(p_logFile.is_open()){
128  p_logFile.close();
129  }
130 // if(p_mode == PLog::STRING_ONLY){
131 // p_logString.close();
132 // }
133  if(p_oldStdCerrBuffer != NULL){
134  std::cerr.rdbuf(p_oldStdCerrBuffer); //Let's get back to previous std::cerr buffer
135  }
136  if(p_oldStdCoutBuffer != NULL){
137  std::cout.rdbuf(p_oldStdCoutBuffer); //Let's get back to previous std::cout buffer
138  }
139  if(p_stream != NULL){
140  delete p_stream;
141  p_stream = NULL;
142  }
143  p_isOpen = false;
144  for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
145  PLog* log = *it;
146  if(log != NULL){
147  log->close();
148  }
149  }
150 }
151 
153 void PLog::clear(){
154  if(p_vecLog.size() != 0lu){
155  for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
156  PLog* log = *it;
157  if(log != NULL){
158  delete log;
159  }
160  }
161  p_vecLog.clear();
162  }
163 }
164 
166 
168 void PLog::appendLog(std::stringstream & str){
169  getLog(PLog::ALWAYS) << "Append log" << std::endl << str.str();
170 }
171 
173 
175 PLog & PLog::getLog(size_t threadIndex){
176  return *(p_vecLog[threadIndex]);
177 }
178 
180 
182 std::ofstream & PLog::getLogFile(){
183  return p_logFile;
184 }
185 
187 
189 std::stringstream & PLog::getLogString(){
190  return p_logString;
191 }
192 
194 
197 std::ostream & PLog::getLog(PLog::Level logLevel){
198  if(logLevel >= p_logLevel){
199  *p_stream << "[" << phoenix_getTime() << "][" << phoenix_getDateCompact() << "][" << p_threadIndex << "]["<<phoenix_logLevelToStr(logLevel)<<"] : ";
200  return *p_stream;
201  }else{
202  return *p_nullStream;
203  }
204 }
205 
207 
209 std::ostream & PLog::getLogDebug(){
210  return getLog(PLog::DEBUG);
211 }
212 
214 
216 std::ostream & PLog::getLogInfo(){
217  return getLog(PLog::INFO);
218 }
219 
221 
223 std::ostream & PLog::getLogWarning(){
224  return getLog(PLog::WARNING);
225 }
226 
228 
230 std::ostream & PLog::getLogError(){
231  return getLog(PLog::ERROR);
232 }
233 
235 
237 std::ostream & PLog::getLogCritical(){
238  return getLog(PLog::CRITICAL);
239 }
240 
242 
244 std::ostream & PLog::getLogAlways(){
245  return getLog(PLog::ALWAYS);
246 }
247 
248 
250 
252 const PPath & PLog::getFileName() const{
253  return p_fileName;
254 }
255 
257 
260  return p_mode;
261 }
262 
264 
267  return p_logLevel;
268 }
269 
271 
273 size_t PLog::getThreadIndex() const{
274  return p_threadIndex;
275 }
276 
278 
280 bool PLog::isOpen() const{
281  return p_isOpen;
282 }
283 
288  p_oldStdCerrBuffer = NULL;
289  p_oldStdCoutBuffer = NULL;
290  p_isOpen = false;
291  p_stream = NULL;
292  p_nullStream = new std::ostream(NULL);
293  p_threadIndex = 0lu;
294 }
295 
297 
299 void PLog::allocateStream(std::streambuf* buffer){
300  if(p_stream != NULL){
301  delete p_stream;
302  }
303  p_stream = new std::ostream(buffer);
304 }
305 
307 
310  bool b(true);
311  if(p_mode == PLog::FILE_ONLY){
312  p_logFile.open(p_fileName);
313  b &= p_logFile.is_open();
314  if(b){
315  allocateStream(p_logFile.rdbuf());
316  }
317  }else if(p_mode == PLog::STRING_ONLY){
318  std::cerr << "PLog::streamOpen : p_logString.rdbuf() = " << p_logString.rdbuf() << std::endl;
319  allocateStream(p_logString.rdbuf());
321  p_logFile.open(p_fileName);
322  b &= p_logFile.is_open();
323  if(b){
324  p_oldStdCerrBuffer = std::cerr.rdbuf(p_logFile.rdbuf());
325  p_oldStdCoutBuffer = std::cout.rdbuf(p_logFile.rdbuf());
326  allocateStream(p_logFile.rdbuf());
327  }
328  }else if(p_mode == PLog::STDOUT_ONLY){
329  allocateStream(std::cout.rdbuf());
330  }else if(p_mode == PLog::DISABLE){
331  allocateStream(NULL);
332  }
333  return b;
334 }
335 
336 
337 
338 
PString phoenix_logLevelToStr(PLog::Level logLevel)
Convert the log level into a PString.
Definition: PLog.cpp:15
PLog::Level phoenix_strToLogLevel(const PString &str)
Convert a string into a log level.
Definition: PLog.cpp:36
Phoenix Logger.
Definition: PLog.h:21
void setFileName(const PPath &fileName)
Set the output filename of the current PLog.
Definition: PLog.cpp:60
const PPath & getFileName() const
Get the filename of the current log.
Definition: PLog.cpp:252
void setThreadIndex(size_t threadIndex)
Set the thread index of the current PLog.
Definition: PLog.cpp:81
std::ostream & getLogAlways()
Write always message into the PLog.
Definition: PLog.cpp:244
void initialisationPLog()
Initialisation function of the class PLog.
Definition: PLog.cpp:285
PLog::Mode getMode() const
Get the mode of the current PLog.
Definition: PLog.cpp:259
void close()
Close the current PLog and its children.
Definition: PLog.cpp:123
Level
Log level to be used in the logger.
Definition: PLog.h:32
@ CRITICAL
Definition: PLog.h:37
@ INFO
Definition: PLog.h:34
@ DEBUG
Definition: PLog.h:33
@ ERROR
Definition: PLog.h:36
@ ALWAYS
Definition: PLog.h:38
@ WARNING
Definition: PLog.h:35
std::stringstream & getLogString()
Get the log string.
Definition: PLog.cpp:189
std::ostream * p_nullStream
Stream used to disable log output.
Definition: PLog.h:88
PLog::Level p_logLevel
Current log level of the PLog (all log greater or equal to the p_logLevel will be logged)
Definition: PLog.h:78
std::ostream * p_stream
Current stream to be used to log things.
Definition: PLog.h:86
std::ofstream p_logFile
Current log file to be used.
Definition: PLog.h:82
void resize(size_t nbThread)
Resize the number of cihldren log file.
Definition: PLog.cpp:88
void allocateStream(std::streambuf *buffer)
Allocate the stream.
Definition: PLog.cpp:299
std::vector< PLog * > p_vecLog
Vector of sur log file to be used (mainly for multithreaded programs)
Definition: PLog.h:94
size_t getThreadIndex() const
Get the thread index of the current PLog.
Definition: PLog.cpp:273
size_t p_threadIndex
Index of the current thread.
Definition: PLog.h:98
std::ostream & getLogDebug()
Write debug message into the PLog.
Definition: PLog.cpp:209
PPath p_fileName
Output filename of the current PLog.
Definition: PLog.h:80
PLog & getLog(size_t threadIndex)
Get the PLog at given index.
Definition: PLog.cpp:175
std::ostream & getLogInfo()
Write info message into the PLog.
Definition: PLog.cpp:216
void setMode(PLog::Mode mode)
Set the mode of the current PLog.
Definition: PLog.cpp:67
PLog()
Default constructor of PLog.
Definition: PLog.cpp:46
bool isOpen() const
Say if the current PLog is opened or not.
Definition: PLog.cpp:280
virtual ~PLog()
Destructor of PLog.
Definition: PLog.cpp:51
std::ostream & getLogWarning()
Write warning message into the PLog.
Definition: PLog.cpp:223
PLog::Level getLogLevel() const
Get the log level of the current PLog.
Definition: PLog.cpp:266
PLog::Mode p_mode
Mode of the logger.
Definition: PLog.h:76
std::streambuf * p_oldStdCoutBuffer
Old std::cout buffer.
Definition: PLog.h:92
Mode
Mode to be used on the logger.
Definition: PLog.h:24
@ STRING_ONLY
Definition: PLog.h:26
@ FILE_CAPTURE_STDOUT_STDERR
Definition: PLog.h:28
@ FILE_ONLY
Definition: PLog.h:25
@ DISABLE
Definition: PLog.h:29
@ STDOUT_ONLY
Definition: PLog.h:27
bool streamOpen()
Open the streams.
Definition: PLog.cpp:309
std::stringstream p_logString
Log string.
Definition: PLog.h:84
bool p_isOpen
True of the log is opened.
Definition: PLog.h:96
std::streambuf * p_oldStdCerrBuffer
Old std::cerr buffer.
Definition: PLog.h:90
void setLogLevel(PLog::Level logLevel)
Set the log level of the current PLog.
Definition: PLog.cpp:74
bool open()
Open the current PLog and its children.
Definition: PLog.cpp:104
std::ostream & getLogCritical()
Write critical message into the PLog.
Definition: PLog.cpp:237
void appendLog(std::stringstream &str)
Append the log (STRING_ONLY mode) into an other log.
Definition: PLog.cpp:168
void clear()
Clear the children of the current PLog.
Definition: PLog.cpp:153
std::ofstream & getLogFile()
Get the current log file.
Definition: PLog.cpp:182
std::ostream & getLogError()
Write error message into the PLog.
Definition: PLog.cpp:230
Path of a directory or a file.
Definition: PPath.h:17
PPath & eraseExtension()
Erase the extension of the PPath.
Definition: PPath.cpp:292
PString getExtension() const
Get file extension.
Definition: PPath.cpp:252
Extends the std::string.
Definition: PString.h:16
std::string valueToString(const T &val)
Convert a type into a string.
PString phoenix_getDateCompact()
Get the current date.
time_t phoenix_getTime()
Get the current time of the program.
PString phoenix_getDate()
Get the current date.