1 | #ifndef __TOTILOG_H__
|
---|
2 | #define __TOTILOG_H__
|
---|
3 |
|
---|
4 | #include <fstream>
|
---|
5 | #include <string>
|
---|
6 | #include <vector>
|
---|
7 | #include <map>
|
---|
8 | #include <set>
|
---|
9 |
|
---|
10 | #include <boost/thread.hpp>
|
---|
11 |
|
---|
12 | #include "mars_util.h"
|
---|
13 |
|
---|
14 | namespace mars
|
---|
15 | {
|
---|
16 | enum LogSettings
|
---|
17 | {
|
---|
18 | Log_File = 1 << 0,
|
---|
19 | Log_Function = 1 << 1,
|
---|
20 | Log_Line = 1 << 2,
|
---|
21 | Log_Time = 1 << 3,
|
---|
22 | Log_Thread = 1 << 4
|
---|
23 | };
|
---|
24 |
|
---|
25 | class Outputter
|
---|
26 | {
|
---|
27 | private:
|
---|
28 | std::ofstream * _pOut;
|
---|
29 | mutable boost::mutex * _pMutex;
|
---|
30 |
|
---|
31 | template< typename CTR >
|
---|
32 | inline Outputter & writeContainer (const CTR & ctr)
|
---|
33 | {
|
---|
34 | if (_pOut != NULL)
|
---|
35 | {
|
---|
36 | *_pOut << "[";
|
---|
37 | for (typename CTR::const_iterator i = ctr.begin(); i != ctr.end(); ++i)
|
---|
38 | {
|
---|
39 | if (i != ctr.begin())
|
---|
40 | *_pOut << ", ";
|
---|
41 | *this << *i;
|
---|
42 | }
|
---|
43 | *_pOut << "]";
|
---|
44 | }
|
---|
45 | return *this;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public:
|
---|
49 | Outputter ();
|
---|
50 | Outputter (std::ofstream * pOut, const unsigned int enSettings, boost::mutex * pMutex, const time_t & tTime, const char * szFile, const unsigned int nLine, const char * szFunction, const boost::thread::id & idThread);
|
---|
51 | ~Outputter();
|
---|
52 |
|
---|
53 | template< typename T >
|
---|
54 | inline Outputter & operator << (const T & nVal)
|
---|
55 | {
|
---|
56 | if (_pOut != NULL)
|
---|
57 | *_pOut << nVal;
|
---|
58 | return *this;
|
---|
59 | }
|
---|
60 |
|
---|
61 | template< typename T >
|
---|
62 | inline Outputter & operator << (const BBox< T > & bbox)
|
---|
63 | {
|
---|
64 | if (_pOut != NULL)
|
---|
65 | *_pOut
|
---|
66 | << typeid(BBox< T >).name()
|
---|
67 | << "[left=" << bbox.left << ", top="
|
---|
68 | << bbox.top << ", right=" << bbox.right << ", bottom="
|
---|
69 | << bbox.bottom << "]";
|
---|
70 |
|
---|
71 | return *this;
|
---|
72 | }
|
---|
73 |
|
---|
74 | template< typename T >
|
---|
75 | inline Outputter & operator << (const Range< T > & rng)
|
---|
76 | {
|
---|
77 | if (_pOut != NULL)
|
---|
78 | {
|
---|
79 | *_pOut
|
---|
80 | << typeid(Range< T >).name()
|
---|
81 | << '[' << rng.minimum << " <= x <= " << rng.maximum << ']';
|
---|
82 | }
|
---|
83 |
|
---|
84 | return *this;
|
---|
85 | }
|
---|
86 |
|
---|
87 | template< typename T >
|
---|
88 | inline Outputter & operator << (const RangeX< T > & rng)
|
---|
89 | {
|
---|
90 | if (_pOut != NULL)
|
---|
91 | {
|
---|
92 | *_pOut
|
---|
93 | << typeid(Range< T >).name()
|
---|
94 | << '[' << rng.minimum << " <= x <= " << rng.maximum;
|
---|
95 |
|
---|
96 | if (rng.step != (std::numeric_limits<T>::epsilon() == 0 ? 1 : std::numeric_limits<T>::epsilon()))
|
---|
97 | *_pOut << ", step=" << rng.step;
|
---|
98 |
|
---|
99 | *_pOut << ']';
|
---|
100 | }
|
---|
101 |
|
---|
102 | return *this;
|
---|
103 | }
|
---|
104 |
|
---|
105 | template< typename T >
|
---|
106 | inline Outputter & operator << (const T * ptr)
|
---|
107 | {
|
---|
108 | if (_pOut != NULL)
|
---|
109 | *_pOut
|
---|
110 | << typeid(T *).name() << "=0x" << std::setw(4) << std::setfill('0') << std::ios::hex << ptr;
|
---|
111 |
|
---|
112 | return *this;
|
---|
113 | }
|
---|
114 |
|
---|
115 | inline Outputter & operator << (const std::string & str)
|
---|
116 | {
|
---|
117 | if (_pOut != NULL)
|
---|
118 | *_pOut << str;
|
---|
119 | return *this;
|
---|
120 | }
|
---|
121 | inline Outputter & operator << (const char * sz)
|
---|
122 | {
|
---|
123 | if (_pOut != NULL)
|
---|
124 | *_pOut << sz;
|
---|
125 | return *this;
|
---|
126 | }
|
---|
127 |
|
---|
128 | template< typename A, typename B >
|
---|
129 | inline Outputter & operator << (const std::pair< A, B > & pair)
|
---|
130 | {
|
---|
131 | if (_pOut != NULL)
|
---|
132 | *_pOut << '(' << pair.first << ", " << pair.second << ')';
|
---|
133 | return *this;
|
---|
134 | }
|
---|
135 |
|
---|
136 | template< typename V >
|
---|
137 | inline Outputter & operator << (const std::set< V > & sett)
|
---|
138 | { return writeContainer(sett); }
|
---|
139 |
|
---|
140 | template< typename V >
|
---|
141 | inline Outputter & operator << (const std::vector< V > & vec)
|
---|
142 | { return writeContainer(vec); }
|
---|
143 |
|
---|
144 | template< typename K, typename V >
|
---|
145 | inline Outputter & operator << (const std::map< K, V > & map)
|
---|
146 | {
|
---|
147 | if (_pOut != NULL)
|
---|
148 | {
|
---|
149 | *_pOut << "[";
|
---|
150 | for (typename std::map< K, V >::const_iterator i = map.begin(); i != map.end(); ++i)
|
---|
151 | {
|
---|
152 | if (i != map.begin())
|
---|
153 | *_pOut << ", ";
|
---|
154 | *this << i->first << " -> " << i->second;
|
---|
155 | }
|
---|
156 | *_pOut << "]";
|
---|
157 | }
|
---|
158 | return *this;
|
---|
159 | }
|
---|
160 | };
|
---|
161 |
|
---|
162 | class Log
|
---|
163 | {
|
---|
164 | public:
|
---|
165 | enum Level
|
---|
166 | {
|
---|
167 | Info = 0,
|
---|
168 | Warn = 1,
|
---|
169 | Fatal = 2,
|
---|
170 | Debug = 3
|
---|
171 | };
|
---|
172 |
|
---|
173 | #ifndef MARS_LOGLEVEL
|
---|
174 | #define MARS_LOGLEVEL Fatal
|
---|
175 | #endif
|
---|
176 | Log (const std::string & sFilename = "mars.log", const Level enLevel = MARS_LOGLEVEL, const unsigned int enSettings = Log_Function | Log_Time);
|
---|
177 |
|
---|
178 | Outputter stream (const Level enLevel = Info, const char * szFile = NULL, const char * szFunction = NULL, const unsigned int nLine = 0);
|
---|
179 | static Log & getSingleton ();
|
---|
180 |
|
---|
181 | inline void setLevel (const Level enLevel) { _enLevel = enLevel; }
|
---|
182 | inline Level getLevel () const { return _enLevel; }
|
---|
183 |
|
---|
184 | inline void setSettings (const unsigned int enSettings) { _enSettings = enSettings; }
|
---|
185 | inline unsigned int getSettings () const { return _enSettings; }
|
---|
186 | inline void toggleSetting (const LogSettings enSetting) { _enSettings ^= enSetting; }
|
---|
187 | inline void addSettings (const unsigned int enSettings) { _enSettings |= enSettings; }
|
---|
188 | inline void removeSettings (const unsigned int enSettings) { _enSettings &= ~enSettings; }
|
---|
189 |
|
---|
190 | ~Log();
|
---|
191 |
|
---|
192 | private:
|
---|
193 | std::ofstream _outfile;
|
---|
194 | boost::mutex _mutex;
|
---|
195 | unsigned int _enSettings;
|
---|
196 | Level _enLevel;
|
---|
197 |
|
---|
198 | const time_t _tTimeStart;
|
---|
199 | };
|
---|
200 |
|
---|
201 | extern Log g_log;
|
---|
202 | }
|
---|
203 |
|
---|
204 | #define LOG(l) mars::g_log.stream(l, __FILE__, __FUNCTION__, __LINE__)
|
---|
205 | #define LOGN LOG(mars::Log::Info)
|
---|
206 | #define LOGD LOG(mars::Log::Debug)
|
---|
207 |
|
---|
208 | #endif
|
---|