00001 #include <log4cpp/BufferingAppender.hh>
00002 #include <algorithm>
00003 #include <sstream>
00004 #include <memory>
00005
00006 namespace log4cpp
00007 {
00008 BufferingAppender::BufferingAppender(const std::string name, unsigned long max_size,
00009 std::auto_ptr<Appender> sink, std::auto_ptr<TriggeringEventEvaluator> evaluator)
00010 :LayoutAppender(name), max_size_(max_size), sink_(sink), evaluator_(evaluator), lossy_(false)
00011 {
00012 max_size_ = (std::max)(1UL, max_size_);
00013 }
00014
00015 void BufferingAppender::_append(const LoggingEvent& event)
00016 {
00017 if (queue_.size() == max_size_)
00018 {
00019 if (lossy_)
00020 queue_.pop_back();
00021 else
00022 dump();
00023 }
00024
00025 queue_.push_front(event);
00026
00027 if (evaluator_->eval(event))
00028 {
00029 dump();
00030 queue_.clear();
00031 }
00032 }
00033
00034 static const std::string EMPTY;
00035
00036 void BufferingAppender::dump()
00037 {
00038 Layout& layout = _getLayout();
00039 std::ostringstream s;
00040
00041 for(queue_t::reverse_iterator i = queue_.rbegin(), last = queue_.rend(); i != last; ++i)
00042 s << layout.format(*i);
00043
00044 LoggingEvent event(EMPTY, s.str(), EMPTY, Priority::NOTSET);
00045 sink_->doAppend(event);
00046 }
00047 }
00048