Logging target base class. More...
#include <senf/Utils/Logger/Target.hh>
Classes | |
struct | InvalidAreaException |
Exception: Invalid area. More... | |
struct | InvalidStreamException |
Exception: Invalid stream. More... | |
struct | RoutingEntry |
Target routing entry. More... | |
Public Types | |
enum | action_t { ACCEPT, REJECT } |
Routing action. More... | |
typedef RIB::const_iterator | iterator |
Routing table iterator. More... | |
typedef RIB::size_type | size_type |
Public Member Functions | |
iterator | begin () const |
Iterator to beginning of routing table. More... | |
iterator | end () const |
Iterator past the end of routing table. More... | |
RoutingEntry const & | operator[] (size_type i) const |
Access routing entry. More... | |
size_type | size () const |
Number of routing table entries. More... | |
bool | empty () const |
true , if routing table empty, false otherwise More... | |
void | flush () |
Clear routing table. More... | |
senf::console::ScopedDirectory & | consoleDir () |
Get console/config directory. More... | |
Protected Member Functions | |
virtual void | v_write (time_type timestamp, std::string const &stream, std::string const &area, unsigned level, std::string const &message)=0 |
Called to write out the routing message. More... | |
Related Functions | |
(Note that these are not member functions.) | |
std::ostream & | operator<< (std::ostream &os, Target::action_t const &action) |
Write route action. More... | |
Structors and default members | |
Target (std::string const &name) | |
virtual | ~Target () |
Routing | |
template<class Stream , class Area , class Level > | |
void | route (action_t action=ACCEPT, int index=-1) |
Add route (static) More... | |
void | route (std::string const &stream, std::string const &area="", unsigned level=NONE::value, action_t action=ACCEPT, int index=-1) |
Add route (dynamic) More... | |
template<class Stream , class Area , class Level > | |
void | unroute (action_t action=ACCEPT) |
Remove route (static) More... | |
void | unroute (std::string const &stream, std::string const &area="", unsigned level=NONE::value, action_t action=ACCEPT) |
Remove route (dynamic) More... | |
void | unroute (int index=-1) |
Remove route (indexed) More... | |
Logging target base class.
Targets are the final destination of log messages. Every message is eventually routed to one or several targets.
Each target manages a routing table. The message meta-data (stream, area and level) is matched against this table. If an entry matches, the action associated with this entry is taken (either \c ACCEPT or \c REJECT). Every target manages it's own routing table. Conceptually, every message will be routed to every target where it will then be matched against each targets routing table (the implementation is more efficient and utilizes a routing cache). Each routing entry consists of the following parameters \li (optional) \e stream. If specified, the entry will match only messages directed at that stream \li (optional) \e area. If the area is specified, only messages directed at that area are matched, otherwise any area will be allowed \li (optional) \e level. If the %log level is specified, messages will be accepted if their level is at least that value. If the value is not specified, the limit will be taken from the stream's default value. Each parameter (stream, area and level) has two representations: A static (compile time constant) representation, which is the representation also used in the %log statements, and a dynamic (runtime) representation. The static representation is used, when passing routing parameters via template arguments:
The identical routing statements may be expressed using dynamic routing via:
The static representation has the benefit of being compile-time type checked: Invalid routing parameters will be caught while compiling the code. The dynamic representation is more flexible as it allows to adjust routing from user input (e.g. configuration files).
The different object representations are:
value
member of that class.The routing table is processed from first route to last route, the first matching entry determines the fate of a log messages. Therefore, the ordering of routing entries is important. If no position is explicitly specified, new routing entries are added to the end of the routing table. All routing statements however take an index as optional argument to explicitly specify the position of the new routing entry. The index value starts from 0 for the first route. The value gives the position the new routing entry will have after it has been added. An index of 0 will thus insert the new routing entry at the beginning of the table. Negative values count from the back, -1 being the last entry.
To implement a new target type, you need to derive from senf::log::Target and implement the single \c v_write member. This member will be called whenever a message should be output. The target may process the message in any arbitrary way: reformat it, write it into an SQL DB, whatever can be envisioned. However, there is one important limitation: The \c v_write call must \e not block. So for more complex scenarios, additional measures must be taken (e.g. writing a %log backend daemon which receives the messages via UDP and processes them). Of course, in rare cases messages might be lost but this cannot be avoided. \see \ref targets
typedef RIB::size_type senf::log::Target::size_type |
|
explicit |
iterator senf::log::Target::begin | ( | ) | const |
Iterator to beginning of routing table.
senf::console::ScopedDirectory & senf::log::Target::consoleDir | ( | ) |
bool senf::log::Target::empty | ( | ) | const |
true
, if routing table empty, false
otherwise
iterator senf::log::Target::end | ( | ) | const |
Iterator past the end of routing table.
RoutingEntry const& senf::log::Target::operator[] | ( | size_type | i | ) | const |
Access routing entry.
void senf::log::Target::route | ( | action_t | action = ACCEPT , |
int | index = -1 |
||
) |
Add route (static)
Add a route for the given combination of Stream, Area and Level. All parameters (Stream, Area and Level) are optional (the template signature is shown simplified here). So possible commands are:
See the class description for information on the \a action and \a index parameters \tparam Stream stream to match \tparam Area area to match \tparam Level level, matches messages with at least the given level. \param[in] action routing action to take \param[in] index position of new route in the routing table
void senf::log::Target::route | ( | std::string const & | stream, |
std::string const & | area = "" , |
||
unsigned | level = NONE::value , |
||
action_t | action = ACCEPT , |
||
int | index = -1 |
||
) |
Add route (dynamic)
Add a route for the given combination of stream, area and level. All parameters (Stream, Area and Level) are optional and may be omitted by setting them to the empty string or the senf::log::NONE::value respectively.
See the class description for information on the action and index parameters
InvalidStreamException | if the given stream is not found in the StreamRegistry |
InvalidAreaException | if the given area is not found in the AreaRegistry |
[in] | stream | stream to match |
[in] | area | area to match |
[in] | level | level, matches messages with at least the given level. |
[in] | action | routing action to take |
[in] | index | position of new route in the routing table |
size_type senf::log::Target::size | ( | ) | const |
Number of routing table entries.
void senf::log::Target::unroute | ( | action_t | action = ACCEPT | ) |
Remove route (static)
This member removes an arbitrary routing entry. The template parameters are the same as for the corresponding route() call.
The routing table is searched for a route exactly matching the given specification. If such a route is found, it will be removed, otherwise the call will be ignored
Stream | stream to match |
Area | area to match |
Level | level, matches messages with at least the given level. |
[in] | action | routing action to take |
void senf::log::Target::unroute | ( | std::string const & | stream, |
std::string const & | area = "" , |
||
unsigned | level = NONE::value , |
||
action_t | action = ACCEPT |
||
) |
Remove route (dynamic)
This member removes an arbitrary routing entry. The stream parameter is mandatory while either area or level may be left unspecified by setting them to the empty string or senf::log::NONE::value respectively.
The routing table is searched for a route exactly matching the given specification. If such a route is found, it will be removed, otherwise the call will be ignored
[in] | stream | stream to match |
[in] | area | area to match |
[in] | level | level, matches messages with at least the given level. |
[in] | action | routing action to take |
void senf::log::Target::unroute | ( | int | index = -1 | ) |
|
protectedpure virtual |
Called to write out the routing message.
This member must be defined in the derived class to somehow format and write the log message.
Every log message always possesses a complete set of meta information (stream, area and level).
[in] | timestamp | log message timing information |
[in] | stream | message stream |
[in] | area | message area |
[in] | level | message level |
[in] | message | the message string |
Implemented in senf::log::IOStreamTarget.
|
related |
Write route action.