Browse Source

[trac976] Add expandLoggerName function

Added to root_logger_name.* which was renamed to the more general
logger_name.*.  Updated files to cope with revised name.  Also
uncommented some unit tests that must have been commented out
from the Makefile.am when chasing a problem as some point in the past.
Stephen Morris 14 years ago
parent
commit
e006dc3e92

+ 1 - 1
src/lib/log/Makefile.am

@@ -17,6 +17,7 @@ liblog_la_SOURCES += logger_level.cc logger_level.h
 liblog_la_SOURCES += logger_level_impl.cc logger_level_impl.h
 liblog_la_SOURCES += logger_manager.cc logger_manager.h
 liblog_la_SOURCES += logger_manager_impl.cc logger_manager_impl.h
+liblog_la_SOURCES += logger_name.cc logger_name.h
 liblog_la_SOURCES += logger_specification.h
 liblog_la_SOURCES += logger_support.cc logger_support.h
 liblog_la_SOURCES += macros.h
@@ -27,7 +28,6 @@ liblog_la_SOURCES += message_initializer.cc message_initializer.h
 liblog_la_SOURCES += message_reader.cc message_reader.h
 liblog_la_SOURCES += message_types.h
 liblog_la_SOURCES += output_option.cc output_option.h
-liblog_la_SOURCES += root_logger_name.cc root_logger_name.h
 
 EXTRA_DIST  = README
 EXTRA_DIST += impldef.mes

+ 1 - 1
src/lib/log/logger.cc

@@ -17,9 +17,9 @@
 
 #include <log/logger.h>
 #include <log/logger_impl.h>
+#include <log/logger_name.h>
 #include <log/message_dictionary.h>
 #include <log/message_types.h>
-#include <log/root_logger_name.h>
 
 #include <util/strutil.h>
 

+ 10 - 7
src/lib/log/logger_impl.cc

@@ -23,14 +23,13 @@
 
 #include <log4cplus/configurator.h>
 
-#include <log/root_logger_name.h>
 #include <log/logger.h>
+#include <log/logger_impl.h>
 #include <log/logger_level.h>
 #include <log/logger_level_impl.h>
-#include <log/logger_impl.h>
+#include <log/logger_name.h>
 #include <log/message_dictionary.h>
 #include <log/message_types.h>
-#include <log/root_logger_name.h>
 
 #include <util/strutil.h>
 
@@ -50,15 +49,19 @@ namespace log {
 LoggerImpl::LoggerImpl(const string& name) :
     logger_(log4cplus::Logger::getRoot())
 {
-    // Are we the root logger?
-    if (name == getRootLoggerName()) {
+    // Are we the root logger, or does the logger name start with
+    // the string "<root_logger_name>.".  If so, use a logger
+    // whose name is the one given.
+    if ((name == getRootLoggerName()) ||
+        (name.find(getRootLoggerName() + string(".")) == 0)) {
         name_ = name;
-        // logger_ already set to log4cplus root logger at this point
 
     } else {
+        // Anything else is assumed to be a sub-logger of the
+        // root logger.
         name_ = getRootLoggerName() + "." + name;
-        logger_ = log4cplus::Logger::getInstance(name);
     }
+    logger_ = log4cplus::Logger::getInstance(name);
 }
 
 // Destructor. (Here because of virtual declaration.)

+ 1 - 1
src/lib/log/logger_manager.cc

@@ -18,13 +18,13 @@
 #include <log/logger.h>
 #include <log/logger_manager_impl.h>
 #include <log/logger_manager.h>
+#include <log/logger_name.h>
 #include <log/messagedef.h>
 #include <log/message_dictionary.h>
 #include <log/message_exception.h>
 #include <log/message_initializer.h>
 #include <log/message_reader.h>
 #include <log/message_types.h>
-#include <log/root_logger_name.h>
 #include <log/macros.h>
 #include <log/messagedef.h>
 #include <log/message_initializer.h>

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

@@ -21,13 +21,12 @@
 #include <log4cplus/fileappender.h>
 #include <log4cplus/syslogappender.h>
 
+#include "log/logger.h"
 #include "log/logger_level_impl.h"
 #include "log/logger_manager.h"
 #include "log/logger_manager_impl.h"
+#include "log/logger_name.h"
 #include "log/logger_specification.h"
-#include "log/root_logger_name.h"
-
-#include "log/logger.h"
 #include "log/messagedef.h"
 
 using namespace std;

+ 16 - 1
src/lib/log/root_logger_name.cc

@@ -13,7 +13,7 @@
 // PERFORMANCE OF THIS SOFTWARE.
 
 #include <string>
-#include <root_logger_name.h>
+#include "log/logger_name.h"
 
 namespace isc {
 namespace log {
@@ -40,5 +40,20 @@ const std::string& getRootLoggerName() {
     return (getRootLoggerNameInternal());
 }
 
+std::string expandLoggerName(const std::string& name) {
+
+    // Are we the root logger, or does the logger name start with
+    // the string "<root_logger_name>.".  If so, use a logger
+    // whose name is the one given.
+    if ((name == getRootLoggerName()) ||
+        (name.find(getRootLoggerName() + std::string(".")) == 0)) {
+        return (name);
+
+    } 
+
+    // Anything else is assumed to be a sub-logger of the root logger.
+    return (getRootLoggerName() + "." + name);
+}
+
 } // namespace log
 } // namespace isc

+ 16 - 5
src/lib/log/root_logger_name.h

@@ -12,8 +12,8 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 
-#ifndef __ROOT_LOGGER_NAME_H
-#define __ROOT_LOGGER_NAME_H
+#ifndef __LOGGER_NAME_H
+#define __LOGGER_NAME_H
 
 #include <string>
 
@@ -27,7 +27,7 @@
 namespace isc {
 namespace log {
 
-/// \brief Set Root Logger Name
+/// \brief Set root logger name
 ///
 /// This function should be called by the program's initialization code before
 /// any logging functions are called.
@@ -35,12 +35,23 @@ namespace log {
 /// \param name Name of the root logger.  This should be the program name.
 void setRootLoggerName(const std::string& name);
 
-/// \brief Get Root Logger Name
+/// \brief Get root logger name
 ///
 /// \return Name of the root logger.
 const std::string& getRootLoggerName();
 
+/// \brief Expand logger name
+///
+/// Given a logger name, returns the fully-expanded logger name.  If the name
+/// starts with the root logger name, it is returned as-is.  Otherwise it is
+/// prefixed with the root logger name.
+///
+/// \param name Name to expand.
+///
+/// \return Fully-expanded logger name.
+std::string expandLoggerName(const std::string& name);
+
 }
 }
 
-#endif // __ROOT_LOGGER_NAME_H
+#endif // __LOGGER_NAME_H

+ 6 - 6
src/lib/log/tests/Makefile.am

@@ -18,14 +18,14 @@ run_unittests_SOURCES += log_formatter_unittest.cc
 run_unittests_SOURCES += logger_level_impl_unittest.cc
 run_unittests_SOURCES += logger_level_unittest.cc
 run_unittests_SOURCES += logger_manager_unittest.cc
+run_unittests_SOURCES += logger_name_unittest.cc
 run_unittests_SOURCES += logger_unittest.cc
 run_unittests_SOURCES += logger_specification_unittest.cc
-# run_unittests_SOURCES += message_dictionary_unittest.cc
-# run_unittests_SOURCES += message_initializer_unittest_2.cc
-# run_unittests_SOURCES += message_initializer_unittest.cc
-# run_unittests_SOURCES += message_reader_unittest.cc
-# run_unittests_SOURCES += output_option_unittest.cc
-# run_unittests_SOURCES += root_logger_name_unittest.cc
+run_unittests_SOURCES += message_dictionary_unittest.cc
+run_unittests_SOURCES += message_initializer_unittest_2.cc
+run_unittests_SOURCES += message_initializer_unittest.cc
+run_unittests_SOURCES += message_reader_unittest.cc
+run_unittests_SOURCES += output_option_unittest.cc
 
 run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES) $(LOG4CPLUS_INCLUDES)
 run_unittests_LDFLAGS  = $(AM_LDFLAGS)  $(GTEST_LDFLAGS)

+ 1 - 1
src/lib/log/tests/logger_example.cc

@@ -34,9 +34,9 @@
 #include <log/logger.h>
 #include <log/logger_level.h>
 #include <log/logger_manager.h>
+#include <log/logger_name.h>
 #include <log/logger_specification.h>
 #include <log/macros.h>
-#include <log/root_logger_name.h>
 
 // Include a set of message definitions.
 #include <log/messagedef.h>

+ 1 - 1
src/lib/log/tests/logger_level_unittest.cc

@@ -17,9 +17,9 @@
 
 #include <gtest/gtest.h>
 
-#include <log/root_logger_name.h>
 #include <log/logger.h>
 #include <log/logger_manager.h>
+#include <log/logger_name.h>
 #include <log/messagedef.h>
 
 using namespace isc;

+ 16 - 10
src/lib/log/tests/root_logger_name_unittest.cc

@@ -16,21 +16,14 @@
 
 #include <gtest/gtest.h>
 
-#include <log/root_logger_name.h>
+#include <log/logger_name.h>
 
 using namespace isc;
 using namespace isc::log;
 
-class RootLoggerNameTest : public ::testing::Test {
-protected:
-    RootLoggerNameTest()
-    {
-    }
-};
+// Check setting and getting of root name
 
-// Check of the (only) functionality of the class.
-
-TEST_F(RootLoggerNameTest, SetGet) {
+TEST(LoggerNameTest, RootNameSetGet) {
     const std::string name1 = "test1";
     const std::string name2 = "test2";
 
@@ -48,3 +41,16 @@ TEST_F(RootLoggerNameTest, SetGet) {
     setRootLoggerName(name2);
     EXPECT_EQ(name2, getRootLoggerName());
 }
+
+// Check expansion of name
+
+TEST(LoggerNameTest, ExpandLoggerName) {
+    const std::string ROOT = "example";
+    const std::string NAME = "something";
+    const std::string FULL_NAME = ROOT + "." + NAME;
+
+    setRootLoggerName(ROOT);
+    EXPECT_EQ(ROOT, expandLoggerName(ROOT));
+    EXPECT_EQ(FULL_NAME, expandLoggerName(NAME));
+    EXPECT_EQ(FULL_NAME, expandLoggerName(FULL_NAME));
+}

+ 1 - 1
src/lib/log/tests/logger_unittest.cc

@@ -17,9 +17,9 @@
 
 #include <gtest/gtest.h>
 
-#include <log/root_logger_name.h>
 #include <log/logger.h>
 #include <log/logger_manager.h>
+#include <log/logger_name.h>
 #include <log/messagedef.h>
 
 using namespace isc;

+ 1 - 1
src/lib/log/tests/output_option_unittest.cc

@@ -30,7 +30,7 @@ TEST(OutputOptionTest, Initialization) {
     EXPECT_EQ(OutputOption::DEST_CONSOLE, option.destination);
     EXPECT_EQ(OutputOption::STR_STDERR, option.stream);
     EXPECT_FALSE(option.flush);
-    EXPECT_EQ(string(""), option.facility);
+    EXPECT_EQ(string("LOCAL0"), option.facility);
     EXPECT_EQ(string(""), option.filename);
     EXPECT_EQ(0, option.maxsize);
     EXPECT_EQ(0, option.maxver);