1 | //----------------------------------------------------------- |
---|
2 | // Copyright Christian Arnault LAL-Orsay CNRS |
---|
3 | // arnault@lal.in2p3.fr |
---|
4 | // Modified by Grigory Rybkin |
---|
5 | // See the complete license in cmt_license.txt "http://www.cecill.info". |
---|
6 | //----------------------------------------------------------- |
---|
7 | |
---|
8 | #ifndef __cmt_log_h__ |
---|
9 | #define __cmt_log_h__ |
---|
10 | |
---|
11 | #include "cmt_string.h" |
---|
12 | |
---|
13 | class CmtLogEnd |
---|
14 | { |
---|
15 | public: |
---|
16 | }; |
---|
17 | |
---|
18 | class CmtLogDummy |
---|
19 | { |
---|
20 | public: |
---|
21 | }; |
---|
22 | |
---|
23 | class CmtLog |
---|
24 | { |
---|
25 | public: |
---|
26 | CmtLog (); |
---|
27 | |
---|
28 | static CmtLogEnd& end (); |
---|
29 | CmtLog& operator << (const cmt_string& s); |
---|
30 | CmtLog& operator << (const char* c); |
---|
31 | CmtLog& operator << (int i); |
---|
32 | CmtLog& operator << (double d); |
---|
33 | CmtLog& operator << (void* p); |
---|
34 | CmtLog& operator << (CmtLogEnd& end); |
---|
35 | CmtLog& operator << (CmtLogDummy& dummy); |
---|
36 | |
---|
37 | private: |
---|
38 | bool check (); |
---|
39 | }; |
---|
40 | |
---|
41 | #define Log static CmtLog log_instance |
---|
42 | #define log_endl CmtLog::end () |
---|
43 | #define log if (Cmt::get_debug ()) log_instance << CmtMessage::prefix () + " (" << __FILE__ << ":" << __LINE__ << ") " |
---|
44 | //#define log if (Cmt::get_debug ()) log_instance << "#CMT> (" << __FILE__ << "-" << __LINE__ << ") " |
---|
45 | #define log_cont if (Cmt::get_debug ()) log_instance |
---|
46 | |
---|
47 | |
---|
48 | enum CmtMsgLevel |
---|
49 | { |
---|
50 | Nil = 0, |
---|
51 | Debug, |
---|
52 | Verbose, |
---|
53 | Info, |
---|
54 | Warning, |
---|
55 | Error, |
---|
56 | Fatal |
---|
57 | }; |
---|
58 | |
---|
59 | class CmtMessage |
---|
60 | { |
---|
61 | public: |
---|
62 | static cmt_string& prefix (); |
---|
63 | static void set_prefix (const cmt_string& prefix); |
---|
64 | static CmtMsgLevel& level (); |
---|
65 | static void set_level (const CmtMsgLevel& level); |
---|
66 | static bool active (const CmtMsgLevel& level); |
---|
67 | static void verbose (const cmt_string& message); |
---|
68 | static void info (const cmt_string& message); |
---|
69 | static void warning (const cmt_string& message); |
---|
70 | static void error (const cmt_string& message); |
---|
71 | static void fatal (const cmt_string& message); |
---|
72 | |
---|
73 | private: |
---|
74 | CmtMessage (); |
---|
75 | static CmtMessage& instance (); |
---|
76 | cmt_string m_prefix; |
---|
77 | CmtMsgLevel m_level; |
---|
78 | }; |
---|
79 | |
---|
80 | /**---------------------------------------------------------- |
---|
81 | The CmtMessage default constructor |
---|
82 | Here are primarily constructed all default definitions |
---|
83 | */ |
---|
84 | inline CmtMessage::CmtMessage () |
---|
85 | : m_prefix ("#CMT--->"), m_level (Info) |
---|
86 | { } |
---|
87 | |
---|
88 | /**---------------------------------------------------------- |
---|
89 | The CmtMessage singleton |
---|
90 | */ |
---|
91 | inline CmtMessage& CmtMessage::instance () |
---|
92 | { |
---|
93 | static CmtMessage me; |
---|
94 | return me; |
---|
95 | } |
---|
96 | |
---|
97 | inline cmt_string& CmtMessage::prefix () |
---|
98 | { |
---|
99 | return instance ().m_prefix; |
---|
100 | } |
---|
101 | |
---|
102 | inline void CmtMessage::set_prefix (const cmt_string& prefix) |
---|
103 | { |
---|
104 | instance ().m_prefix = prefix; |
---|
105 | } |
---|
106 | |
---|
107 | inline CmtMsgLevel& CmtMessage::level () |
---|
108 | { |
---|
109 | return instance ().m_level; |
---|
110 | } |
---|
111 | |
---|
112 | inline void CmtMessage::set_level (const CmtMsgLevel& level) |
---|
113 | { |
---|
114 | instance ().m_level = level; |
---|
115 | } |
---|
116 | |
---|
117 | inline bool CmtMessage::active (const CmtMsgLevel& lvl) |
---|
118 | { |
---|
119 | return level () <= lvl; |
---|
120 | } |
---|
121 | |
---|
122 | inline void CmtMessage::verbose (const cmt_string& message) |
---|
123 | { |
---|
124 | if (active (Verbose)) |
---|
125 | cerr << prefix () << " Verbose: " << message << endl; |
---|
126 | } |
---|
127 | |
---|
128 | inline void CmtMessage::info (const cmt_string& message) |
---|
129 | { |
---|
130 | if (active (Info)) |
---|
131 | cerr << prefix () << " Info: " << message << endl; |
---|
132 | } |
---|
133 | |
---|
134 | inline void CmtMessage::warning (const cmt_string& message) |
---|
135 | { |
---|
136 | if (active (Warning)) |
---|
137 | cerr << prefix () << " Warning: " << message << endl; |
---|
138 | } |
---|
139 | |
---|
140 | inline void CmtMessage::error (const cmt_string& message) |
---|
141 | { |
---|
142 | if (active (Error)) |
---|
143 | cerr << prefix () << " Error: " << message << endl; |
---|
144 | } |
---|
145 | |
---|
146 | inline void CmtMessage::fatal (const cmt_string& message) |
---|
147 | { |
---|
148 | if (active (Fatal)) |
---|
149 | cerr << prefix () << " Fatal: " << message << endl; |
---|
150 | } |
---|
151 | #endif |
---|