PhoenixPresentation  2.0.0
Set of cmake function to automate presentation generation
PLog.h
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 #ifndef __PLOG_H__
8 #define __PLOG_H__
9 
10 #include <string>
11 #include <iostream>
12 #include <fstream>
13 #include <sstream>
14 #include <vector>
15 
16 #include "PPath.h"
17 
18 class PLog;
19 
21 class PLog{
22  public:
24  enum Mode{
29  DISABLE
30  };
32  enum Level{
33  DEBUG = 1,
34  INFO = 2,
35  WARNING = 3,
36  ERROR = 4,
37  CRITICAL = 5,
38  ALWAYS = 6
39  };
40  PLog();
41  virtual ~PLog();
42 
43  void setFileName(const PPath & fileName);
44  void setMode(PLog::Mode mode);
45  void setLogLevel(PLog::Level logLevel);
46  void setThreadIndex(size_t threadIndex);
47  void resize(size_t nbThread);
48  bool open();
49  void close();
50  void clear();
51  void appendLog(std::stringstream & str);
52 
53  PLog & getLog(size_t threadIndex);
54  std::ofstream & getLogFile();
55  std::stringstream & getLogString();
56  std::ostream & getLog(PLog::Level logLevel = PLog::INFO);
57  std::ostream & getLogDebug();
58  std::ostream & getLogInfo();
59  std::ostream & getLogWarning();
60  std::ostream & getLogError();
61  std::ostream & getLogCritical();
62  std::ostream & getLogAlways();
63 
64  const PPath & getFileName() const;
65  PLog::Mode getMode() const;
66  PLog::Level getLogLevel() const;
67  size_t getThreadIndex() const;
68  bool isOpen() const;
69 
70  private:
71  void initialisationPLog();
72  void allocateStream(std::streambuf* buffer);
73  bool streamOpen();
74 
82  std::ofstream p_logFile;
84  std::stringstream p_logString;
86  std::ostream * p_stream;
88  std::ostream * p_nullStream;
90  std::streambuf* p_oldStdCerrBuffer;
92  std::streambuf* p_oldStdCoutBuffer;
94  std::vector<PLog*> p_vecLog;
96  bool p_isOpen;
98  size_t p_threadIndex;
99 };
100 
103 
104 #endif
105 
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
Extends the std::string.
Definition: PString.h:16