Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#27 max_size for StringQueueAppender

open
nobody
Appender (3)
5
2009-04-09
2009-04-09
Mario Frasca
No

I am developing a server which will be placed on a remote system, and we will be accessing it via xmlrpc.
we want to retrieve its loggging events and I was considering using the StringQueueAppender.
the only one requirement this Appender does not meet is that memory usage does not overflow all limits.
it is such a limited enhancement that I don't feel like subclassing StringQueueAppender, I would prefer an optional parameter to the constructor.

I have a patch for the cvs version; the changes are limited:
Index: include/log4cpp/StringQueueAppender.hh
===================================================================
RCS file: /cvsroot/log4cpp/log4cpp/include/log4cpp/StringQueueAppender.hh,v
retrieving revision 1.4
diff -u -r1.4 StringQueueAppender.hh
--- include/log4cpp/StringQueueAppender.hh 11 Feb 2002 21:52:48 -0000 1.4
+++ include/log4cpp/StringQueueAppender.hh 9 Apr 2009 13:26:06 -0000
@@ -26,7 +26,7 @@
class LOG4CPP_EXPORT StringQueueAppender : public LayoutAppender {
public:

- StringQueueAppender(const std::string& name);
+ StringQueueAppender(const std::string& name, const size_t max_size=0);
virtual ~StringQueueAppender();

virtual bool reopen();
@@ -66,6 +66,7 @@
virtual void _append(const LoggingEvent& event);

std::queue<std::string> _queue;
+ size_t _max_size;
};
}

Index: src/StringQueueAppender.cpp

RCS file: /cvsroot/log4cpp/log4cpp/src/StringQueueAppender.cpp,v
retrieving revision 1.5
diff -u -r1.5 StringQueueAppender.cpp
--- src/StringQueueAppender.cpp 26 Oct 2002 18:30:55 -0000 1.5
+++ src/StringQueueAppender.cpp 9 Apr 2009 13:26:07 -0000
@@ -12,8 +12,9 @@

namespace log4cpp {

- StringQueueAppender::StringQueueAppender(const std::string& name) :
- LayoutAppender(name) {
+ StringQueueAppender::StringQueueAppender(const std::string& name,
+ const size_t max_size) :
+ LayoutAppender(name), _max_size(max_size) {
}

StringQueueAppender::~StringQueueAppender() {
@@ -26,6 +27,10 @@

void StringQueueAppender::_append(const LoggingEvent& event) {
_queue.push(_getLayout().format(event));
+ if (_max_size) {
+ while (_queue.size() > _max_size)
+ _queue.pop();
+ }
}

bool StringQueueAppender::reopen() {

Discussion