[380] | 1 | #ifndef __XIOS_LOG_HPP__ |
---|
| 2 | #define __XIOS_LOG_HPP__ |
---|
[300] | 3 | |
---|
[364] | 4 | #include <string> |
---|
[300] | 5 | #include <iostream> |
---|
[369] | 6 | #include <string> |
---|
[2341] | 7 | #include "log_type.hpp" |
---|
[300] | 8 | |
---|
[335] | 9 | namespace xios |
---|
[300] | 10 | { |
---|
| 11 | using namespace std ; |
---|
| 12 | |
---|
| 13 | class CLog : public ostream |
---|
| 14 | { |
---|
| 15 | public : |
---|
[523] | 16 | CLog(const string& name_, std::streambuf* sBuff = cout.rdbuf()) |
---|
| 17 | : ostream(sBuff), level(0), name(name_), strBuf_(sBuff) {} |
---|
[490] | 18 | CLog& operator()(int l) |
---|
| 19 | { |
---|
| 20 | if (l<=level) |
---|
[300] | 21 | { |
---|
[490] | 22 | rdbuf(strBuf_); |
---|
[347] | 23 | *this<<"-> "<<name<<" : " ; |
---|
[300] | 24 | } |
---|
| 25 | else rdbuf(NULL) ; |
---|
| 26 | return *this; |
---|
| 27 | } |
---|
[2341] | 28 | |
---|
| 29 | CLog& operator()(CLogType& logType) |
---|
| 30 | { |
---|
| 31 | if (logSetting==nullptr) setLogSetting() ; |
---|
| 32 | if (logSetting->isSet(logType)) |
---|
| 33 | { |
---|
| 34 | rdbuf(strBuf_); |
---|
| 35 | *this<<"-> "<<name<<" : " ; |
---|
| 36 | } |
---|
| 37 | else rdbuf(NULL) ; |
---|
| 38 | return *this; |
---|
| 39 | } |
---|
| 40 | |
---|
[490] | 41 | void setLevel(int l) {level=l; } |
---|
[300] | 42 | int getLevel() {return level ;} |
---|
| 43 | bool isActive(void) { if (rdbuf()==NULL) return true ; else return false ;} |
---|
| 44 | bool isActive(int l) {if (l<=level) return true ; else return false ; } |
---|
[2341] | 45 | bool isActive(CLogType& logType) {if (logSetting==nullptr) setLogSetting() ; if (logSetting->isSet(logType)) return true; else return false; } |
---|
[300] | 46 | |
---|
[490] | 47 | public: |
---|
[523] | 48 | //! Write log into a file with its streambuf |
---|
[490] | 49 | void write2File(std::streambuf* sBuff) { changeStreamBuff(sBuff); } |
---|
| 50 | |
---|
[523] | 51 | //! Write log into standard output |
---|
[490] | 52 | void write2StdOut() { changeStreamBuff(cout.rdbuf()); } |
---|
[523] | 53 | |
---|
| 54 | //! Write log into standard error output |
---|
| 55 | void write2StdErr() { changeStreamBuff(cerr.rdbuf()); } |
---|
| 56 | |
---|
[490] | 57 | private: |
---|
| 58 | /*! |
---|
| 59 | * \brief Change current streambuf (by default std::cout) to new one |
---|
| 60 | * This function associates a new streambuf to the current log object |
---|
| 61 | * \param [in] pointer to new streambuf |
---|
| 62 | */ |
---|
| 63 | void changeStreamBuff(std::streambuf* sBuff) { strBuf_ = sBuff; rdbuf(sBuff); } |
---|
| 64 | |
---|
[300] | 65 | int level ; |
---|
[347] | 66 | string name ; |
---|
[490] | 67 | std::streambuf* strBuf_; |
---|
[2341] | 68 | |
---|
| 69 | void setLogSetting(void) {logSetting = new CSetLog ;} |
---|
| 70 | |
---|
| 71 | CSetLog* logSetting = nullptr ; |
---|
[300] | 72 | }; |
---|
| 73 | |
---|
| 74 | extern CLog info; |
---|
[347] | 75 | extern CLog report; |
---|
[523] | 76 | extern CLog error; |
---|
[300] | 77 | } |
---|
| 78 | #endif |
---|