PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
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 "string_filename.h"
9 #include "string_system.h"
10 
11 #include "PLog.h"
12 
14 
17 std::string phoenix_logLevelToStr(PLog::Level logLevel){
18  switch(logLevel){
19  case PLog::INFO:
20  return "INFO";
21  case PLog::WARNING:
22  return "WARNING";
23  case PLog::ERROR:
24  return "ERROR";
25  case PLog::CRITICAL:
26  return "CRITICAL";
27  case PLog::ALWAYS:
28  return "ALWAYS";
29  default:
30  return "DEBUG";
31  }
32 }
33 
35 
38 PLog::Level phoenix_strToLogLevel(const std::string & str){
39  if(str == "DEBUG"){return PLog::DEBUG;}
40  else if(str == "WARNING"){return PLog::WARNING;}
41  else if(str == "ERROR"){return PLog::ERROR;}
42  else if(str == "CRITICAL"){return PLog::CRITICAL;}
43  else if(str == "ALWAYS"){return PLog::ALWAYS;}
44  else{return PLog::INFO;}
45 }
46 
50 }
51 
54  close();
55  clear();
56  delete p_nullStream;
57 }
58 
60 
62 void PLog::setFileName(const std::string & fileName){
63  p_fileName = fileName;
64 }
65 
67 
70  p_mode = mode;
71 }
72 
74 
77  p_logLevel = logLevel;
78 }
79 
81 
83 void PLog::setThreadIndex(size_t threadIndex){
84  p_threadIndex = threadIndex;
85 }
86 
88 
90 void PLog::resize(size_t nbThread){
91  clear();
92  p_vecLog.resize(nbThread);
93  std::string baseFileName(eraseExtension(p_fileName));
94  std::string extention(getExtention(p_fileName));
95  for(size_t i(0lu); i < nbThread; ++i){
96  p_vecLog[i] = new PLog;
97  p_vecLog[i]->setFileName(baseFileName + "_" + convertToString(i) + "." + extention);
98  p_vecLog[i]->setMode(p_mode);
99  p_vecLog[i]->setLogLevel(p_logLevel);
100  }
101 }
102 
104 
106 bool PLog::open(){
107  bool b(true);
108  b &= streamOpen();
109  p_isOpen = b;
110  if(b){
111  getLogAlways() << "[UTC][Date][ThreadIndex][LogLevel] : log message" << std::endl;
112  getLogAlways() << "Start logging at " << phoenix_getDate() << std::endl;
113  getLogAlways() << "Current logging level '"<<phoenix_logLevelToStr(getLogLevel())<<"'" << std::endl;
114  }
115  for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
116  PLog* log = *it;
117  if(log != NULL){
118  b &= log->open();
119  }
120  }
121  return b;
122 }
123 
125 void PLog::close(){
126  if(p_stream != NULL && p_isOpen){
127  getLogAlways() << "Close Log File at " << phoenix_getDate() << std::endl;
128  }
129  if(p_logFile.is_open()){
130  p_logFile.close();
131  }
132 // if(p_mode == PLog::STRING_ONLY){
133 // p_logString.close();
134 // }
135  if(p_oldStdCerrBuffer != NULL){
136  std::cerr.rdbuf(p_oldStdCerrBuffer); //Let's get back to previous std::cerr buffer
137  }
138  if(p_oldStdCoutBuffer != NULL){
139  std::cout.rdbuf(p_oldStdCoutBuffer); //Let's get back to previous std::cout buffer
140  }
141  if(p_stream != NULL){
142  delete p_stream;
143  p_stream = NULL;
144  }
145  p_isOpen = false;
146  for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
147  PLog* log = *it;
148  if(log != NULL){
149  log->close();
150  }
151  }
152 }
153 
155 void PLog::clear(){
156  if(p_vecLog.size() != 0lu){
157  for(std::vector<PLog*>::iterator it(p_vecLog.begin()); it != p_vecLog.end(); ++it){
158  PLog* log = *it;
159  if(log != NULL){
160  delete log;
161  }
162  }
163  p_vecLog.clear();
164  }
165 }
166 
168 
170 void PLog::appendLog(std::stringstream & str){
171  getLog(PLog::ALWAYS) << "Append log" << std::endl << str.str();
172 }
173 
175 
177 PLog & PLog::getLog(size_t threadIndex){
178  return *(p_vecLog[threadIndex]);
179 }
180 
182 
184 std::ofstream & PLog::getLogFile(){
185  return p_logFile;
186 }
187 
189 
191 std::stringstream & PLog::getLogString(){
192  return p_logString;
193 }
194 
196 
199 std::ostream & PLog::getLog(PLog::Level logLevel){
200  if(logLevel >= p_logLevel){
201  *p_stream << "[" << phoenix_getTime() << "][" << phoenix_getDateCompact() << "][" << p_threadIndex << "]["<<phoenix_logLevelToStr(logLevel)<<"] : ";
202  return *p_stream;
203  }else{
204  return *p_nullStream;
205  }
206 }
207 
209 
211 std::ostream & PLog::getLogDebug(){
212  return getLog(PLog::DEBUG);
213 }
214 
216 
218 std::ostream & PLog::getLogInfo(){
219  return getLog(PLog::INFO);
220 }
221 
223 
225 std::ostream & PLog::getLogWarning(){
226  return getLog(PLog::WARNING);
227 }
228 
230 
232 std::ostream & PLog::getLogError(){
233  return getLog(PLog::ERROR);
234 }
235 
237 
239 std::ostream & PLog::getLogCritical(){
240  return getLog(PLog::CRITICAL);
241 }
242 
244 
246 std::ostream & PLog::getLogAlways(){
247  return getLog(PLog::ALWAYS);
248 }
249 
250 
252 
254 const std::string & PLog::getFileName() const{
255  return p_fileName;
256 }
257 
259 
262  return p_mode;
263 }
264 
266 
269  return p_logLevel;
270 }
271 
273 
275 size_t PLog::getThreadIndex() const{
276  return p_threadIndex;
277 }
278 
283  p_oldStdCerrBuffer = NULL;
284  p_oldStdCoutBuffer = NULL;
285  p_isOpen = false;
286  p_stream = NULL;
287  p_nullStream = new std::ostream(NULL);
288  p_threadIndex = 0lu;
289 }
290 
292 
294 void PLog::allocateStream(std::streambuf* buffer){
295  if(p_stream != NULL){
296  delete p_stream;
297  }
298  p_stream = new std::ostream(buffer);
299 }
300 
302 
305  bool b(true);
306  if(p_mode == PLog::FILE_ONLY){
307  p_logFile.open(p_fileName);
308  b &= p_logFile.is_open();
309  if(b){
310  allocateStream(p_logFile.rdbuf());
311  }
312  }else if(p_mode == PLog::STRING_ONLY){
313  std::cerr << "PLog::streamOpen : p_logString.rdbuf() = " << p_logString.rdbuf() << std::endl;
314  allocateStream(p_logString.rdbuf());
316  p_logFile.open(p_fileName);
317  b &= p_logFile.is_open();
318  if(b){
319  p_oldStdCerrBuffer = std::cerr.rdbuf(p_logFile.rdbuf());
320  p_oldStdCoutBuffer = std::cout.rdbuf(p_logFile.rdbuf());
321  allocateStream(p_logFile.rdbuf());
322  }
323  }else if(p_mode == PLog::STDOUT_ONLY){
324  allocateStream(std::cout.rdbuf());
325  }else if(p_mode == PLog::DISABLE){
326  allocateStream(NULL);
327  }
328  return b;
329 }
330 
331 
332 
333 
PLog::setThreadIndex
void setThreadIndex(size_t threadIndex)
Set the thread index of the current PLog.
Definition: PLog.cpp:83
convertToString
std::string convertToString(const T &val)
Convert a type into a string.
Definition: convertToString_impl.h:17
PLog.h
PLog::getLog
PLog & getLog(size_t threadIndex)
Get the PLog at given index.
Definition: PLog.cpp:177
PLog::getFileName
const std::string & getFileName() const
Get the filename of the current log.
Definition: PLog.cpp:254
PLog::WARNING
@ WARNING
Definition: PLog.h:33
PLog::initialisationPLog
void initialisationPLog()
Initialisation function of the class PLog.
Definition: PLog.cpp:280
PLog::Mode
Mode
Mode to be used on the logger.
Definition: PLog.h:22
PLog::STDOUT_ONLY
@ STDOUT_ONLY
Definition: PLog.h:25
PLog::CRITICAL
@ CRITICAL
Definition: PLog.h:35
PLog::ERROR
@ ERROR
Definition: PLog.h:34
PLog::DEBUG
@ DEBUG
Definition: PLog.h:31
PLog::getLogError
std::ostream & getLogError()
Write error message into the PLog.
Definition: PLog.cpp:232
PLog::allocateStream
void allocateStream(std::streambuf *buffer)
Allocate the stream.
Definition: PLog.cpp:294
PLog::close
void close()
Close the current PLog and its children.
Definition: PLog.cpp:125
PLog::getLogAlways
std::ostream & getLogAlways()
Write always message into the PLog.
Definition: PLog.cpp:246
PLog::appendLog
void appendLog(std::stringstream &str)
Append the log (STRING_ONLY mode) into an other log.
Definition: PLog.cpp:170
PLog::getLogWarning
std::ostream & getLogWarning()
Write warning message into the PLog.
Definition: PLog.cpp:225
PLog::setFileName
void setFileName(const std::string &fileName)
Set the output filename of the current PLog.
Definition: PLog.cpp:62
phoenix_logLevelToStr
std::string phoenix_logLevelToStr(PLog::Level logLevel)
Convert the log level into a string.
Definition: PLog.cpp:17
PLog::FILE_CAPTURE_STDOUT_STDERR
@ FILE_CAPTURE_STDOUT_STDERR
Definition: PLog.h:26
PLog::p_logFile
std::ofstream p_logFile
Current log file to be used.
Definition: PLog.h:79
PLog::p_isOpen
bool p_isOpen
True of the log is opened.
Definition: PLog.h:93
PLog::clear
void clear()
Clear the children of the current PLog.
Definition: PLog.cpp:155
PLog::getLogLevel
PLog::Level getLogLevel() const
Get the log level of the current PLog.
Definition: PLog.cpp:268
string_filename.h
PLog::setLogLevel
void setLogLevel(PLog::Level logLevel)
Set the log level of the current PLog.
Definition: PLog.cpp:76
PLog::p_oldStdCoutBuffer
std::streambuf * p_oldStdCoutBuffer
Old std::cout buffer.
Definition: PLog.h:89
PLog::getLogDebug
std::ostream & getLogDebug()
Write debug message into the PLog.
Definition: PLog.cpp:211
PLog::p_stream
std::ostream * p_stream
Current stream to be used to log things.
Definition: PLog.h:83
PLog::~PLog
virtual ~PLog()
Destructor of PLog.
Definition: PLog.cpp:53
PLog::getLogInfo
std::ostream & getLogInfo()
Write info message into the PLog.
Definition: PLog.cpp:218
PLog::resize
void resize(size_t nbThread)
Resize the number of cihldren log file.
Definition: PLog.cpp:90
PLog::setMode
void setMode(PLog::Mode mode)
Set the mode of the current PLog.
Definition: PLog.cpp:69
PLog::open
bool open()
Open the current PLog and its children.
Definition: PLog.cpp:106
PLog::streamOpen
bool streamOpen()
Open the streams.
Definition: PLog.cpp:304
eraseExtension
std::string eraseExtension(const std::string &fileName)
Erase extention of the given file.
Definition: string_filename.cpp:368
PLog::p_fileName
std::string p_fileName
Output filename of the current PLog.
Definition: PLog.h:77
PLog::STRING_ONLY
@ STRING_ONLY
Definition: PLog.h:24
PLog::getMode
PLog::Mode getMode() const
Get the mode of the current PLog.
Definition: PLog.cpp:261
PLog::ALWAYS
@ ALWAYS
Definition: PLog.h:36
convertToString.h
PLog::p_nullStream
std::ostream * p_nullStream
Stream used to disable log output.
Definition: PLog.h:85
PLog::p_vecLog
std::vector< PLog * > p_vecLog
Vector of sur log file to be used (mainly for multithreaded programs)
Definition: PLog.h:91
phoenix_getTime
time_t phoenix_getTime()
Get the current time of the program.
Definition: string_system.cpp:445
PLog::getLogFile
std::ofstream & getLogFile()
Get the current log file.
Definition: PLog.cpp:184
PLog::FILE_ONLY
@ FILE_ONLY
Definition: PLog.h:23
createReleaseCurl.str
str
Definition: createReleaseCurl.py:128
PLog::DISABLE
@ DISABLE
Definition: PLog.h:27
PLog::p_oldStdCerrBuffer
std::streambuf * p_oldStdCerrBuffer
Old std::cerr buffer.
Definition: PLog.h:87
phoenix_getDateCompact
std::string phoenix_getDateCompact()
Get the current date.
Definition: string_system.cpp:463
phoenix_getDate
std::string phoenix_getDate()
Get the current date.
Definition: string_system.cpp:452
PLog::getLogString
std::stringstream & getLogString()
Get the log string.
Definition: PLog.cpp:191
PLog
Phoenix Logger.
Definition: PLog.h:19
string_system.h
PLog::p_logLevel
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:75
PLog::p_threadIndex
size_t p_threadIndex
Index of the current thread.
Definition: PLog.h:95
getExtention
std::string getExtention(const std::string &fileName)
Get file extention.
Definition: string_filename.cpp:326
PLog::p_logString
std::stringstream p_logString
Log string.
Definition: PLog.h:81
PLog::getLogCritical
std::ostream & getLogCritical()
Write critical message into the PLog.
Definition: PLog.cpp:239
PLog::Level
Level
Log level to be used in the logger.
Definition: PLog.h:30
PLog::p_mode
PLog::Mode p_mode
Mode of the logger.
Definition: PLog.h:73
PLog::getThreadIndex
size_t getThreadIndex() const
Get the thread index of the current PLog.
Definition: PLog.cpp:275
PLog::INFO
@ INFO
Definition: PLog.h:32
PLog::PLog
PLog()
Default constructor of PLog.
Definition: PLog.cpp:48
phoenix_strToLogLevel
PLog::Level phoenix_strToLogLevel(const std::string &str)
Convert a string into a log level.
Definition: PLog.cpp:38