message_dictionary.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (C) 2011-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef MESSAGE_DICTIONARY_H
  7. #define MESSAGE_DICTIONARY_H
  8. #include <cstddef>
  9. #include <string>
  10. #include <map>
  11. #include <vector>
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <log/message_types.h>
  15. namespace isc {
  16. namespace log {
  17. /// \brief Forward declaration of \c MessageDictionary
  18. class MessageDictionary;
  19. /// \brief Shared pointer to the \c MessageDictionary.
  20. typedef boost::shared_ptr<MessageDictionary> MessageDictionaryPtr;
  21. /// \brief Message Dictionary
  22. ///
  23. /// The message dictionary is a wrapper around a std::map object, and allows
  24. /// message text to be retrieved given the string identification.
  25. ///
  26. /// Adding text occurs in two modes:
  27. ///
  28. /// Through the "Add" method, ID/text mappings are added to the dictionary
  29. /// unless the ID already exists. This is designed for use during program
  30. /// initialization, where a local message may supplant a compiled-in message.
  31. ///
  32. /// Through the "Replace" method, ID/text mappings are added to the dictionary
  33. /// only if the ID already exists. This is for use when a message file is
  34. /// supplied to replace messages provided with the program.
  35. ///
  36. /// Although the class can be used stand-alone, it does supply a static method
  37. /// to return a particular instance - the "global" dictionary.
  38. class MessageDictionary {
  39. public:
  40. typedef std::map<std::string, std::string> Dictionary;
  41. typedef Dictionary::const_iterator const_iterator;
  42. /// \brief Constructor
  43. MessageDictionary();
  44. /// \brief Virtual Destructor
  45. virtual ~MessageDictionary();
  46. /// \brief Add Message
  47. ///
  48. /// Adds a message to the dictionary. If the ID already exists, the ID is
  49. /// added to the overflow vector.
  50. ///
  51. /// \param ident Identification of the message to add
  52. /// \param text Message text
  53. ///
  54. /// \return true if the message was added to the dictionary, false if the
  55. /// message existed and it was not added.
  56. virtual bool add(const MessageID& ident, const std::string& text) {
  57. return (add(boost::lexical_cast<std::string>(ident), text));
  58. }
  59. /// \brief Add Message
  60. ///
  61. /// Alternate signature.
  62. ///
  63. /// \param ident Identification of the message to add
  64. /// \param text Message text
  65. ///
  66. /// \return true if the message was added to the dictionary, false if the
  67. /// message existed and it was not added.
  68. virtual bool add (const std::string& ident, const std::string& text);
  69. /// \brief Replace Message
  70. ///
  71. /// Replaces a message in the dictionary. If the ID does not exist, it is
  72. /// added to the overflow vector.
  73. ///
  74. /// \param ident Identification of the message to replace
  75. /// \param text Message text
  76. ///
  77. /// \return true if the message was added to the dictionary, false if the
  78. /// message did not exist and it was not added.
  79. virtual bool replace(const MessageID& ident, const std::string& text) {
  80. return (replace(boost::lexical_cast<std::string>(ident), text));
  81. }
  82. /// \brief Replace Message
  83. ///
  84. /// Alternate signature.
  85. ///
  86. /// \param ident Identification of the message to replace
  87. /// \param text Message text
  88. ///
  89. /// \return true if the message was added to the dictionary, false if the
  90. /// message did not exist and it was not added.
  91. virtual bool replace(const std::string& ident, const std::string& text);
  92. /// \brief Removes the specified message from the dictionary.
  93. ///
  94. /// Checks if both the message identifier and the text match the message
  95. /// in the dictionary before removal. If the text doesn't match it is
  96. /// an indication that the message which removal is requested is a
  97. /// duplicate of another message. This may occur when two Kea modules
  98. /// register messages with the same identifier. When one of the modules
  99. /// is unloaded and the relevant messages are unregistered, there is a
  100. /// need to make sure that the message registered by the other module
  101. /// is not accidentally removed. Hence, the additional check for the
  102. /// text match is needed.
  103. ///
  104. /// \param ident Identification of the message to remove.
  105. /// \param text Message text
  106. ///
  107. /// \return true of the message has been removed, false if the message
  108. /// couldn't be found.
  109. virtual bool erase(const std::string& ident, const std::string& text);
  110. /// \brief Load Dictionary
  111. ///
  112. /// Designed to be used during the initialization of programs, this
  113. /// accepts a set of (ID, text) pairs as a one-dimensional array of
  114. /// const char* and adds them to the dictionary. The messages are added
  115. /// using "Add".
  116. ///
  117. /// \param elements null-terminated array of const char* alternating ID and
  118. /// message text. This should be an odd number of elements long, the last
  119. /// element being NULL. If it is an even number of elements long, the
  120. /// last ID is ignored.
  121. ///
  122. /// \return Vector of message IDs that were not loaded because an ID of the
  123. /// same name already existing in the dictionary. This vector may be
  124. /// empty.
  125. virtual std::vector<std::string> load(const char* elements[]);
  126. /// \brief Get Message Text
  127. ///
  128. /// Given an ID, retrieve associated message text.
  129. ///
  130. /// \param ident Message identification
  131. ///
  132. /// \return Text associated with message or empty string if the ID is not
  133. /// recognized. (Note: this precludes an ID being associated with an empty
  134. /// string.)
  135. virtual const std::string& getText(const MessageID& ident) const {
  136. return(getText(boost::lexical_cast<std::string>(ident)));
  137. }
  138. /// \brief Get Message Text
  139. ///
  140. /// Alternate signature.
  141. ///
  142. /// \param ident Message identification
  143. ///
  144. /// \return Text associated with message or empty string if the ID is not
  145. /// recognized. (Note: this precludes an ID being associated with an empty
  146. /// string.)
  147. virtual const std::string& getText(const std::string& ident) const;
  148. /// \brief Number of Items in Dictionary
  149. ///
  150. /// \return Number of items in the dictionary
  151. virtual size_t size() const {
  152. return (dictionary_.size());
  153. }
  154. /// \brief Return begin() iterator of internal map
  155. const_iterator begin() const {
  156. return (dictionary_.begin());
  157. }
  158. /// \brief Return end() iterator of internal map
  159. const_iterator end() const {
  160. return (dictionary_.end());
  161. }
  162. /// \brief Return Global Dictionary
  163. ///
  164. /// Returns a pointer to the singleton global dictionary.
  165. ///
  166. /// \return Pointer to global dictionary.
  167. static const MessageDictionaryPtr& globalDictionary();
  168. private:
  169. Dictionary dictionary_; ///< Holds the ID to text lookups
  170. const std::string empty_; ///< Empty string
  171. };
  172. } // namespace log
  173. } // namespace isc
  174. #endif // MESSAGE_DICTIONARY_H