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 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"
38 #include "../Exception.hh"
40 //#include "Target.mpp"
41 ///////////////////////////////hh.p////////////////////////////////////////
43 /** \defgroup targets Targets
45 Targets receive log messages and write them to some destination: The console, a log file, an SQL
46 DB and so on. Every target is derived from the \ref senf::log::Target base class. This base
47 class provides the target with the necessary routing infrastructure. The different targets only
48 differ in the way, they write the data.
50 \see senf::log::Target
56 namespace detail { class TargetRegistry; }
58 /** \brief Logging target base class
60 Targets are the final destination of %log messages. Every message is eventually routed to one
63 \section target_routing Routing
65 Each target manages a routing table. The message meta-data (stream, area and level) is
66 matched against this table. If an entry matches, the action associated with this entry is
67 taken (either \c ACCEPT or \c REJECT).
69 Every target manages it's own routing table. Conceptually, every routing message will be
70 routed to every target where it will then be matched against each targets routing table (the
71 implementation is more efficient and utilizes a routing cache).
73 Each routing entry consists of the following parameters
74 \li (optional) \e stream. The entry will match only messages directed at that stream
75 \li (optional) \e area. If the area is specified, only messages directed at that area are
76 matched, otherwise any area will be allowed
77 \li (optional) \e level. If the log level is specified, messages will be accepted if their
78 level is at least that value. If the value is not specified, the limit will be taken
79 from the stream's default value.
81 Each parameter (stream, area and level) has two representations: A static (compile time
82 constant) representation, which is the representation also used in the log statements, and a
83 dynamic representation, which may be used for manipulating the routing table.
85 The static representation is used, when passing routing parameters via template arguments:
87 target.route<foo::SomeStream, senf::log::NOTICE>(senf::log::Target::REJECT);
88 target.route<foo::SomeStream>();
90 The identical routing statements may be expressed using dynamic routing via:
92 target.route("foo::SomeStream", "", senf::log::NOTICE::value, senf::log::Target::REJECT);
93 target.route("foo::SomeStream");
95 The static representation has the benefit of being compile-time type checked: Invalid
96 routing parameters will be caught while compiling the code. The dynamic representation is
97 more flexible as it allows to adjust routing from user input (e.g. configuration files).
99 The different object representations are:
100 \li The \e streams is statically represented by it's name, which is the name of a class
101 defined with \ref SENF_LOG_DEFINE_STREAM. The dynamic representation is a string
102 representation of this name.
103 \li The \e area is statically represented by it's name, which again is the name of a class
104 defined with \ref SENF_LOG_DEFINE_STREAM. The dynamic representation again is a string
105 representation of this class's name. The dynamic representation represents an absent
106 area with the empty string.
107 \li The \e level is statically represented by a level class from \ref
108 loglevels. Dynamically, it is represented by an unsigned integer number, the \c value
109 member of that class.
111 \section target_impl Implementing new targets
113 To implement a new target type, you need to derive from senf::log::Target and implement the
114 single \c v_write member. This member will be called whenever a message should be output.
116 The target may process in any arbitrary way: reformat, writing it into an SQL DB, whatever
117 can be envisioned. However, there is one important limitation: The \c v_write call must not
118 block. So for more complex scenarios, additional measures must be taken (e.g. writing a %log
119 backend daemon which receives the messages via UDP and processes them). Of course, in rare
120 cases messages might be lost but this cannot be avoided.
124 class Target : private boost::noncopyable
127 ///////////////////////////////////////////////////////////////////////////
130 /** \brief Routing action
132 Every routing entry is associated with a routing action. This action is final (for this
133 target. Each target is processed independently).
136 ACCEPT /** Output message */
137 , REJECT /** Suppress message output */
140 /** \brief Target routing entry
142 A single routing entry matches messages against their \e stream, \e area and \e
143 level. If the entry matches, the given \e action is performed.
145 \see senf::log::Target
149 std::string stream() const; ///< Stream to match
150 std::string area() const; ///< Area to match (empty of unspecified)
151 unsigned level() const; ///< Level to match (senf::log::NONE::value if unspecified)
152 action_t action() const; ///< Action to take
159 bool operator==(RoutingEntry const & other);
162 RoutingEntry(detail::StreamBase const * stream, detail::AreaBase const * area,
163 unsigned level, action_t action);
165 detail::StreamBase const * stream_;
166 detail::AreaBase const * area_;
174 typedef std::vector<RoutingEntry> RIB;
177 typedef RIB::const_iterator iterator; ///< Routing table iterator
179 ///////////////////////////////////////////////////////////////////////////
180 ///\name Structors and default members
187 ///////////////////////////////////////////////////////////////////////////
193 template <class Stream, class Area, class Level> void route(
194 action_t action = ACCEPT, int index = -1); ///< Add route (static)
195 /**< Add a route for the given combination of \a Stream, \a
196 Area and \a Level. All parameters (\a Stream, \a Area
197 and \a Level) are optional (the template signature is
198 shown simplified here). Examples:
200 target.route<SomeLevel>();
201 target.route<SomeStream>();
202 target.route<SomeStream, SomeLevel>();
203 target.route<SomeStream, SomeArea>();
204 target.route<SomeStream, SomeArea, SomeLevel>();
207 See the class description for information on the \a
208 action and \a index parameters
210 \tparam Stream stream to match
211 \tparam Area area to match
212 \tparam Level level, matches messages with
213 at least the given level.
214 \param[in] action routing action to take
215 \param[in] index position of new route in the routing
220 void route(std::string const & stream, std::string const & area = "",
221 unsigned level = NONE::value, action_t action = ACCEPT, int index = -1);
222 ///< Add route (dynamic)
223 /**< Add a route for the given combination of \a stream, \a
224 area and \a level. All parameters (\a Stream, \a Area
225 and \a Level) are optional and may be omitted by
226 setting them to the empty string or the
227 senf::log::NONE::value respectively.
229 See the class description for information on the \a
230 action and \a index parameters
232 \throws InvalidStreamException if the given \a stream
233 is not found in the StreamRegistry
234 \throws InvalidAreaException if the given \a area is
235 not found in the AreaRegistry
237 \param[in] stream stream to match
238 \param[in] area area to match
239 \param[in] level level, matches messages with at least
241 \param[in] action routing action to take
242 \param[in] index position of new route in the routing
247 template <class Stream, class Area, class Level>
248 void unroute(action_t action = ACCEPT);
249 ///< Remove route (static)
250 /**< This member removes an arbitrary routing entry. The
251 template parameters are the same as for the
252 corresponding \ref route() call.
254 The routing table is searched for a route exactly
255 matching the given specification. If such a route is
256 found, it will be removed, otherwise the call will be
259 \tparam Stream stream to match
260 \tparam Area area to match
261 \tparam Level level, matches messages with
262 at least the given level.
263 \param[in] action routing action to take */
267 void unroute(std::string const & stream, std::string const & area = "",
268 unsigned level = NONE::value, action_t action = ACCEPT);
269 ///< Remove route (dynamic)
270 /**< This member removes an arbitrary routing entry. The \a
271 stream parameter is mandatory while either \a area or
272 \a level may be left unspecified by setting them to the
273 empty string or senf::log::NONE::value respectively.
275 The routing table is searched for a route exactly
276 matching the given specification. If such a route is
277 found, it will be removed, otherwise the call will be
280 \param[in] stream stream to match
281 \param[in] area area to match
282 \param[in] level level, matches messages with at least
284 \param[in] action routing action to take */
285 void unroute(int index=-1); ///< Remove route (indexed)
286 /**< This call will remove the route with the given index.
288 See the class documentation for more information on
291 \param[in] index index of routing entry to remove */
296 void route(action_t action = ACCEPT, int index = -1);
297 template <class A1, class A2>
298 void route(action_t action = ACCEPT, int index = -1);
299 template <class A1, class A2, class A3>
300 void route(action_t action = ACCEPT, int index = -1);
303 void unroute(action_t action = ACCEPT);
304 template <class A1, class A2>
305 void unroute(action_t action = ACCEPT);
306 template <class A1, class A2, class A3>
307 void unroute(action_t action = ACCEPT);
313 /** \brief Exception: Invalid stream */
314 struct InvalidStreamException : public senf::Exception
315 { InvalidStreamException()
316 : senf::Exception("senf::log::Target::InvalidStreamException"){} };
318 /** \brief Exception: Invalid area */
319 struct InvalidAreaException : public senf::Exception
320 { InvalidAreaException()
321 : senf::Exception("senf::log::Target::InvalidAreaException"){} };
323 iterator begin() const; ///< Iterator to beginning of routing table
324 iterator end() const; ///< Iterator past the end of routing table
327 void route(detail::StreamBase const * stream, detail::AreaBase const * area,
328 unsigned level, action_t action, int index);
329 void unroute(detail::StreamBase const * stream, detail::AreaBase const * area,
330 unsigned level, action_t action);
332 void updateRoutingCache(detail::StreamBase const * stream, detail::AreaBase const * area);
334 void write(boost::posix_time::ptime timestamp, detail::StreamBase const & stream,
335 detail::AreaBase const & area, unsigned level, std::string const & message);
341 virtual void v_write(boost::posix_time::ptime timestamp, std::string const & stream,
342 std::string const & area, unsigned level,
343 std::string const & message) = 0;
344 ///< Called to write out the routing message
345 /**< This member must be defined in the derived class to
346 somehow format and write the %log message.
348 Every %log message always possesses a complete set of
349 meta information (\a stream, \a area and \a level).
351 \note This member must \e not block since it may be
352 called from any unknown context. This prohibits
353 simple logging over NFS or many other network
356 \param[in] timestamp %log message timing information
357 \param[in] stream message stream
358 \param[in] area message area
359 \param[in] level message level
360 \param[in] message the message string */
368 friend class detail::AreaBase;
369 friend class detail::TargetRegistry;
372 /** \brief Log message time source abstract base class
374 Instances derived from TimeSource provide the Logging library with the current date/time
375 value. The \c operator() member must be implemented to return the current universal time
378 A new TimeSource may be installed using \ref senf::log::timeSource().
384 virtual ~TimeSource();
385 virtual boost::posix_time::ptime operator()() const = 0;
388 /** \brief Default %log message time source
390 This time source is installed by default and uses gettimeofday() (via the Boost.DateTime
391 library) to get the current universal time.
395 struct SystemTimeSource : public TimeSource
397 virtual boost::posix_time::ptime operator()() const;
400 /** \brief Change %log message time source
402 Set the %log message time source to \a source. The logging library will take ownership of \e
403 source and will take care to free it, if necessary.
405 Since the time source class will in almost all cases be default constructible, see the
406 template overload for a simpler interface.
410 void timeSource(std::auto_ptr<TimeSource> source);
412 /** \brief Change %log message time source
414 Set the %log message time source to (an instance of) \a Source. \a Source must be default
415 constructible, otherwise use the non-template senf::log::timeSource() overload.
419 template <class Source> void timeSource();
423 ///////////////////////////////hh.e////////////////////////////////////////
424 #include "Target.cci"
425 //#include "Target.ct"
426 #include "Target.cti"
433 // comment-column: 40
434 // c-file-style: "senf"
435 // indent-tabs-mode: nil
436 // ispell-local-dictionary: "american"
437 // compile-command: "scons -u test"