1 | #ifndef __XIOS_LOG_HPP__ |
---|
2 | #define __XIOS_LOG_HPP__ |
---|
3 | |
---|
4 | #include <string> |
---|
5 | #include <iostream> |
---|
6 | #include <string> |
---|
7 | #include "log_type.hpp" |
---|
8 | |
---|
9 | namespace xios |
---|
10 | { |
---|
11 | using namespace std ; |
---|
12 | |
---|
13 | class CLog : public ostream |
---|
14 | { |
---|
15 | public : |
---|
16 | CLog(const string& name_, std::streambuf* sBuff = cout.rdbuf()) |
---|
17 | : ostream(sBuff), level(0), name(name_), strBuf_(sBuff) {} |
---|
18 | CLog& operator()(int l) |
---|
19 | { |
---|
20 | if (l<=level) |
---|
21 | { |
---|
22 | rdbuf(strBuf_); |
---|
23 | *this<<"-> "<<name<<" : " ; |
---|
24 | } |
---|
25 | else rdbuf(NULL) ; |
---|
26 | return *this; |
---|
27 | } |
---|
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 | |
---|
41 | void setLevel(int l) {level=l; } |
---|
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 ; } |
---|
45 | bool isActive(CLogType& logType) {if (logSetting==nullptr) setLogSetting() ; if (logSetting->isSet(logType)) return true; else return false; } |
---|
46 | |
---|
47 | public: |
---|
48 | //! Write log into a file with its streambuf |
---|
49 | void write2File(std::streambuf* sBuff) { changeStreamBuff(sBuff); } |
---|
50 | |
---|
51 | //! Write log into standard output |
---|
52 | void write2StdOut() { changeStreamBuff(cout.rdbuf()); } |
---|
53 | |
---|
54 | //! Write log into standard error output |
---|
55 | void write2StdErr() { changeStreamBuff(cerr.rdbuf()); } |
---|
56 | |
---|
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 | |
---|
65 | int level ; |
---|
66 | string name ; |
---|
67 | std::streambuf* strBuf_; |
---|
68 | |
---|
69 | void setLogSetting(void) {logSetting = new CSetLog ;} |
---|
70 | |
---|
71 | CSetLog* logSetting = nullptr ; |
---|
72 | }; |
---|
73 | |
---|
74 | extern CLog info; |
---|
75 | extern CLog report; |
---|
76 | extern CLog error; |
---|
77 | } |
---|
78 | #endif |
---|