Parcourir la source

[2445] Address smaller points in logbuffer code

- update README
- copy vector of event on flush
- only use very basic functions in case of flush-on-destruct
Jelte Jansen il y a 12 ans
Parent
commit
1378932f02

+ 5 - 5
src/lib/log/README

@@ -366,11 +366,11 @@ first time a logger specification is processed. This way the program can
 use logging before even processing its logging configuration. As soon as any
 use logging before even processing its logging configuration. As soon as any
 specification is processed (even an empty one), the buffered log messages will
 specification is processed (even an empty one), the buffered log messages will
 be flushed according to the specification. Note that if this option is used,
 be flushed according to the specification. Note that if this option is used,
-the program SHOULD call one of the LoggerManager's process() calls. If the
-program exits before this is done, all log messages are dumped in a shortened
-format to stdout (so that no messages get lost). If you are using the built-in
-logging configuration handling in ModuleCCSession, this is automatically
-handled.
+the program SHOULD call one of the LoggerManager's process() calls (if you
+are using the built-in logging configuration handling in ModuleCCSession,
+this is automatically handled). If the program exits before this is done,
+all log messages are dumped in a raw format to stdout (so that no messages
+get lost).
 
 
 Variant #2, Used by Unit Tests
 Variant #2, Used by Unit Tests
 ------------------------------
 ------------------------------

+ 10 - 9
src/lib/log/log_buffer_impl.cc

@@ -16,7 +16,7 @@
 
 
 #include <log4cplus/loglevel.h>
 #include <log4cplus/loglevel.h>
 #include <boost/scoped_ptr.hpp>
 #include <boost/scoped_ptr.hpp>
-#include <iostream>
+#include <cstdio>
 
 
 namespace isc {
 namespace isc {
 namespace log {
 namespace log {
@@ -65,23 +65,24 @@ LogBuffer::flushStdout() {
     // be a good idea; as we can't reliably know whether in what
     // be a good idea; as we can't reliably know whether in what
     // state the logger instance is now (or what the specific logger's
     // state the logger instance is now (or what the specific logger's
     // settings were).
     // settings were).
-    // So we print a slightly shortened format (it really only excludes
-    // the time and the pid)
+    // So we print a raw format (it excludes the time and the pid, and
+    // it prints severity as a number)
     LoggerEventPtrList::const_iterator it;
     LoggerEventPtrList::const_iterator it;
-    const log4cplus::LogLevelManager& manager =
-        log4cplus::getLogLevelManager();
     for (it = stored_.begin(); it != stored_.end(); ++it) {
     for (it = stored_.begin(); it != stored_.end(); ++it) {
-        std::cout << manager.toString((*it)->getLogLevel()) << " " <<
-                     "[" << (*it)->getLoggerName() << "] " <<
-                     (*it)->getMessage() << std::endl;
+        std::printf("Severity=%d [%s]: %s\n", (*it)->getLogLevel(),
+                    (*it)->getLoggerName().c_str(),
+                    (*it)->getMessage().c_str());
     }
     }
     stored_.clear();
     stored_.clear();
 }
 }
 
 
 void
 void
 LogBuffer::flush() {
 LogBuffer::flush() {
+    LoggerEventPtrList stored_copy;
+    stored_.swap(stored_copy);
+
     LoggerEventPtrList::const_iterator it;
     LoggerEventPtrList::const_iterator it;
-    for (it = stored_.begin(); it != stored_.end(); ++it) {
+    for (it = stored_copy.begin(); it != stored_copy.end(); ++it) {
         log4cplus::Logger logger =
         log4cplus::Logger logger =
             log4cplus::Logger::getInstance((*it)->getLoggerName());
             log4cplus::Logger::getInstance((*it)->getLoggerName());
 
 

+ 1 - 0
src/lib/log/log_buffer_impl.h

@@ -85,6 +85,7 @@ public:
     /// Once this method has been called, no more events can be
     /// Once this method has been called, no more events can be
     /// added trough calls to \c add(); if \c add() is called after flush(),
     /// added trough calls to \c add(); if \c add() is called after flush(),
     /// an exception will be raised.
     /// an exception will be raised.
+    /// If flush for any reason fails, the remaining events are dropped.
     void flush();
     void flush();
 
 
     /// \brief Returns number of stored events
     /// \brief Returns number of stored events

+ 2 - 3
src/lib/log/logger_manager_impl.cc

@@ -58,9 +58,8 @@ LoggerManagerImpl::processEnd() {
 // add output specifications.
 // add output specifications.
 void
 void
 LoggerManagerImpl::processSpecification(const LoggerSpecification& spec) {
 LoggerManagerImpl::processSpecification(const LoggerSpecification& spec) {
-    log4cplus::Logger logger;
-    // If this is an 'empty' specification, just set the root logger
-    logger = log4cplus::Logger::getInstance(expandLoggerName(spec.getName()));
+    log4cplus::Logger logger = log4cplus::Logger::getInstance(
+                                   expandLoggerName(spec.getName()));
 
 
     // Set severity level according to specification entry.
     // Set severity level according to specification entry.
     logger.setLogLevel(LoggerLevelImpl::convertFromBindLevel(
     logger.setLogLevel(LoggerLevelImpl::convertFromBindLevel(

+ 3 - 3
src/lib/log/tests/buffer_logger_test.sh.in

@@ -46,9 +46,9 @@ passfail $?
 
 
 echo -n  "   - Buffer excluding process() call: "
 echo -n  "   - Buffer excluding process() call: "
 cat > $tempfile << .
 cat > $tempfile << .
-INFO [buffertest.log] LOG_BAD_SEVERITY unrecognized log severity: info
-DEBUG [buffertest.log] LOG_BAD_DESTINATION unrecognized log destination: debug-50
-INFO [buffertest.log] LOG_BAD_SEVERITY unrecognized log severity: info
+Severity=20000 [buffertest.log]: LOG_BAD_SEVERITY unrecognized log severity: info
+Severity=10000 [buffertest.log]: LOG_BAD_DESTINATION unrecognized log destination: debug-50
+Severity=20000 [buffertest.log]: LOG_BAD_SEVERITY unrecognized log severity: info
 .
 .
 ./buffer_logger_test -n 2>&1 | diff $tempfile -
 ./buffer_logger_test -n 2>&1 | diff $tempfile -
 passfail $?
 passfail $?