4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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 public header */
31 #include <boost/date_time/posix_time/posix_time.hpp>
32 #include <boost/utility.hpp>
33 #include <boost/type_traits/is_convertible.hpp>
34 #include "../singleton.hh"
36 #include "StreamRegistry.hh"
37 #include "AreaRegistry.hh"
39 //#include "Target.mpp"
40 ///////////////////////////////hh.p////////////////////////////////////////
47 /** \brief Logging target base class
49 All enabled log messages are eventually routed to one or more logging targets. It is the
50 responsibility of the logging target to write the log messages somewhere: onto the console,
51 to a file, to mail them to the administrator or whatever. To this end, the logging target is
52 passed the log message and a complete set of logging parameters (\e stream, \e area and \e
55 \fixme optionally Integrate with Scheduler / ClockService to reduce number of gettimeofday()
58 class Target : private boost::noncopyable
61 ///////////////////////////////////////////////////////////////////////////
64 enum action_t { ACCEPT, REJECT };
69 bool operator==(RoutingEntry const & other);
71 std::string stream() const;
72 std::string area() const;
73 unsigned level() const;
74 action_t action() const;
77 RoutingEntry(detail::StreamBase const * stream, detail::AreaBase const * area,
78 unsigned level, action_t action);
80 detail::StreamBase const * stream_;
81 detail::AreaBase const * area_;
89 typedef std::vector<RoutingEntry> RIB;
92 typedef RIB::const_iterator iterator;
94 ///////////////////////////////////////////////////////////////////////////
95 ///\name Structors and default members
103 template <class Stream> void route(
104 action_t action = ACCEPT, int index = -1);
105 template <class Stream, class Level> void route(
106 action_t action = ACCEPT, int index = -1,
107 typename boost::enable_if< boost::is_convertible<Level*,
108 detail::LevelBase *> >::type * = 0);
109 template <class Stream, class Area> void route(
110 action_t action = ACCEPT, int index = -1,
111 typename boost::enable_if< boost::is_convertible<Area*,
112 detail::AreaBase *> >::type * = 0);
113 template <class Stream, class AreaClass> void route(
114 action_t action = ACCEPT, int index = -1,
115 typename boost::enable_if< boost::is_convertible<typename AreaClass::SENFLogArea *,
116 detail::AreaBase *> >::type * = 0);
117 template <class Stream, class Area, class Level> void route(
118 action_t action = ACCEPT, int index = -1,
119 typename boost::enable_if< boost::is_convertible<Area *,
120 detail::AreaBase *> >::type * = 0);
121 template <class Stream, class AreaClass, class Level> void route(
122 action_t action = ACCEPT, int index = -1,
123 typename boost::enable_if< boost::is_convertible<typename AreaClass::SENFLogArea *,
124 detail::AreaBase *> >::type * = 0);
126 void route(std::string const & stream, std::string const & area = "",
127 unsigned level = NONE::value, action_t action = ACCEPT, int index = -1);
129 template <class Stream> void unroute(
130 action_t action = ACCEPT);
131 template <class Stream, class Level> void unroute(
132 action_t action = ACCEPT,
133 typename boost::enable_if< boost::is_convertible<Level*,
134 detail::LevelBase *> >::type * = 0);
135 template <class Stream, class Area> void unroute(
136 action_t action = ACCEPT,
137 typename boost::enable_if< boost::is_convertible<Area*,
138 detail::AreaBase *> >::type * = 0);
139 template <class Stream, class AreaClass> void unroute(
140 action_t action = ACCEPT,
141 typename boost::enable_if< boost::is_convertible<typename AreaClass::SENFLogArea *,
142 detail::AreaBase *> >::type * = 0);
143 template <class Stream, class Area, class Level> void unroute(
144 action_t action = ACCEPT,
145 typename boost::enable_if< boost::is_convertible<Area*,
146 detail::AreaBase *> >::type * = 0);
147 template <class Stream, class AreaClass, class Level> void unroute(
148 action_t action = ACCEPT,
149 typename boost::enable_if< boost::is_convertible<typename AreaClass::SENFLogArea *,
150 detail::AreaBase *> >::type * = 0);
152 void unroute(std::string const & stream, std::string const & area = "",
153 unsigned level = NONE::value, action_t action = ACCEPT);
154 void unroute(int index);
156 struct InvalidStreamException : public std::exception
157 { virtual char const * what() const throw()
158 { return "senf::log::Target::InvalidStreamException"; } };
160 struct InvalidAreaException : public std::exception
161 { virtual char const * what() const throw()
162 { return "senf::log::Target::InvalidAreaException"; } };
164 iterator begin() const;
165 iterator end() const;
168 void route(detail::StreamBase const * stream, detail::AreaBase const * area,
169 unsigned level, action_t action, int index);
170 void unroute(detail::StreamBase const * stream, detail::AreaBase const * area,
171 unsigned level, action_t action);
173 void updateRoutingCache(detail::StreamBase const * stream, detail::AreaBase const * area);
175 void write(boost::posix_time::ptime timestamp, detail::StreamBase const & stream,
176 detail::AreaBase const & area, unsigned level, std::string const & message);
182 virtual void v_write(boost::posix_time::ptime, std::string const & stream,
183 std::string const & area, unsigned level,
184 std::string const & message) = 0;
192 friend class detail::AreaBase;
195 /** \brief Target registry
197 The TargetRegistry keeps a record of all existing targets.
200 : public senf::singleton<TargetRegistry>
203 using senf::singleton<TargetRegistry>::instance;
205 void write(detail::StreamBase const & stream, detail::AreaBase const & area,
206 unsigned level, std::string msg);
209 void registerTarget(Target * target);
210 void unregisterTarget(Target * target);
212 typedef std::set<Target *> Targets;
219 template <class Stream, class Area, class Level>
220 void write(std::string msg);
224 ///////////////////////////////hh.e////////////////////////////////////////
225 #include "Target.cci"
226 //#include "Target.ct"
227 #include "Target.cti"
234 // comment-column: 40
235 // c-file-style: "senf"
236 // indent-tabs-mode: nil
237 // ispell-local-dictionary: "american"
238 // compile-command: "scons -u test"