4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Target non-inline non-template implementation */
31 #include "ConsoleTarget.hh"
33 //#include "Target.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////
40 prefix_ senf::log::Target::Target()
42 detail::TargetRegistry::instance().registerTarget(this);
45 prefix_ senf::log::Target::~Target()
47 while( ! rib_.empty()) {
48 // This is slow but simplifies the area cache handling and removing a target should be
50 RIB::reverse_iterator i (rib_.rbegin());
51 unroute(i->stream_, i->area_, i->level_, i->action_);
53 detail::TargetRegistry::instance().unregisterTarget(this);
56 prefix_ void senf::log::Target::route(std::string const & stream, std::string const & area,
57 unsigned level, action_t action, int index)
59 detail::StreamBase const * s (StreamRegistry::instance().lookup(stream));
61 throw InvalidStreamException();
62 detail::AreaBase const * a (0);
64 a = AreaRegistry::instance().lookup(area);
66 throw InvalidAreaException();
68 route(s, a, level, action, index);
71 prefix_ void senf::log::Target::unroute(std::string const & stream, std::string const & area,
72 unsigned level, action_t action)
74 detail::StreamBase const * s (StreamRegistry::instance().lookup(stream));
76 throw InvalidStreamException();
77 detail::AreaBase const * a (0);
79 a = AreaRegistry::instance().lookup(area);
81 throw InvalidAreaException();
83 unroute(s, a, level, action);
86 prefix_ void senf::log::Target::unroute(int index)
92 if (RIB::size_type(-index) >= rib_.size())
96 std::advance(i, index);
99 if (RIB::size_type(index+1) >= rib_.size()) {
104 std::advance(i, index);
109 RoutingEntry entry (*i);
111 if (entry.action_ == ACCEPT)
112 updateRoutingCache(entry.stream_, entry.area_);
115 prefix_ void senf::log::Target::clear()
119 RIB::const_iterator i (old.begin());
120 RIB::const_iterator const i_end (old.end());
121 for (; i != i_end; ++i)
122 if (i->action_ == ACCEPT)
123 updateRoutingCache(i->stream_, i->area_)
126 ////////////////////////////////////////
129 prefix_ void senf::log::Target::route(detail::StreamBase const * stream,
130 detail::AreaBase const * area, unsigned level,
131 action_t action, int index)
135 if (RIB::size_type(-index-1) >= rib_.size())
139 std::advance(i, index + 1 );
142 if (RIB::size_type(index) >= rib_.size())
146 std::advance(i, index);
149 rib_.insert(i, RoutingEntry(stream, area, level, action));
150 if (action == ACCEPT)
151 updateRoutingCache(stream, area);
152 // This disables the initial fallback routing
153 detail::TargetRegistry::instance().routed();
156 prefix_ void senf::log::Target::unroute(detail::StreamBase const * stream,
157 detail::AreaBase const * area, unsigned level,
160 RIB::iterator i = std::find(rib_.begin(), rib_.end(),
161 RoutingEntry(stream, area, level, action));
163 unroute(std::distance(rib_.begin(), i));
166 prefix_ void senf::log::Target::updateRoutingCache(detail::StreamBase const * stream,
167 detail::AreaBase const * area)
170 StreamRegistry::Registry::iterator i (StreamRegistry::instance().registry_.begin());
171 StreamRegistry::Registry::iterator const i_end (StreamRegistry::instance().registry_.end());
172 for (; i != i_end ; ++i)
173 updateRoutingCache(i->second, area);
177 AreaRegistry::Registry::iterator i (AreaRegistry::instance().registry_.begin());
178 AreaRegistry::Registry::iterator const i_end (AreaRegistry::instance().registry_.end());
179 for (; i != i_end ; ++i)
180 updateRoutingCache(stream, i->second);
183 unsigned limit (DISABLED::value);
184 RIB::iterator i (rib_.begin());
185 RIB::iterator const i_end (rib_.end());
186 for(; i != i_end; ++i)
187 if ( (! i->stream_ || i->stream_ == stream) &&
188 (! i->area_ || i->area_ == area) &&
189 i->action_ == ACCEPT ) {
190 unsigned l (i->level_ == NONE::value ? stream->defaultRuntimeLimit() : i->level_);
194 if (limit == DISABLED::value)
195 area->removeRoutingCache(*this, *stream);
197 area->updateRoutingCache(*this, *stream, limit);
200 prefix_ void senf::log::Target::write(time_type timestamp,
201 detail::StreamBase const & stream,
202 detail::AreaBase const & area, unsigned level,
203 std::string const & message)
205 RIB::iterator i (rib_.begin());
206 RIB::iterator const i_end (rib_.end());
207 for (; i != i_end; ++i)
208 if ( (! i->stream_ || i->stream_ == &stream) &&
209 (! i->area_ || i->area_ == &area) &&
210 (i->level_ == NONE::value ? stream.defaultRuntimeLimit() : i->level_) <= level ) {
211 if (i->action_ == ACCEPT)
212 v_write(timestamp, stream.v_name(), area.v_name(), level, message);
217 ///////////////////////////////////////////////////////////////////////////
218 // senf::log::detail::TargetRegistry
220 prefix_ void senf::log::detail::TargetRegistry::write(StreamBase const & stream,
221 AreaBase const & area, unsigned level,
222 std::string const & msg)
224 if (fallbackRouting_) {
225 if (level >= stream.defaultRuntimeLimit())
226 static_cast<Target &>(ConsoleTarget::instance()).v_write(
227 TimeSource::now(), stream.v_name(), area.v_name(), level, msg );
230 area.write( TimeSource::now(), stream, level, msg );
232 ///////////////////////////////////////////////////////////////////////////
235 prefix_ std::ostream & senf::log::operator<<(std::ostream & os, senf::log::Target::action_t const & action)
237 if( action == Target::ACCEPT) os << "ACCEPT";
238 else if( action == Target::REJECT) os << "REJECT";
239 else os << "unknown action";
244 ///////////////////////////////cc.e////////////////////////////////////////
246 //#include "Target.mpp"
252 // comment-column: 40
253 // c-file-style: "senf"
254 // indent-tabs-mode: nil
255 // ispell-local-dictionary: "american"
256 // compile-command: "scons -u test"