Browse Source

Additional tests for logging module

Added additional tests to check the formatting methods supplied
in the first version of the logging.
Stephen Morris 14 years ago
parent
commit
758460fa8a
3 changed files with 75 additions and 5 deletions
  1. 12 1
      src/lib/log/README.txt
  2. 10 0
      src/lib/log/strutil.h
  3. 53 4
      src/lib/log/tests/logger_unittest.cc

+ 12 - 1
src/lib/log/README.txt

@@ -14,7 +14,7 @@ Outstanding
 ===========
 * Ability to configure system according to configuration database.
 * Writing of suitable appenders and formatters.
-* Ability to override message via a run-time file.
+* Overrididing message at run-time with a localised file.
 * Update the build procedure to create .cc and .h files from the .msg file
   during the build process. (Requires that the message compiler is built first.)
 
@@ -151,3 +151,14 @@ To use the current version of the logging:
 
 As noted above, presently the only logging is to the console using the default
 log4cxx format (which is somewhat awkward to read).
+
+
+Notes
+=====
+The message compiler is written in C++ (instead of Python) because it contains
+a component that reads the message file.  This component is used in both the
+message compiler and the server; in the server it is used when the server
+starts up (or when triggered by a command) to read in a message file to
+overwrite the internal dictionary.  Writing it in C++ means there is only
+one piece of code that does this functionality.
+

+ 10 - 0
src/lib/log/strutil.h

@@ -55,6 +55,16 @@ std::string trim(const std::string& instring);
 /// the delimiter characters) and returns the tokens in a vector array. Note
 /// that adjacent delimiters are considered to be a single delimiter.
 ///
+/// Special cases are:
+/// 1) The empty string is considered to be zero tokens.
+/// 2) A string comprising nothing but delimiters is considered to be zero
+///    tokens.
+///
+/// The reasoning behind this is that the string can be thought of as having
+/// invisible leading and trailing delimiter characters.  Therefore both cases
+/// reduce to a set of contiguous delimiters, which are considered a single
+/// delimiter (so getting rid of the string).
+///
 /// We could use Boost for this, but this (simple) function eliminates one
 /// dependency in the code.
 ///

+ 53 - 4
src/lib/log/tests/logger_unittest.cc

@@ -21,9 +21,11 @@
 
 #include <log/root_logger_name.h>
 #include <log/logger.h>
+#include <log/messagedef.h>
 
 using namespace isc;
 using namespace isc::log;
+using namespace std;
 
 namespace isc {
 namespace log {
@@ -36,7 +38,7 @@ namespace log {
 class TestLogger : public Logger {
 public:
     /// \brief constructor
-    TestLogger(const std::string& name) : Logger(name)
+    TestLogger(const string& name) : Logger(name)
     {}
 
     /// \brief Logger Equality
@@ -76,7 +78,7 @@ TEST_F(LoggerTest, Name) {
     Logger logger("alpha");
 
     // ... and check the name
-    EXPECT_EQ(std::string("test1.alpha"), logger.getName());
+    EXPECT_EQ(string("test1.alpha"), logger.getName());
 }
 
 // This test attempts to get two instances of a logger with the same name
@@ -88,8 +90,8 @@ TEST_F(LoggerTest, GetLogger) {
     // case in the program(.
     RootLoggerName::setName("test2");
 
-    const std::string name1 = "alpha";
-    const std::string name2 = "beta";
+    const string name1 = "alpha";
+    const string name2 = "beta";
 
     // Instantiate two loggers that should be the same
     TestLogger logger1(name1);
@@ -381,3 +383,50 @@ TEST_F(LoggerTest, IsDebugEnabledLevel) {
     EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
     EXPECT_TRUE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
 }
+
+// Check that the message formatting is correct.  As this test program is
+// linking with the logger library - which includes messages from the logger
+// itself - we'll use those messages for testing.
+
+TEST_F(LoggerTest, Format) {
+    RootLoggerName::setName("test9");
+    Logger logger("test9");
+
+// Individual arguments
+    string result = logger.formatMessage(MSG_OPENIN);
+    EXPECT_EQ(string("OPENIN, unable to open %s for input: %s"), result);
+
+    vector<string> args;
+    args.push_back("alpha.txt");
+
+    result = logger.formatMessage(MSG_OPENIN, &args);
+    EXPECT_EQ(string("OPENIN, unable to open alpha.txt for input: %s"), result);
+
+    args.push_back("test");
+    result = logger.formatMessage(MSG_OPENIN, &args);
+    EXPECT_EQ(string("OPENIN, unable to open alpha.txt for input: test"), result);
+
+    // Excess arguments should be ignored
+    args.push_back("ignore me");
+    result = logger.formatMessage(MSG_OPENIN, &args);
+    EXPECT_EQ(string("OPENIN, unable to open alpha.txt for input: test"), result);
+
+    // Try the same using concatenated arguments
+    string strarg = "alpha.txt";
+    result = logger.formatMessage(MSG_OPENIN, strarg);
+    EXPECT_EQ(string("OPENIN, unable to open alpha.txt for input: %s"), result);
+
+    strarg += "\0test";
+    result = logger.formatMessage(MSG_OPENIN, &args);
+    EXPECT_EQ(string("OPENIN, unable to open alpha.txt for input: test"), result);
+
+    // With the latter method, try a few "unusual" argument strings
+    strarg = "";
+    result = logger.formatMessage(MSG_OPENIN, strarg);
+    EXPECT_EQ(string("OPENIN, unable to open %s for input: %s"), result);
+    
+    strarg="\0";
+    result = logger.formatMessage(MSG_OPENIN, strarg);
+    EXPECT_EQ(string("OPENIN, unable to open %s for input: %s"), result);
+
+}