Utils/Logger: Complete route caching
[senf.git] / Utils / Logger / Target.cc
index 651671e..11142a7 100644 (file)
 ///////////////////////////////////////////////////////////////////////////
 // senf::log::Target
 
+prefix_ senf::log::Target::Target()
+{
+    TargetRegistry::instance().registerTarget(this);
+}
+
 prefix_ senf::log::Target::~Target()
-{}
+{
+    while( ! rib_.empty()) {
+        // This is terribly slow but simplifies the area cache handling and removing a target should
+        // be quite seldom
+        RIB::reverse_iterator i (rib_.rbegin());
+        unroute(i->stream, i->area, i->level, i->action);
+    }
+    TargetRegistry::instance().unregisterTarget(this);
+}
+
+prefix_ void senf::log::Target::updateRoutingCache(detail::StreamBase const * stream,
+                                                   detail::AreaBase const * area)
+{
+    if (! stream) {
+        StreamRegistry::Registry::iterator i (StreamRegistry::instance().registry_.begin());
+        StreamRegistry::Registry::iterator const i_end (StreamRegistry::instance().registry_.end());
+        for (; i != i_end ; ++i)
+            updateRoutingCache(i->second, area);
+        return;
+    }
+    if (! area) {
+        AreaRegistry::Registry::iterator i (AreaRegistry::instance().registry_.begin());
+        AreaRegistry::Registry::iterator const i_end (AreaRegistry::instance().registry_.end());
+        for (; i != i_end ; ++i)
+            updateRoutingCache(stream, i->second);
+        return;
+    }
+    unsigned limit (DISABLED::value);
+    RIB::iterator i (rib_.begin());
+    RIB::iterator const i_end (rib_.end());
+    for(; i != i_end; ++i)
+        if ( (! i->stream || i->stream == stream) &&
+             (! i->area || i->area == area) &&
+             i->action == ACCEPT ) {
+            unsigned l (i->level == NONE::value ? i->stream->defaultRuntimeLimit() : i->level);
+            if (l < limit)
+                limit = l;
+        }
+    if (limit == DISABLED::value)
+        area->removeRoutingCache(*this, *stream);
+    else
+        area->updateRoutingCache(*this, *stream, limit);
+}
 
-prefix_ void senf::log::Target::write(detail::StreamBase const & stream, 
-                                      detail::AreaBase const & area,
-                                      unsigned level, std::string const & message)
+prefix_ void senf::log::Target::write(boost::posix_time::ptime timestamp,
+                                      detail::StreamBase const & stream,
+                                      detail::AreaBase const & area, unsigned level,
+                                      std::string const & message)
 {
     RIB::iterator i (rib_.begin());
     RIB::iterator const i_end (rib_.end());
     for (; i != i_end; ++i)
-        if ( ( ! i->stream || i->stream == &stream ) &&
-             ( ! i->area || i->area == &area ) &&
-             i->level <= level ) {
-            v_write(stream.v_name(), area.v_name(), level, message);
+        if ( (! i->stream || i->stream == &stream) &&
+             (! i->area || i->area == &area) &&
+             (i->level == NONE::value ? i->stream->defaultRuntimeLimit() : i->level) <= level ) {
+            if (i->action == ACCEPT)
+                v_write(timestamp, stream.v_name(), area.v_name(), level, message);
             return;
         }
 }
 
+///////////////////////////////////////////////////////////////////////////
+// senf::log::TargetRegistry
+
+prefix_ void senf::log::TargetRegistry::write(detail::StreamBase const & stream,
+                                              detail::AreaBase const & area, unsigned level,
+                                              std::string msg)
+{
+    boost::posix_time::ptime timestamp (boost::posix_time::microsec_clock::universal_time());
+    area.write(timestamp, stream, level, msg);
+}
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "Target.mpp"