Browse Source

[2746] Constify getDuplicates()

This does have the side-effect of no longer sorting the printed list in the logger manager.
(if we do need that, we should add a MessageInitializer::sortDuplicates i think)
Jelte Jansen 12 years ago
parent
commit
83d8c2ef2f

+ 4 - 6
src/lib/log/logger_manager.cc

@@ -121,15 +121,13 @@ LoggerManager::init(const std::string& root, isc::log::Severity severity,
 
     // Check if there were any duplicate message IDs in the default dictionary
     // and if so, log them.  Log using the logging facility logger.
-    vector<string>& duplicates = MessageInitializer::getDuplicates();
+    const vector<string>& duplicates = MessageInitializer::getDuplicates();
     if (!duplicates.empty()) {
 
-        // There are duplicates present.  This will be listed in alphabetic
-        // order of message ID, so they need to be sorted.  This list itself may
-        // contain duplicates; if so, the message ID is listed as many times as
+        // There are duplicates present. This list itself may contain
+        // duplicates; if so, the message ID is listed as many times as
         // there are duplicates.
-        sort(duplicates.begin(), duplicates.end());
-        for (vector<string>::iterator i = duplicates.begin();
+        for (vector<string>::const_iterator i = duplicates.begin();
              i != duplicates.end(); ++i) {
             LOG_WARN(logger, LOG_DUPLICATE_MESSAGE_ID).arg(*i);
         }

+ 15 - 8
src/lib/log/message_initializer.cc

@@ -42,7 +42,13 @@ size_t& getIndex() {
     return (index);
 }
 
+// Return the duplicates singleton version (non-const for local use)
+std::vector<std::string>&
+getNonConstDuplicates() {
+    static std::vector<std::string> duplicates;
+    return (duplicates);
 }
+} // end unnamed namespace
 
 
 namespace isc {
@@ -76,7 +82,7 @@ MessageInitializer::loadDictionary(bool ignore_duplicates) {
         // Append the IDs in the list just loaded (the "repeats") to the
         // global list of duplicate IDs.
         if (!ignore_duplicates && !repeats.empty()) {
-            std::vector<std::string>& duplicates = getDuplicates();
+            std::vector<std::string>& duplicates = getNonConstDuplicates();
             duplicates.insert(duplicates.end(), repeats.begin(),
                               repeats.end());
         }
@@ -88,15 +94,16 @@ MessageInitializer::loadDictionary(bool ignore_duplicates) {
     getIndex() = 0;
 }
 
-// Return reference to duplicate array
-std::vector<std::string>& MessageInitializer::getDuplicates() {
-    static std::vector<std::string> duplicates;
-    return (duplicates);
+// Return reference to duplicates vector
+const std::vector<std::string>&
+MessageInitializer::getDuplicates() {
+    return (getNonConstDuplicates());
 }
 
-// Clear the duplicate array
-void MessageInitializer::clearDuplicates() {
-    getDuplicates().clear();
+// Clear the duplicates vector
+void
+MessageInitializer::clearDuplicates() {
+    getNonConstDuplicates().clear();
 }
 
 } // namespace log

+ 2 - 2
src/lib/log/message_initializer.h

@@ -102,11 +102,11 @@ public:
     ///
     /// \return List of duplicate message IDs when the global dictionary was
     /// loaded.  Note that the duplicates list itself may contain duplicates.
-    static std::vector<std::string>& getDuplicates();
+    static const std::vector<std::string>& getDuplicates();
 
     /// \brief Clear the static duplicates vector
     ///
-    /// Empties the vector returned by getDuplicates
+    /// Empties the vector returned by getDuplicates()
     static void clearDuplicates();
 };
 

+ 1 - 2
src/lib/log/tests/message_dictionary_unittest.cc

@@ -190,8 +190,7 @@ TEST_F(MessageDictionaryTest, GlobalTest) {
 
 TEST_F(MessageDictionaryTest, GlobalLoadTest) {
     // There were duplicates but the vector should be cleared in init() now
-    vector<string>& duplicates = MessageInitializer::getDuplicates();
-    ASSERT_EQ(0, duplicates.size());
+    ASSERT_EQ(0, MessageInitializer::getDuplicates().size());
 
     string text = MessageDictionary::globalDictionary().getText("NEWSYM");
     EXPECT_EQ(string("new symbol added"), text);