Utils/Console: Fix rename Target::clear() to Target::flush() to fix name clash (Targe...
[senf.git] / Utils / Logger / Target.cc
index 9740cec..068631c 100644 (file)
@@ -45,8 +45,8 @@ prefix_ senf::log::Target::Target()
 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
+        // This is slow but simplifies the area cache handling and removing a target should be
+        // relatively seldom
         RIB::reverse_iterator i (rib_.rbegin());
         unroute(i->stream_, i->area_, i->level_, i->action_);
     }
@@ -85,6 +85,8 @@ prefix_ void senf::log::Target::unroute(std::string const & stream, std::string
 
 prefix_ void senf::log::Target::unroute(int index)
 {
+    if (rib_.empty())
+        return;
     RIB::iterator i;
     if (index < 0) {
         if (RIB::size_type(-index) >= rib_.size())
@@ -110,6 +112,17 @@ prefix_ void senf::log::Target::unroute(int index)
         updateRoutingCache(entry.stream_, entry.area_);
 }
 
+prefix_ void senf::log::Target::flush()
+{
+    RIB old;
+    rib_.swap(old);
+    RIB::const_iterator i (old.begin());
+    RIB::const_iterator const i_end (old.end());
+    for (; i != i_end; ++i)
+        if (i->action_ == ACCEPT)
+            updateRoutingCache(i->stream_, i->area_);
+}
+
 ////////////////////////////////////////
 // private members
 
@@ -141,10 +154,10 @@ prefix_ void senf::log::Target::route(detail::StreamBase const * stream,
 }
 
 prefix_ void senf::log::Target::unroute(detail::StreamBase const * stream,
-                                        detail::AreaBase const * area, unsigned level, 
+                                        detail::AreaBase const * area, unsigned level,
                                         action_t action)
 {
-    RIB::iterator i = std::find(rib_.begin(), rib_.end(), 
+    RIB::iterator i = std::find(rib_.begin(), rib_.end(),
                                 RoutingEntry(stream, area, level, action));
     if (i != rib_.end())
         unroute(std::distance(rib_.begin(), i));
@@ -174,7 +187,7 @@ prefix_ void senf::log::Target::updateRoutingCache(detail::StreamBase const * st
         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_);
+            unsigned l (i->level_ == NONE::value ? stream->defaultRuntimeLimit() : i->level_);
             if (l < limit)
                 limit = l;
         }
@@ -184,7 +197,7 @@ prefix_ void senf::log::Target::updateRoutingCache(detail::StreamBase const * st
         area->updateRoutingCache(*this, *stream, limit);
 }
 
-prefix_ void senf::log::Target::write(boost::posix_time::ptime timestamp,
+prefix_ void senf::log::Target::write(time_type timestamp,
                                       detail::StreamBase const & stream,
                                       detail::AreaBase const & area, unsigned level,
                                       std::string const & message)
@@ -194,7 +207,7 @@ prefix_ void senf::log::Target::write(boost::posix_time::ptime timestamp,
     for (; i != i_end; ++i)
         if ( (! i->stream_ || i->stream_ == &stream) &&
              (! i->area_ || i->area_ == &area) &&
-             (i->level_ == NONE::value ? i->stream_->defaultRuntimeLimit() : i->level_) <= level ) {
+             (i->level_ == NONE::value ? stream.defaultRuntimeLimit() : i->level_) <= level ) {
             if (i->action_ == ACCEPT)
                 v_write(timestamp, stream.v_name(), area.v_name(), level, message);
             return;
@@ -206,37 +219,33 @@ prefix_ void senf::log::Target::write(boost::posix_time::ptime timestamp,
 
 prefix_ void senf::log::detail::TargetRegistry::write(StreamBase const & stream,
                                                       AreaBase const & area, unsigned level,
-                                                      std::string msg)
+                                                      std::string const & msg)
 {
     if (fallbackRouting_) {
         if (level >= stream.defaultRuntimeLimit())
-            static_cast<Target &>(ConsoleTarget::instance()).v_write( 
-                (*timeSource_)(), stream.v_name(), area.v_name(), level, msg );
+            static_cast<Target &>(ConsoleTarget::instance()).v_write(
+                TimeSource::now(), stream.v_name(), area.v_name(), level, msg );
     }
     else
-        area.write( (*timeSource_)(), stream, level, msg );
+        area.write( TimeSource::now(), stream, level, msg );
 }
-
 ///////////////////////////////////////////////////////////////////////////
-// senf::log::TimeSource
+// namespace members
 
-prefix_ senf::log::TimeSource::~TimeSource()
-{}
-
-///////////////////////////////////////////////////////////////////////////
-// senf::log::SystemTimeSource
-
-prefix_ boost::posix_time::ptime senf::log::SystemTimeSource::operator()()
-    const
+prefix_ std::ostream & senf::log::operator<<(std::ostream & os, senf::log::Target::action_t const & action)
 {
-    return boost::posix_time::microsec_clock::universal_time();
+    if( action == Target::ACCEPT) os << "ACCEPT";
+    else if( action == Target::REJECT) os << "REJECT";
+    else os << "unknown action";
+    return os;
 }
 
+
 ///////////////////////////////cc.e////////////////////////////////////////
 #undef prefix_
 //#include "Target.mpp"
 
-\f
+
 // Local Variables:
 // mode: c++
 // fill-column: 100