message_dictionary.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef MESSAGE_DICTIONARY_H
  15. #define MESSAGE_DICTIONARY_H
  16. #include <cstddef>
  17. #include <string>
  18. #include <map>
  19. #include <vector>
  20. #include <boost/lexical_cast.hpp>
  21. #include <log/message_types.h>
  22. namespace isc {
  23. namespace log {
  24. /// \brief Message Dictionary
  25. ///
  26. /// The message dictionary is a wrapper around a std::map object, and allows
  27. /// message text to be retrieved given the string identification.
  28. ///
  29. /// Adding text occurs in two modes:
  30. ///
  31. /// Through the "Add" method, ID/text mappings are added to the dictionary
  32. /// unless the ID already exists. This is designed for use during program
  33. /// initialization, where a local message may supplant a compiled-in message.
  34. ///
  35. /// Through the "Replace" method, ID/text mappings are added to the dictionary
  36. /// only if the ID already exists. This is for use when a message file is
  37. /// supplied to replace messages provided with the program.
  38. ///
  39. /// Although the class can be used stand-alone, it does supply a static method
  40. /// to return a particular instance - the "global" dictionary.
  41. class MessageDictionary {
  42. public:
  43. typedef std::map<std::string, std::string> Dictionary;
  44. typedef Dictionary::const_iterator const_iterator;
  45. // Default constructor and assignment operator are OK for this class
  46. /// \brief Virtual Destructor
  47. virtual ~MessageDictionary();
  48. /// \brief Add Message
  49. ///
  50. /// Adds a message to the dictionary. If the ID already exists, the ID is
  51. /// added to the overflow vector.
  52. ///
  53. /// \param ident Identification of the message to add
  54. /// \param text Message text
  55. ///
  56. /// \return true if the message was added to the dictionary, false if the
  57. /// message existed and it was not added.
  58. virtual bool add(const MessageID& ident, const std::string& text) {
  59. return (add(boost::lexical_cast<std::string>(ident), text));
  60. }
  61. /// \brief Add Message
  62. ///
  63. /// Alternate signature.
  64. ///
  65. /// \param ident Identification of the message to add
  66. /// \param text Message text
  67. ///
  68. /// \return true if the message was added to the dictionary, false if the
  69. /// message existed and it was not added.
  70. virtual bool add (const std::string& ident, const std::string& text);
  71. /// \brief Replace Message
  72. ///
  73. /// Replaces a message in the dictionary. If the ID does not exist, it is
  74. /// added to the overflow vector.
  75. ///
  76. /// \param ident Identification of the message to replace
  77. /// \param text Message text
  78. ///
  79. /// \return true if the message was added to the dictionary, false if the
  80. /// message did not exist and it was not added.
  81. virtual bool replace(const MessageID& ident, const std::string& text) {
  82. return (replace(boost::lexical_cast<std::string>(ident), text));
  83. }
  84. /// \brief Replace Message
  85. ///
  86. /// Alternate signature.
  87. ///
  88. /// \param ident Identification of the message to replace
  89. /// \param text Message text
  90. ///
  91. /// \return true if the message was added to the dictionary, false if the
  92. /// message did not exist and it was not added.
  93. virtual bool replace(const std::string& ident, const std::string& text);
  94. /// \brief Load Dictionary
  95. ///
  96. /// Designed to be used during the initialization of programs, this
  97. /// accepts a set of (ID, text) pairs as a one-dimensional array of
  98. /// const char* and adds them to the dictionary. The messages are added
  99. /// using "Add".
  100. ///
  101. /// \param elements null-terminated array of const char* alternating ID and
  102. /// message text. This should be an odd number of elements long, the last
  103. /// element being NULL. If it is an even number of elements long, the
  104. /// last ID is ignored.
  105. ///
  106. /// \return Vector of message IDs that were not loaded because an ID of the
  107. /// same name already existing in the dictionary. This vector may be
  108. /// empty.
  109. virtual std::vector<std::string> load(const char* elements[]);
  110. /// \brief Get Message Text
  111. ///
  112. /// Given an ID, retrieve associated message text.
  113. ///
  114. /// \param ident Message identification
  115. ///
  116. /// \return Text associated with message or empty string if the ID is not
  117. /// recognized. (Note: this precludes an ID being associated with an empty
  118. /// string.)
  119. virtual const std::string& getText(const MessageID& ident) const {
  120. return(getText(boost::lexical_cast<std::string>(ident)));
  121. }
  122. /// \brief Get Message Text
  123. ///
  124. /// Alternate signature.
  125. ///
  126. /// \param ident Message identification
  127. ///
  128. /// \return Text associated with message or empty string if the ID is not
  129. /// recognized. (Note: this precludes an ID being associated with an empty
  130. /// string.)
  131. virtual const std::string& getText(const std::string& ident) const;
  132. /// \brief Number of Items in Dictionary
  133. ///
  134. /// \return Number of items in the dictionary
  135. virtual size_t size() const {
  136. return (dictionary_.size());
  137. }
  138. /// \brief Return begin() iterator of internal map
  139. const_iterator begin() const {
  140. return (dictionary_.begin());
  141. }
  142. /// \brief Return end() iterator of internal map
  143. const_iterator end() const {
  144. return (dictionary_.end());
  145. }
  146. /// \brief Return Global Dictionary
  147. ///
  148. /// Returns a pointer to the singleton global dictionary.
  149. ///
  150. /// \return Pointer to global dictionary.
  151. static MessageDictionary& globalDictionary();
  152. private:
  153. Dictionary dictionary_; ///< Holds the ID to text lookups
  154. };
  155. } // namespace log
  156. } // namespace isc
  157. #endif // MESSAGE_DICTIONARY_H