11 |
// |
// |
12 |
// $Id$ |
// $Id$ |
13 |
// $Log$ |
// $Log$ |
14 |
|
// Revision 1.4 2004/12/26 07:50:33 skunix |
15 |
|
// fixed Stream error, added a buffered Stream2 class, and added Level prefixing |
16 |
|
// |
17 |
// Revision 1.3 2004/12/26 06:52:36 skunix |
// Revision 1.3 2004/12/26 06:52:36 skunix |
18 |
// changed level > impl->level check to <, it acts right now |
// changed level > impl->level check to <, it acts right now |
19 |
// |
// |
31 |
|
|
32 |
#include <iostream> |
#include <iostream> |
33 |
#include <list> |
#include <list> |
34 |
|
|
35 |
|
#include <map> |
36 |
#include <boost/lambda/bind.hpp> |
#include <boost/lambda/bind.hpp> |
37 |
|
#include <boost/format.hpp> |
38 |
#include <boost/shared_ptr.hpp> |
#include <boost/shared_ptr.hpp> |
39 |
#include <boost/thread.hpp> |
#include <boost/thread.hpp> |
|
|
|
40 |
namespace usata |
namespace usata |
41 |
{ |
{ |
42 |
namespace log |
namespace log |
90 |
{ |
{ |
91 |
public: |
public: |
92 |
typedef std::list<Target *> TargetList; |
typedef std::list<Target *> TargetList; |
93 |
|
typedef std::map<int,std::string> PrefixMap; |
94 |
|
PrefixMap prefixes; |
95 |
|
|
96 |
TargetList targets; |
TargetList targets; |
97 |
int level; |
int level; |
116 |
{ |
{ |
117 |
impl->level = level; |
impl->level = level; |
118 |
} |
} |
119 |
|
void |
120 |
|
Server::set_prefix(int level, const std::string& prefix) |
121 |
|
{ |
122 |
|
impl->prefixes.insert( make_pair(level,prefix)); |
123 |
|
} |
124 |
|
|
125 |
void |
void |
126 |
Server::log(int level, const std::string& message) |
Server::log(int level, const std::string& message) |
127 |
{ |
{ |
128 |
|
typedef ServerImpl::PrefixMap::iterator PrefixMapIterator; |
129 |
// FIXME: add timestamping - descender |
// FIXME: add timestamping - descender |
130 |
|
|
131 |
if (level < impl->level) |
if (level < impl->level) |
132 |
return; |
return; |
133 |
|
PrefixMapIterator pit = impl->prefixes.find(level); |
134 |
|
|
135 |
|
if (pit != impl->prefixes.end()) |
136 |
|
{ |
137 |
|
using namespace boost::lambda; |
138 |
|
std::string nmessage = (boost::format("%1% - %2%") % pit->second % message).str(); |
139 |
|
std::for_each(impl->targets.begin(), impl->targets.end(), |
140 |
|
bind(&Target::write, _1, nmessage)); |
141 |
|
} |
142 |
|
else |
143 |
|
{ |
144 |
|
using namespace boost::lambda; |
145 |
|
std::for_each(impl->targets.begin(), impl->targets.end(), |
146 |
|
bind(&Target::write, _1, message)); |
147 |
|
|
148 |
using namespace boost::lambda; |
} |
|
std::for_each(impl->targets.begin(), impl->targets.end(), |
|
|
bind(&Target::write, _1, message)); |
|
149 |
} |
} |
150 |
|
|
151 |
// FIXME: should we throw an error? - descender |
// FIXME: should we throw an error? - descender |
168 |
bind(&Target::commit, _1)); |
bind(&Target::commit, _1)); |
169 |
} |
} |
170 |
|
|
171 |
|
void |
172 |
|
Stream2::commit() |
173 |
|
{ |
174 |
|
buf << std::endl; |
175 |
|
m_server->log(m_level, buf.str()); |
176 |
|
buf.str(""); |
177 |
|
} |
178 |
|
|
179 |
// Stream manipulators |
// Stream manipulators |
180 |
|
|
181 |
StreamSetLevel |
StreamSetLevel |