Utils/Logger: BUGFIX: Compile time syntax error
[senf.git] / Utils / Logger / Target.cc
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Target non-inline non-template implementation */
25
26 #include "Target.hh"
27 //#include "Target.ih"
28
29 // Custom includes
30 #include <algorithm>
31
32 //#include "Target.mpp"
33 #define prefix_
34 ///////////////////////////////cc.p////////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::log::Target
38
39 prefix_ senf::log::Target::Target()
40 {
41     TargetRegistry::instance().registerTarget(this);
42 }
43
44 prefix_ senf::log::Target::~Target()
45 {
46     while( ! rib_.empty()) {
47         // This is terribly slow but simplifies the area cache handling and removing a target should
48         // be quite seldom
49         RIB::reverse_iterator i (rib_.rbegin());
50         unroute(i->stream_, i->area_, i->level_, i->action_);
51     }
52     TargetRegistry::instance().unregisterTarget(this);
53 }
54
55 prefix_ void senf::log::Target::route(std::string const & stream, std::string const & area,
56                                       unsigned level, action_t action, int index)
57 {
58     detail::StreamBase const * s (StreamRegistry::instance().lookup(stream));
59     if (!s)
60         throw InvalidStreamException();
61     detail::AreaBase const * a (0);
62     if (! area.empty()) {
63         a = AreaRegistry::instance().lookup(area);
64         if (!a)
65             throw InvalidAreaException();
66     }
67     route(s, a, level, action, index);
68 }
69
70 prefix_ void senf::log::Target::unroute(int index)
71 {
72     RIB::iterator i;
73     if (index < 0) {
74         if (RIB::size_type(-index) >= rib_.size())
75             i = rib_.begin();
76         else {
77             i = rib_.end();
78             std::advance(i, -index);
79         }
80     } else {
81         if (RIB::size_type(index+1) >= rib_.size()) {
82             i = rib_.end();
83             --i;
84         } else {
85             i = rib_.begin();
86             std::advance(i, index);
87         }
88     }
89     RoutingEntry entry (*i);
90     rib_.erase(i);
91     if (entry.action_ == ACCEPT)
92         updateRoutingCache(entry.stream_, entry.area_);
93 }
94
95 ////////////////////////////////////////
96 // private members
97
98 prefix_ void senf::log::Target::route(detail::StreamBase const * stream,
99                                       detail::AreaBase const * area, unsigned level,
100                                       action_t action, int index)
101 {
102     RIB::iterator i;
103     if (index < 0) {
104         if (RIB::size_type(-index-1) >= rib_.size())
105             i = rib_.begin();
106         else {
107             i = rib_.end();
108             std::advance(i, -index - 1);
109         }
110     } else {
111         if (RIB::size_type(index) >= rib_.size())
112             i = rib_.end();
113         else {
114             i = rib_.begin();
115             std::advance(i, index);
116         }
117     }
118     rib_.insert(i, RoutingEntry(stream, area, level, action));
119     if (action == ACCEPT)
120         updateRoutingCache(stream, area);
121 }
122
123 prefix_ void senf::log::Target::unroute(detail::StreamBase const * stream,
124                                         detail::AreaBase const * area, unsigned level, 
125                                         action_t action)
126 {
127     RIB::iterator i = std::find(rib_.begin(), rib_.end(), 
128                                 RoutingEntry(stream, area, level, action));
129     if (i != rib_.end())
130         unroute(std::distance(rib_.begin(), i));
131 }
132
133 prefix_ void senf::log::Target::updateRoutingCache(detail::StreamBase const * stream,
134                                                    detail::AreaBase const * area)
135 {
136     if (! stream) {
137         StreamRegistry::Registry::iterator i (StreamRegistry::instance().registry_.begin());
138         StreamRegistry::Registry::iterator const i_end (StreamRegistry::instance().registry_.end());
139         for (; i != i_end ; ++i)
140             updateRoutingCache(i->second, area);
141         return;
142     }
143     if (! area) {
144         AreaRegistry::Registry::iterator i (AreaRegistry::instance().registry_.begin());
145         AreaRegistry::Registry::iterator const i_end (AreaRegistry::instance().registry_.end());
146         for (; i != i_end ; ++i)
147             updateRoutingCache(stream, i->second);
148         return;
149     }
150     unsigned limit (DISABLED::value);
151     RIB::iterator i (rib_.begin());
152     RIB::iterator const i_end (rib_.end());
153     for(; i != i_end; ++i)
154         if ( (! i->stream_ || i->stream_ == stream) &&
155              (! i->area_ || i->area_ == area) &&
156              i->action_ == ACCEPT ) {
157             unsigned l (i->level_ == NONE::value ? i->stream_->defaultRuntimeLimit() : i->level_);
158             if (l < limit)
159                 limit = l;
160         }
161     if (limit == DISABLED::value)
162         area->removeRoutingCache(*this, *stream);
163     else
164         area->updateRoutingCache(*this, *stream, limit);
165 }
166
167 prefix_ void senf::log::Target::write(boost::posix_time::ptime timestamp,
168                                       detail::StreamBase const & stream,
169                                       detail::AreaBase const & area, unsigned level,
170                                       std::string const & message)
171 {
172     RIB::iterator i (rib_.begin());
173     RIB::iterator const i_end (rib_.end());
174     for (; i != i_end; ++i)
175         if ( (! i->stream_ || i->stream_ == &stream) &&
176              (! i->area_ || i->area_ == &area) &&
177              (i->level_ == NONE::value ? i->stream_->defaultRuntimeLimit() : i->level_) <= level ) {
178             if (i->action_ == ACCEPT)
179                 v_write(timestamp, stream.v_name(), area.v_name(), level, message);
180             return;
181         }
182 }
183
184 ///////////////////////////////////////////////////////////////////////////
185 // senf::log::TargetRegistry
186
187 prefix_ void senf::log::TargetRegistry::write(detail::StreamBase const & stream,
188                                               detail::AreaBase const & area, unsigned level,
189                                               std::string msg)
190 {
191     boost::posix_time::ptime timestamp (boost::posix_time::microsec_clock::universal_time());
192     area.write(timestamp, stream, level, msg);
193 }
194
195 ///////////////////////////////cc.e////////////////////////////////////////
196 #undef prefix_
197 //#include "Target.mpp"
198
199 \f
200 // Local Variables:
201 // mode: c++
202 // fill-column: 100
203 // comment-column: 40
204 // c-file-style: "senf"
205 // indent-tabs-mode: nil
206 // ispell-local-dictionary: "american"
207 // compile-command: "scons -u test"
208 // End: