Target.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #ifndef HH_SENF_Utils_Logger_Target_
18 #define HH_SENF_Utils_Logger_Target_ 1
19 
20 // Custom includes
21 #include <vector>
22 #include <boost/noncopyable.hpp>
23 #include <senf/Utils/Exception.hh>
25 #include "TimeSource.hh"
26 #include "Levels.hh"
27 
28 //#include "Target.mpp"
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
41 namespace senf {
42 namespace log {
43 
44  namespace detail { class TargetRegistry; }
45  namespace detail { struct AreaBase; }
46  namespace detail { struct LogParameters; }
47  namespace detail { struct StreamBase; }
48 
133  class Target : private boost::noncopyable
134  {
135  public:
136  //-////////////////////////////////////////////////////////////////////////
137  // Types
138 
144  enum action_t {
145  ACCEPT
146  , REJECT
147  };
148 
157  {
158  std::string stream() const;
159  std::string area() const;
160  unsigned level() const;
161  action_t action() const;
162 
163 # ifdef DOXYGEN
164  private:
165 # endif
166 
167  RoutingEntry();
168  bool operator==(RoutingEntry const & other) const;
169 
170  private:
171  RoutingEntry(detail::StreamBase const * stream, detail::AreaBase const * area,
172  unsigned level, action_t action);
173 
174  detail::StreamBase const * stream_;
175  detail::AreaBase const * area_;
176  unsigned level_;
177  action_t action_;
178 
179  friend class Target;
180  };
181 
182  private:
183  typedef std::vector<RoutingEntry> RIB;
184 
185  public:
186  typedef RIB::const_iterator iterator;
187  typedef RIB::size_type size_type;
188 
189  //-////////////////////////////////////////////////////////////////////////
191  //\{
192 
193  explicit Target(std::string const & name);
194  virtual ~Target();
195 
196  //\}
197  //-////////////////////////////////////////////////////////////////////////
199  //\{
200 
201 # ifdef DOXYGEN
202 
203  template <class Stream, class Area, class Level> void route(
204  action_t action = ACCEPT, int index = -1);
205 
231 # endif
232 
233  void route(std::string const & stream, std::string const & area = "",
234  unsigned level = NONE::value, action_t action = ACCEPT, int index = -1);
236 
258 # ifdef DOXYGEN
259 
260  template <class Stream, class Area, class Level>
261  void unroute(action_t action = ACCEPT);
263 
278 # endif
279 
280  void unroute(std::string const & stream, std::string const & area = "",
281  unsigned level = NONE::value, action_t action = ACCEPT);
283 
298  void unroute(int index=-1);
299 
306 # ifndef DOXYGEN
307 
308  void route(action_t action = ACCEPT, int index = -1);
309  template <class A1>
310  void route(action_t action = ACCEPT, int index = -1);
311  template <class A1, class A2>
312  void route(action_t action = ACCEPT, int index = -1);
313  template <class A1, class A2, class A3>
314  void route(action_t action = ACCEPT, int index = -1);
315 
316  void unroute(action_t action = ACCEPT);
317  template <class A1>
318  void unroute(action_t action = ACCEPT);
319  template <class A1, class A2>
320  void unroute(action_t action = ACCEPT);
321  template <class A1, class A2, class A3>
322  void unroute(action_t action = ACCEPT);
323 
324 # endif
325 
326  //\}
327 
331  : senf::Exception("senf::log::Target::InvalidStreamException") {} };
332 
336  : senf::Exception("senf::log::Target::InvalidAreaException") {} };
337 
338  iterator begin() const;
339  iterator end() const;
340 
341  RoutingEntry const & operator[](size_type i) const;
342 
343  size_type size() const;
344  bool empty() const;
345 
346  void flush();
347 
349 
350  private:
351  void route(detail::StreamBase const * stream, detail::AreaBase const * area,
352  unsigned level, action_t action, int index);
353  void unroute(detail::StreamBase const * stream, detail::AreaBase const * area,
354  unsigned level, action_t action);
355 
356  void updateRoutingCache(detail::StreamBase const * stream, detail::AreaBase const * area);
357 
358  void write(time_type timestamp, detail::StreamBase const & stream,
359  detail::AreaBase const & area, unsigned level, std::string const & message);
360 
361  void consoleList(std::ostream & os);
362  void consoleRoute(int index, detail::LogParameters const & pm, action_t action);
363  void consoleUnroute(detail::LogParameters const & pm, action_t action);
364 
365 # ifdef DOXYGEN
366  protected:
367 # endif
368 
369  virtual void v_write(time_type timestamp, std::string const & stream,
370  std::string const & area, unsigned level,
371  std::string const & message) = 0;
373 
390 # ifdef DOXYGEN
391  private:
392 # endif
393 
394  RIB rib_;
395 
396  console::LazyDirectory consoleDir_;
397 
398  friend struct detail::AreaBase;
399  friend class detail::TargetRegistry;
400  };
401 
405  std::ostream & operator<<(std::ostream & os, Target::action_t const & action);
406 
407 }}
408 
409 //-/////////////////////////////////////////////////////////////////////////////////////////////////
410 #include "Target.cci"
411 //#include "Target.ct"
412 #include "Target.cti"
413 #endif
414 
415 
416 // Local Variables:
417 // mode: c++
418 // fill-column: 100
419 // comment-column: 40
420 // c-file-style: "senf"
421 // indent-tabs-mode: nil
422 // ispell-local-dictionary: "american"
423 // compile-command: "scons -u test"
424 // End:
bool empty()
Exception: Invalid area.
Definition: Target.hh:334
static unsigned const value
Definition: Levels.hh:99
std::ostream & operator<<(std::ostream &os, Packet const &packet)
action_t
Routing action.
Definition: Target.hh:144
bool operator==(ArrayValueParserBase< Parser, ValueType > const &parser, ValueType const &value)
Exception: Invalid stream.
Definition: Target.hh:329
console::ScopedDirectory & consoleDir()
Target routing entry.
Definition: Target.hh:156
Levels public header.
TimeSource public header.
RIB::const_iterator iterator
Routing table iterator.
Definition: Target.hh:186
config::time_type time_type
Definition: TimeSource.hh:31
Logging target base class.
Definition: Target.hh:133
RIB::size_type size_type
Definition: Target.hh:187