Utils/Logger: Implement targets
[senf.git] / Utils / Logger / Target.cci
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 inline non-template implementation */
25
26 //#include "Target.ih"
27
28 // Custom includes
29 #include <algorithm>
30
31 #define prefix_ inline
32 ///////////////////////////////cci.p///////////////////////////////////////
33
34 ///////////////////////////////////////////////////////////////////////////
35 // senf::log::Target
36
37 prefix_ void senf::log::Target::route(detail::StreamBase const * stream,
38                                       detail::AreaBase const * area, unsigned level)
39 {
40     rib_.push_back(RoutingEntry(stream, area, level));
41
42     // Update the area/stream routing cache
43     if (area)
44         updateAreaCache(*area, stream, level);
45     else {
46         AreaRegistry::Registry::iterator i (AreaRegistry::instance().registry_.begin());
47         AreaRegistry::Registry::iterator const i_end (AreaRegistry::instance().registry_.end());
48         for (; i != i_end; ++i)
49             updateAreaCache(*(i->second), stream, level);
50     }
51         
52 }
53
54 prefix_ void
55 senf::log::Target::updateAreaCache(detail::AreaBase const & area,
56                                    detail::StreamBase const * stream, unsigned level)
57 {
58     if (stream) {
59         if (level < area.streamLimit(*stream))
60             area.setStreamLimit(*stream, level);
61     } else {
62         StreamRegistry::Registry::iterator i (StreamRegistry::instance().registry_.begin());
63         StreamRegistry::Registry::iterator const i_end (StreamRegistry::instance().registry_.end());
64         for(; i != i_end; ++i)
65             if (level < area.streamLimit(*(i->second)))
66                 area.setStreamLimit(*(i->second),level);
67     }
68 }
69
70 prefix_ void senf::log::Target::unroute(detail::StreamBase const * stream,
71                                         detail::AreaBase const * area, unsigned level)
72 {
73     rib_.erase(std::remove(rib_.begin(), rib_.end(), RoutingEntry(stream, area, level)),
74                rib_.end());
75
76     ///\fixme Update area/stream routing cache
77     // Not doing anything here does not produce incorrect behavior, since removing a route
78     // can never lower the logging limit. Not updating the cache just reduces the performance.
79 }
80
81 ///////////////////////////////////////////////////////////////////////////
82 // senf::log::TargetRegistry
83
84 prefix_ void senf::log::TargetRegistry::registerTarget(Target * target)
85 {
86     targets_.insert(target);
87 }
88
89 prefix_ void senf::log::TargetRegistry::unregisterTarget(Target * target)
90 {
91     targets_.erase(target);
92 }
93
94 /////////////////////////////cci.e///////////////////////////////////////
95 #undef prefix_
96
97 \f
98 // Local Variables:
99 // mode: c++
100 // fill-column: 100
101 // comment-column: 40
102 // c-file-style: "senf"
103 // indent-tabs-mode: nil
104 // ispell-local-dictionary: "american"
105 // compile-command: "scons -u test"
106 // End: