1 |
// -*- mode: C++; tab-width: 4; indent-tabs-mode: t; -*- vim:ts=4:sw=4 |
// -*- mode: C++; tab-width: 4; indent-tabs-mode: t; -*- vim:ts=4:sw=4 |
2 |
// |
// |
3 |
// Copyright (C) 2004 Chong Kai Xiong (descender) |
// Copyright (C) 2004 Chong Kai Xiong (descender) |
4 |
|
// David Lau (skunix) |
5 |
// |
// |
6 |
// This file is part of The Plains of Usata. |
// This file is part of The Plains of Usata. |
7 |
// |
// |
10 |
// included in the software distribution, or visit |
// included in the software distribution, or visit |
11 |
// http://www.fsf.org/licenses/gpl.html. |
// http://www.fsf.org/licenses/gpl.html. |
12 |
// |
// |
13 |
// $Log$ |
// $Id$ |
|
// Revision 1.5 2004/12/26 09:35:23 skunix |
|
|
// use logging functionality |
|
|
// |
|
|
// Revision 1.4 2004/12/26 08:03:34 skunix |
|
|
// changed lambda::bind's to boost::binds |
|
|
// |
|
|
// Revision 1.3 2004/12/26 07:50:33 skunix |
|
|
// fixed Stream error, added a buffered Stream2 class, and added Level prefixing |
|
|
// |
|
|
// Revision 1.2 2004/12/26 02:59:35 Descender |
|
|
// |
|
|
// log::Target: change access of destructor to protected |
|
|
// log::Stream: change type of parameter 'server' for constructor to pointer, remove unused constructor |
|
|
// |
|
|
// Revision 1.1 2004/12/26 02:26:18 Descender |
|
|
// |
|
|
// New logging functionality. |
|
|
// |
|
14 |
|
|
15 |
#ifndef USATA_LOG_HPP |
#ifndef USATA_LOG_HPP |
16 |
#define USATA_LOG_HPP |
#define USATA_LOG_HPP |
112 |
}; |
}; |
113 |
|
|
114 |
|
|
115 |
class Stream2 |
class BufferedStream |
116 |
{ |
{ |
117 |
public: |
public: |
118 |
Stream2(int level = Level::INFO, Server*pServer=&::usata::log::server) |
|
119 |
:m_server(pServer),m_level(level){}; |
BufferedStream(int level = Level::INFO, Server *server = &::usata::log::server) |
120 |
~Stream2(){if (!buf.str().empty()) commit(); } |
: m_server(server), |
121 |
void set_level(int level){m_level=level;} |
m_level(level) |
122 |
|
{}; |
123 |
|
|
124 |
|
~BufferedStream() |
125 |
|
{ |
126 |
|
if (!m_buffer.str().empty()) |
127 |
|
commit(); |
128 |
|
} |
129 |
|
|
130 |
|
void |
131 |
|
set_level(int level) |
132 |
|
{ m_level = level; } |
133 |
|
|
134 |
template <typename T> |
template <typename T> |
135 |
friend Stream2& |
friend BufferedStream& |
136 |
operator << (Stream2& log, T data); |
operator << (BufferedStream& log, T data) |
137 |
|
{ |
138 |
|
log.m_buffer << data; |
139 |
|
return log; |
140 |
|
} |
141 |
|
|
142 |
void commit(); |
void commit(); |
143 |
|
|
144 |
private: |
private: |
145 |
Server *m_server; |
|
146 |
int m_level; |
Server *m_server; |
147 |
std::stringstream buf; |
int m_level; |
148 |
|
std::stringstream m_buffer; |
149 |
}; |
}; |
150 |
|
|
151 |
class Stream |
class Stream |
152 |
{ |
{ |
153 |
public: |
public: |
154 |
|
|
155 |
Stream(Server *server, int level = Level::INFO) |
Stream(int level = Level::INFO, Server *server = &usata::log::server) |
156 |
: m_server(server), m_level(level) |
: m_server(server), m_level(level) |
157 |
{} |
{} |
158 |
|
|
167 |
|
|
168 |
template <typename T> |
template <typename T> |
169 |
friend Stream& |
friend Stream& |
170 |
operator << (Stream& log, T data); |
operator << (Stream& log, T data) |
171 |
|
{ |
172 |
|
log.m_server->log(log.m_level, boost::lexical_cast<std::string>(data)); |
173 |
|
return log; |
174 |
|
} |
175 |
|
|
176 |
private: |
private: |
177 |
|
|
178 |
Server *m_server; |
Server *m_server; |
179 |
int m_level; |
int m_level; |
|
}; |
|
|
template <typename T> |
|
|
Stream& operator<<(Stream&log,T data) |
|
|
{ |
|
|
log.m_server->log(log.m_level, boost::lexical_cast<std::string>(data)); |
|
|
return log; |
|
|
} |
|
180 |
|
|
181 |
template <typename T> |
}; |
|
Stream2& operator<<(Stream2&log,T data) |
|
|
{ |
|
|
log.buf << data; |
|
|
return log; |
|
|
} |
|
182 |
|
|
183 |
// Stream manipulators |
// Stream manipulators |
184 |
|
|
185 |
struct StreamSetLevel { int level; }; |
struct StreamSetLevel { int level; }; |
186 |
struct CommitT {}; |
|
187 |
extern CommitT Commit; |
struct BufferedStreamCommit {}; |
188 |
|
extern BufferedStreamCommit commit; |
189 |
|
|
190 |
|
|
191 |
|
StreamSetLevel set_level(int level); |
192 |
|
|
193 |
inline Stream& |
inline Stream& |
194 |
operator << (Stream& log, const StreamSetLevel& manip) |
operator << (Stream& log, const StreamSetLevel& manip) |
195 |
{ |
{ |
197 |
return log; |
return log; |
198 |
} |
} |
199 |
|
|
200 |
inline Stream2& |
inline BufferedStream& |
201 |
operator << (Stream2& log, const StreamSetLevel&manip) |
operator << (BufferedStream& log, const StreamSetLevel& manip) |
202 |
{ |
{ |
203 |
log.set_level(manip.level); |
log.set_level(manip.level); |
204 |
return log; |
return log; |
205 |
} |
} |
206 |
|
|
207 |
inline Stream2& |
inline BufferedStream& |
208 |
operator << (Stream2&log, const CommitT&) |
operator << (BufferedStream &log, const BufferedStreamCommit& commit) |
209 |
{ |
{ |
210 |
log.commit(); |
log.commit(); |
211 |
return log; |
return log; |
212 |
} |
} |
213 |
|
|
|
StreamSetLevel set_level(int level); |
|
|
|
|
214 |
} // namespace log |
} // namespace log |
215 |
|
|
216 |
} // namespace usata |
} // namespace usata |