message_dictionary.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // $Id$
  15. #ifndef __MESSAGE_DICTIONARY_H
  16. #define __MESSAGE_DICTIONARY_H
  17. #include <cstddef>
  18. #include <string>
  19. #include <map>
  20. #include <vector>
  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. // Default constructor and assignment operator are OK for this class
  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. /// \brief Replace Message
  58. ///
  59. /// Replaces a message in the dictionary. If the ID does not exist, it is
  60. /// added to the overflow vector.
  61. ///
  62. /// \param ident Identification of the message to replace
  63. /// \param text Message text
  64. ///
  65. /// \return true if the message was added to the dictionary, false if the
  66. /// message did not exist and it was not added.
  67. virtual bool replace(const MessageID& ident, const std::string& text);
  68. /// \brief Load Dictionary
  69. ///
  70. /// Designed to be used during the initialization of programs, this
  71. /// accepts a set of (ID, text) pairs as a one-dimensional array of
  72. /// const char* and adds them to the dictionary. The messages are added
  73. /// using "Add".
  74. ///
  75. /// \param data null-terminated array of const char* alternating ID and
  76. /// message text. This should be an odd number of elements long, the last
  77. /// elemnent being NULL. If it is an even number of elements long, the
  78. /// last ID is ignored.
  79. ///
  80. /// \return Vector of message IDs that were not loaded because an ID of the
  81. /// same name already existing in the dictionary. This vector may be
  82. /// empty.
  83. virtual std::vector<MessageID> load(const char* elements[]);
  84. /// \brief Get Message Text
  85. ///
  86. /// Given an ID, retrieve associated message text.
  87. ///
  88. /// \param ident Message identification
  89. ///
  90. /// \return Text associated with message or empty string if the ID is not
  91. /// recognised. (Note: this precludes an ID being associated with an empty
  92. /// string.)
  93. virtual std::string getText(const MessageID& ident) const;
  94. /// \brief Number of Items in Dictionary
  95. ///
  96. /// \return Number of items in the dictionary
  97. virtual size_t size() const {
  98. return dictionary_.size();
  99. }
  100. // Allow access to the internal map structure, but don't allow alteration.
  101. typedef std::map<MessageID, std::string>::const_iterator const_iterator;
  102. /// \brief Return begin() iterator of internal map
  103. const_iterator begin() const {
  104. return dictionary_.begin();
  105. }
  106. /// \brief Return end() iterator of internal map
  107. const_iterator end() const {
  108. return dictionary_.end();
  109. }
  110. /// \brief Return Global Dictionary
  111. ///
  112. /// Returns a pointer to the singleton global dictionary.
  113. ///
  114. /// \return Pointer to global dictionary.
  115. static MessageDictionary* globalDictionary();
  116. private:
  117. std::map<MessageID, std::string> dictionary_;
  118. };
  119. } // namespace log
  120. } // namespace isc
  121. #endif // __MESSAGE_DICTIONARY_H