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